【发布时间】: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