【问题标题】:Laravel, Datatables, inefficient queriesLaravel,数据表,低效查询
【发布时间】:2018-04-25 10:58:30
【问题描述】:

一些开发人员使用 Laravel 和 Datatables 制作了 PHP 软件。 当我看到应用程序生成的数据库查询时,我发现一个效率很低。示例(涉及的列列表从一种软件功能更改为另一种):

 select count(*) as aggregate from (select '1' as [row_count] from [assignments]
  left join [jobs] on [assignments].[id] = [jobs].[name] 
  left join [categories] on [categories].[id] = [jobs].[vol_cat_type]
   where ([assignments].[id] LIKE '%XYZ%' or [assignments].[person_id] LIKE '%XYZ%' or 
    (select count(1) from [people] where [assignments].[person_id] = [people].[person_id] and [people].[name_title] LIKE '%XYZ%') >= 1 or 
    (select count(1) from [people] where [assignments].[person_id] = [people].[person_id] and [people].[first_name] LIKE '%XYZ%') >= 1 or 
    [assignments].[title] LIKE '%XYZ%' or 
    (select count(1) from [people] where [assignments].[person_id] = [people].[person_id] and [people].[gender] LIKE '%XYZ%') >= 1 or 
    (select count(1) from [states_master] where [states_master].[identity] = [assignments].[current_state] and [states_master].[name] LIKE '%XYZ%') >= 1 or
        [assignments].[updated_datetime] LIKE '%XYZ%' or
        (select count(1) from [people] where [assignments].[person_id] = [people].[person_id] and [people].[nationality] LIKE '%XYZ%') >= 1 or 
    (select count(1) from [people] where [assignments].[person_id] = [people].[person_id] and [people].[place_of_birth] LIKE '%XYZ%') >= 1 or 
    (select count(1) from [people] where [assignments].[person_id] = [people].[person_id] and [people].[civil_status] LIKE '%XYZ%') >= 1 or 
    (select count(1) from [people] where [assignments].[person_id] = [people].[person_id] and [people].[email] LIKE '%XYZ%') >= 1 or 
    (select count(1) from [people] where [assignments].[person_id] = [people].[person_id] and [people].[date_of_birth] LIKE '%XYZ%') >= 1 or 
    (select count(1) from [people] where [assignments].[person_id] = [people].[person_id] and [people].[nationality2] LIKE '%XYZ%') >= 1
      or (select count(1) from [people] where [assignments].[person_id] = [people].[person_id] and [people].[skype] LIKE '%XYZ%') >= 1))
       count_row_table

涉及的表很大,而且这些查询不仅执行需要 20-30 秒,而且会影响服务器的总体性能。 我不喜欢的是许多 LIKE 应用于所有列,尤其是那些应用于非字符串的列(如最后修改日期)。 我的建议是:

1) 对所有非字符串使用 = 而不是 LIKE:数字、单字符代码(如性别 M/F、布尔值)

2) 配置Microsoft SQL Server中包含的全文搜索,写CONTAINS(column,'XYZ')而不是LIKE

开发者的反对意见是 Laravel 和 Datatables 会产生这些查询,我们对此无能为力。

是否可以在不更改库代码的情况下配置 Laravel 和 Datatables,为每一列指定是否使用 LIKE、= 或 CONTAINS?

【问题讨论】:

    标签: php sql-server laravel datatables


    【解决方案1】:

    Eloquent 通常用于创建更简单的模型查询。

    对于更复杂的 SQL 查询,请使用查询生成器。

    这是文档中有关如何使用它的示例。

    $users = DB::table('users')
                         ->select(DB::raw('count(*) as user_count, status'))
                         ->where('status', '<>', 1)
                         ->groupBy('status')
                         ->get();
    

    如果您愿意,也可以使用原始 sql。

    $results = DB::select('select * from users where id = :id', ['id' => 1]);
    

    参考资料:

    https://laravel.com/docs/5.6/queries

    https://laravel.com/docs/5.6/database#running-queries

    【讨论】:

      猜你喜欢
      • 2012-05-31
      • 2020-05-03
      • 1970-01-01
      • 2011-07-28
      • 1970-01-01
      • 2022-01-21
      • 2011-12-13
      • 2013-01-28
      • 2023-03-22
      相关资源
      最近更新 更多