[英]Optimize MySQL query to avoid “Using where; Using temporary; Using filesort”
[英]MySQL - Optimize Query To Remove Using Temporary and Using filesort
SELECT SQL_NO_CACHE
`Table1`.`recordID`
FROM
`Table1`
LEFT JOIN `Table3` ON `Table3`.`table1RecordID`=`Table1`.`recordID`
WHERE
(`Table3`.`status` = '3' OR `Table3`.`status` = '4') AND
(`Table1`.`groupName` = 'Sample Name')
GROUP BY `Table3`.`recordID` ASC, `Table1`.`recordID` ASC;
+----+-------------+--------+--------+------------------------------+---------+---------+------------------------------+-------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+--------+------------------------------+---------+---------+------------------------------+-------+----------------------------------------------+ | 1 | SIMPLE | Table3 | ALL | fk_packageID,regStatus,pkgID | NULL | NULL | NULL | 11322 | Using where; Using temporary; Using filesort | | 1 | SIMPLE | Table1 | eq_ref | PRIMARY,groupName | PRIMARY | 4 | testDb.Table3.table1RecordID | 1 | Using where | +----+-------------+--------+--------+------------------------------+---------+---------+------------------------------+-------+----------------------------------------------+
如果删除了GROUP BY的第二部分,“ Table1
。 recordID
ASC”,但数据不是我做的正确。 为什么这样做,以及如何修复它,并且除Table3之外,仍然按Table1分组。
提前致谢!
我有时间进行完整的查询,然后将表拉成通用形式以发布没有客户端数据的表单。 我能够向sqlfiddle添加架构,但是如果没有我使用的数据,结果可能会有所不同,并且由于字符限制,我什至无法将100行预表(共7行)放入sqlfiddle中。 因此,我转储了表,并通过保管箱进行共享。
https://www.dropbox.com/s/9fgu626996utpar/stackoverflow-21291707_test_db_schema_and_data.sql
SELECT `t1`.`name` AS `Object1.Name`, GROUP_CONCAT(DISTINCT IF(`t5`.`questionID`=68, IF(`t6`.`writeInRequired` = 1, CONCAT( `t6`.`value`, ':', `t5`.`writeInResponse` ), `t6`.`value` ), NULL ) SEPARATOR ', ') AS `Object3.Response_68`, GROUP_CONCAT(DISTINCT IF(`t5`.`questionID`=67, IF(`t6`.`writeInRequired` = 1, CONCAT( `t6`.`value`, ':', `t5`.`writeInResponse` ), `t6`.`value` ), NULL ) SEPARATOR ', ') AS `Object3.Response_67`, GROUP_CONCAT(DISTINCT IF(`t5`.`questionID`=66, IF(`t6`.`writeInRequired` = 1, CONCAT( `t6`.`value`, ':', `t5`.`writeInResponse` ), `t6`.`value` ), NULL ) SEPARATOR ', ') AS `Object3.Response_66`, `t7`.`firstName` AS `Object8.FirstName`, `t7`.`lastName` AS `Object8.LastName`, `t7`.`email` AS `Object8.Email`, `t1`.`recordID` AS `Object1.PackageID`, `t3`.`recordID` AS `Object5.RegistrationID` FROM `Table1` t1 LEFT JOIN `Table2` t2 ON `t1`.`recordID`=`t2`.`table1RecordID` LEFT JOIN `Table3` t3 ON `t3`.`table1RecordID`=`t1`.`recordID` LEFT JOIN `Table4` t4 ON `t4`.`table3RecordID`=`t3`.`recordID` AND `t4`.`type` = 1 LEFT JOIN `Table5` t5 ON `t5`.`objectID`=`t3`.`recordID` AND `t5`.`objectType`='Type2' LEFT JOIN `Table6` t6 ON `t6`.`recordID`=`t5`.`table6RecordID` JOIN `Table7` t7 ON `t7`.`recordID`=`t4`.`table7RecordID` WHERE `t3`.`status` IN ('3','4') GROUP BY `Object5.RegistrationID` ASC, `Object1.PackageID` ASC
*************************** 1. row *************************** id: 1 select_type: SIMPLE table: t7 type: ALL possible_keys: PRIMARY key: NULL key_len: NULL ref: NULL rows: 11627 filtered: 100.00 Extra: Using temporary; Using filesort *************************** 2. row *************************** id: 1 select_type: SIMPLE table: t4 type: ref possible_keys: idx_table7RecordID,idx_table3RecordID key: idx_table7RecordID key_len: 5 ref: testDb.t7.recordID rows: 1 filtered: 100.00 Extra: Using where *************************** 3. row *************************** id: 1 select_type: SIMPLE table: t3 type: eq_ref possible_keys: PRIMARY,table1RecordID_status,idx_status,idx_table1RecordID key: PRIMARY key_len: 4 ref: testDb.t4.table3RecordID rows: 1 filtered: 100.00 Extra: Using where *************************** 4. row *************************** id: 1 select_type: SIMPLE table: t5 type: ref possible_keys: compositeIDs key: compositeIDs key_len: 773 ref: const,testDb.t3.recordID rows: 5 filtered: 100.00 Extra: *************************** 5. row *************************** id: 1 select_type: SIMPLE table: t6 type: eq_ref possible_keys: PRIMARY key: PRIMARY key_len: 4 ref: testDb.t5.table6RecordID rows: 1 filtered: 100.00 Extra: *************************** 6. row *************************** id: 1 select_type: SIMPLE table: t1 type: eq_ref possible_keys: PRIMARY key: PRIMARY key_len: 4 ref: testDb.t3.table1RecordID rows: 1 filtered: 100.00 Extra: *************************** 7. row *************************** id: 1 select_type: SIMPLE table: t2 type: ref possible_keys: idx_table1RecordID key: idx_table1RecordID key_len: 5 ref: testDb.t1.recordID rows: 85 filtered: 100.00 Extra: Using index 7 rows in set, 1 warning (0.13 sec)
+----+-------------+-------+--------+-------------------------------------------------------------+--------------------+---------+--------------------------+-------+----------+---------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+--------+-------------------------------------------------------------+--------------------+---------+--------------------------+-------+----------+---------------------------------+ | 1 | SIMPLE | t7 | ALL | PRIMARY | NULL | NULL | NULL | 11627 | 100.00 | Using temporary; Using filesort | | 1 | SIMPLE | t4 | ref | idx_table7RecordID,idx_table3RecordID | idx_table7RecordID | 5 | testDb.t7.recordID | 1 | 100.00 | Using where | | 1 | SIMPLE | t3 | eq_ref | PRIMARY,table1RecordID_status,idx_status,idx_table1RecordID | PRIMARY | 4 | testDb.t4.table3RecordID | 1 | 100.00 | Using where | | 1 | SIMPLE | t5 | ref | compositeIDs | compositeIDs | 773 | const,testDb.t3.recordID | 5 | 100.00 | | | 1 | SIMPLE | t6 | eq_ref | PRIMARY | PRIMARY | 4 | testDb.t5.table6RecordID | 1 | 100.00 | | | 1 | SIMPLE | t1 | eq_ref | PRIMARY | PRIMARY | 4 | testDb.t3.table1RecordID | 1 | 100.00 | | | 1 | SIMPLE | t2 | ref | idx_table1RecordID | idx_table1RecordID | 5 | testDb.t1.recordID | 85 | 100.00 | Using index | +----+-------------+-------+--------+-------------------------------------------------------------+--------------------+---------+--------------------------+-------+----------+---------------------------------+
再次感谢高级!
删除选择子句,只需选择第一个表的recordID。 该查询仍然从上面产生相同的解释。
SELECT `t1`.`recordID` FROM `Table1` t1 LEFT JOIN `Table2` t2 ON `t1`.`recordID`=`t2`.`table1RecordID` LEFT JOIN `Table3` t3 ON `t3`.`table1RecordID`=`t1`.`recordID` LEFT JOIN `Table4` t4 ON `t4`.`table3RecordID`=`t3`.`recordID` AND `t4`.`type` = 1 LEFT JOIN `Table5` t5 ON `t5`.`objectID`=`t3`.`recordID` AND `t5`.`objectType`='Type2' LEFT JOIN `Table6` t6 ON `t6`.`recordID`=`t5`.`table6RecordID` JOIN `Table7` t7 ON `t7`.`recordID`=`t4`.`table7RecordID` WHERE `t3`.`status` IN ('3','4') GROUP BY `t3`.`recordID` ASC, `t1`.`recordID` ASC;
有关查询的一些评论:
where
子句撤消外部联接。 Table1
的两个字段进行分组。 recordID
就足够了。 in
比与or
进行一堆比较更具可读性。 这是结果查询:
SELECT SQL_NO_CACHE t1.`recordID`
FROM `Table1` t1 JOIN
`Table3` t3
ON t3.`table1RecordID` = t1.`recordID`
WHERE t3.`status` in ('3', '4') AND
t1.`groupName` = 'Sample Name'
GROUP BY t1.`recordID` ASC;
这会更好吗?
如果没有,让我们考虑一下查询在做什么。 它试图基于t3
从t1
过滤掉记录。 假设table1.RecordId
是唯一的,那么它将起作用:
SELECT SQL_NO_CACHE t1.`recordID`
FROM `Table1` t1
WHERE t1.`groupName` = 'Sample Name' and
exists (select 1
from Table3 t3
where t3.`status` in ('3', '4') and
t1.RecordId = t3.RecordId
);
为了获得最佳性能,您应该在Table3(RecordId, status)
上有一个索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.