我的EXPLAIN查询中有以下内容。我不知道如何使用它来提高我的查询性能。我的查询如下:
SELECT
`b`.`business_name` AS `Name`,
`b`.`address` AS `Address`,
`b`.`city` AS `City`,
`b`.`phone_number` AS `Phone`,
`b`.`state` AS `Prov`,
`i`.`date` AS `Last_Observed`,
group_concat(`v`.`notes` separator ', ') AS `Notes`,
`v`.`critical` AS `Criticality`,
`i`.`type` AS `Inspection_Type`,
`i`.`rating` AS `Rating`
FROM (`fs_v1_violation_table` `v`
LEFT JOIN (`fs_v1_inspection_table` `i`
LEFT JOIN `fs_v1_business_table` `b` ON((`b`.`id` = `i`.`business_id`)))
ON((`i`.`id` = `v`.`inspection_id`)))
WHERE `v`.`type` = 'Cleanliness' AND (
b.city = 'North Vancouver'
OR b.city = 'Vancouver'
OR b.city = 'White Rock'
OR b.city = 'West Vancouver'
OR b.city = 'Burnaby'
OR b.city = 'Langley'
OR b.city = 'Maple Ridge'
OR b.city = 'Delta'
OR b.city = 'Surrey')
GROUP BY `i`.`id`
ORDER BY `i`.`date` desc;
关于我可能会在哪些方面提高性能的想法?查询大约需要3分钟。
答案 0 :(得分:1)
尝试尽快减少行数。
从'b.city'列创建索引。
示例SQL。
SELECT
`b`.`business_name` AS `Name`,
`b`.`address` AS `Address`,
`b`.`city` AS `City`,
`b`.`phone_number` AS `Phone`,
`b`.`state` AS `Prov`,
`i`.`date` AS `Last_Observed`,
group_concat(`v`.`notes` separator ', ') AS `Notes`,
`v`.`critical` AS `Criticality`,
`i`.`type` AS `Inspection_Type`,
`i`.`rating` AS `Rating`
FROM `fs_v1_violation_table` AS `v`
LEFT JOIN `fs_v1_inspection_table` AS `i` ON `i`.`id` = `v`.`inspection_id`
LEFT JOIN `fs_v1_business_table` AS `b` ON `b`.`id` = `i`.`business_id` AND b.city IN ('North Vancouver', 'Vancouver', 'White Rock', 'West Vancouver', 'Burnaby', 'Langley', 'Maple Ridge', 'Delta', 'Surrey')
WHERE `v`.`type` = 'Cleanliness'
GROUP BY `i`.`id`
ORDER BY `i`.`date` desc;
答案 1 :(得分:0)
首先,您的查询似乎是针对sql标准的,因为选择列表包含不在列表中的字段,也不在功能上依赖于列表中的字段,也不使用聚合聚合min()
或sum()
等功能。不幸的是,MySQL允许在某些sql模式设置下运行此类查询。幸运的是,此设置现在默认关闭。
使用IN (...)
代替一系列OR,并确保在fs_v1_business_table
表的id, city
字段上有多列索引以加快查询速度。您还可以考虑在fs_v1_inspection_table
表的business_id, id, date
字段上创建多列索引。