奇怪的MySQL性能行为
我的问题很长,所以我尽量简短。我试图在MySQL中优化一个查询,并编写了两个版本。这两个函数返回完全相同的结果(如行数、列数和内容)。但这两种方法在执行时间上有很大不同。奇怪的是,当我打印它们的奇怪的MySQL性能行为,mysql,performance,Mysql,Performance,我的问题很长,所以我尽量简短。我试图在MySQL中优化一个查询,并编写了两个版本。这两个函数返回完全相同的结果(如行数、列数和内容)。但这两种方法在执行时间上有很大不同。奇怪的是,当我打印它们的explain时,它们看起来几乎是一样的,甚至有人可能会说速度越慢,性能就越好!这是: 查询版本A: 平均执行时间:0.0846秒 SQL: 解释输出: +----+--------------------+-------+--------+-------------------------------
explain
时,它们看起来几乎是一样的,甚至有人可能会说速度越慢,性能就越好!这是:
查询版本A: 平均执行时间:0.0846秒 SQL:
解释
输出:
+----+--------------------+-------+--------+----------------------------------------------------------------+-------------------+---------+-----------------------------------------------+------+----------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------------+-------+--------+----------------------------------------------------------------+-------------------+---------+-----------------------------------------------+------+----------+----------------------------------------------+
| 1 | PRIMARY | a | ref | PRIMARY,inxName,inx_entity_id,fk_account_contact1_idx,inx_type | inx_type | 1 | const | 1361 | 100.00 | Using where; Using temporary; Using filesort |
| 1 | PRIMARY | mc | ref | inx_entity_id | inx_entity_id | 4 | db.a.contact_entity_id | 1 | 100.00 | Using index |
| 1 | PRIMARY | er | eq_ref | PRIMARY | PRIMARY | 9 | db.a.entity_id,db.a.account_id,const | 1 | 100.00 | Using index |
| 1 | PRIMARY | mc_er | eq_ref | PRIMARY | PRIMARY | 9 | db.mc.entity_id,db.mc.contact_id,const | 1 | 100.00 | Using index |
| 1 | PRIMARY | mc_cp | ref | Relationship1_idx | Relationship1_idx | 4 | db.mc.contact_id | 1 | 100.00 | NULL |
| 1 | PRIMARY | mc_ce | ref | Relationship1_idx | Relationship1_idx | 4 | db.mc.contact_id | 1 | 100.00 | NULL |
| 1 | PRIMARY | mc_cw | ref | Relationship1_idx | Relationship1_idx | 4 | db.mc.contact_id | 1 | 100.00 | NULL |
| 1 | PRIMARY | mc_ca | ref | Relationship1_idx | Relationship1_idx | 4 | db.mc.contact_id | 1 | 100.00 | NULL |
| 3 | DEPENDENT SUBQUERY | c | ref | PRIMARY,inx_entity_id | inx_entity_id | 4 | func | 1 | 100.00 | Using index |
| 3 | DEPENDENT SUBQUERY | er | eq_ref | PRIMARY | PRIMARY | 9 | func,db.c.contact_id,const | 1 | 100.00 | Using where; Using index |
| 3 | DEPENDENT SUBQUERY | cp | ref | Relationship1_idx | Relationship1_idx | 4 | db.c.contact_id | 1 | 100.00 | Using index |
| 3 | DEPENDENT SUBQUERY | ce | ref | Relationship1_idx | Relationship1_idx | 4 | db.c.contact_id | 1 | 100.00 | Using index |
| 3 | DEPENDENT SUBQUERY | cw | ref | Relationship1_idx | Relationship1_idx | 4 | db.c.contact_id | 1 | 100.00 | Using index |
| 2 | DEPENDENT SUBQUERY | ac | ref | PRIMARY,fk_account_contact1,fk_account_contact_account1_idx | PRIMARY | 4 | func | 1 | 100.00 | Using index |
| 2 | DEPENDENT SUBQUERY | c | ref | PRIMARY,inx_entity_id | inx_entity_id | 4 | db.ac.contact_entity_id | 1 | 100.00 | Using index |
| 2 | DEPENDENT SUBQUERY | er | eq_ref | PRIMARY | PRIMARY | 9 | db.ac.contact_entity_id,db.c.contact_id,const | 1 | 100.00 | Using index |
| 2 | DEPENDENT SUBQUERY | cp | ref | Relationship1_idx | Relationship1_idx | 4 | db.c.contact_id | 1 | 100.00 | Using index |
| 2 | DEPENDENT SUBQUERY | ce | ref | Relationship1_idx | Relationship1_idx | 4 | db.c.contact_id | 1 | 100.00 | Using index |
| 2 | DEPENDENT SUBQUERY | cw | ref | Relationship1_idx | Relationship1_idx | 4 | db.c.contact_id | 1 | 100.00 | Using index |
+----+--------------------+-------+--------+----------------------------------------------------------------+-------------------+---------+-----------------------------------------------+------+----------+----------------------------------------------+
19 rows in set, 1 warning (0.00 sec)
+----+-------------+--------+--------+----------------------------------------------------------------+-------------------+---------+------------------------------------------+------+----------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+--------+----------------------------------------------------------------+-------------------+---------+------------------------------------------+------+----------+----------------------------------------------+
| 1 | SIMPLE | a | ref | PRIMARY,inxName,inx_entity_id,fk_account_contact1_idx,inx_type | inx_type | 1 | const | 1361 | 100.00 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | mc | ref | inx_entity_id | inx_entity_id | 4 | db.a.contact_entity_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | er | eq_ref | PRIMARY | PRIMARY | 9 | db.a.entity_id,db.a.account_id,const | 1 | 100.00 | Using index |
| 1 | SIMPLE | mc_er | eq_ref | PRIMARY | PRIMARY | 9 | db.mc.entity_id,db.mc.contact_id,const | 1 | 100.00 | Using index |
| 1 | SIMPLE | mc_cp | ref | Relationship1_idx | Relationship1_idx | 4 | db.mc.contact_id | 1 | 100.00 | NULL |
| 1 | SIMPLE | mc_ce | ref | Relationship1_idx | Relationship1_idx | 4 | db.mc.contact_id | 1 | 100.00 | NULL |
| 1 | SIMPLE | mc_cw | ref | Relationship1_idx | Relationship1_idx | 4 | db.mc.contact_id | 1 | 100.00 | NULL |
| 1 | SIMPLE | mc_ca | ref | Relationship1_idx | Relationship1_idx | 4 | db.mc.contact_id | 1 | 100.00 | NULL |
| 1 | SIMPLE | smc | ref | inx_entity_id | inx_entity_id | 4 | db.a.contact_entity_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | smc_er | eq_ref | PRIMARY | PRIMARY | 9 | db.smc.entity_id,db.smc.contact_id,const | 1 | 100.00 | Using index |
| 1 | SIMPLE | smc_cp | ref | Relationship1_idx | Relationship1_idx | 4 | db.smc.contact_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | smc_ce | ref | Relationship1_idx | Relationship1_idx | 4 | db.smc.contact_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | smc_cw | ref | Relationship1_idx | Relationship1_idx | 4 | db.smc.contact_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | smc_ca | ref | Relationship1_idx | Relationship1_idx | 4 | db.smc.contact_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | sac | ref | PRIMARY,fk_account_contact_account1_idx | PRIMARY | 4 | db.a.account_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | sc | ref | inx_entity_id | inx_entity_id | 4 | db.sac.contact_entity_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | ser | eq_ref | PRIMARY | PRIMARY | 9 | db.sc.entity_id,db.sc.contact_id,const | 1 | 100.00 | Using index |
| 1 | SIMPLE | scp | ref | Relationship1_idx | Relationship1_idx | 4 | db.sc.contact_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | sce | ref | Relationship1_idx | Relationship1_idx | 4 | db.sc.contact_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | scw | ref | Relationship1_idx | Relationship1_idx | 4 | db.sc.contact_id | 1 | 100.00 | Using index |
+----+-------------+--------+--------+----------------------------------------------------------------+-------------------+---------+------------------------------------------+------+----------+----------------------------------------------+
20 rows in set, 1 warning (0.00 sec)
查询版本B: 平均执行时间:0.32078秒 SQL:
解释
输出:
+----+--------------------+-------+--------+----------------------------------------------------------------+-------------------+---------+-----------------------------------------------+------+----------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------------+-------+--------+----------------------------------------------------------------+-------------------+---------+-----------------------------------------------+------+----------+----------------------------------------------+
| 1 | PRIMARY | a | ref | PRIMARY,inxName,inx_entity_id,fk_account_contact1_idx,inx_type | inx_type | 1 | const | 1361 | 100.00 | Using where; Using temporary; Using filesort |
| 1 | PRIMARY | mc | ref | inx_entity_id | inx_entity_id | 4 | db.a.contact_entity_id | 1 | 100.00 | Using index |
| 1 | PRIMARY | er | eq_ref | PRIMARY | PRIMARY | 9 | db.a.entity_id,db.a.account_id,const | 1 | 100.00 | Using index |
| 1 | PRIMARY | mc_er | eq_ref | PRIMARY | PRIMARY | 9 | db.mc.entity_id,db.mc.contact_id,const | 1 | 100.00 | Using index |
| 1 | PRIMARY | mc_cp | ref | Relationship1_idx | Relationship1_idx | 4 | db.mc.contact_id | 1 | 100.00 | NULL |
| 1 | PRIMARY | mc_ce | ref | Relationship1_idx | Relationship1_idx | 4 | db.mc.contact_id | 1 | 100.00 | NULL |
| 1 | PRIMARY | mc_cw | ref | Relationship1_idx | Relationship1_idx | 4 | db.mc.contact_id | 1 | 100.00 | NULL |
| 1 | PRIMARY | mc_ca | ref | Relationship1_idx | Relationship1_idx | 4 | db.mc.contact_id | 1 | 100.00 | NULL |
| 3 | DEPENDENT SUBQUERY | c | ref | PRIMARY,inx_entity_id | inx_entity_id | 4 | func | 1 | 100.00 | Using index |
| 3 | DEPENDENT SUBQUERY | er | eq_ref | PRIMARY | PRIMARY | 9 | func,db.c.contact_id,const | 1 | 100.00 | Using where; Using index |
| 3 | DEPENDENT SUBQUERY | cp | ref | Relationship1_idx | Relationship1_idx | 4 | db.c.contact_id | 1 | 100.00 | Using index |
| 3 | DEPENDENT SUBQUERY | ce | ref | Relationship1_idx | Relationship1_idx | 4 | db.c.contact_id | 1 | 100.00 | Using index |
| 3 | DEPENDENT SUBQUERY | cw | ref | Relationship1_idx | Relationship1_idx | 4 | db.c.contact_id | 1 | 100.00 | Using index |
| 2 | DEPENDENT SUBQUERY | ac | ref | PRIMARY,fk_account_contact1,fk_account_contact_account1_idx | PRIMARY | 4 | func | 1 | 100.00 | Using index |
| 2 | DEPENDENT SUBQUERY | c | ref | PRIMARY,inx_entity_id | inx_entity_id | 4 | db.ac.contact_entity_id | 1 | 100.00 | Using index |
| 2 | DEPENDENT SUBQUERY | er | eq_ref | PRIMARY | PRIMARY | 9 | db.ac.contact_entity_id,db.c.contact_id,const | 1 | 100.00 | Using index |
| 2 | DEPENDENT SUBQUERY | cp | ref | Relationship1_idx | Relationship1_idx | 4 | db.c.contact_id | 1 | 100.00 | Using index |
| 2 | DEPENDENT SUBQUERY | ce | ref | Relationship1_idx | Relationship1_idx | 4 | db.c.contact_id | 1 | 100.00 | Using index |
| 2 | DEPENDENT SUBQUERY | cw | ref | Relationship1_idx | Relationship1_idx | 4 | db.c.contact_id | 1 | 100.00 | Using index |
+----+--------------------+-------+--------+----------------------------------------------------------------+-------------------+---------+-----------------------------------------------+------+----------+----------------------------------------------+
19 rows in set, 1 warning (0.00 sec)
+----+-------------+--------+--------+----------------------------------------------------------------+-------------------+---------+------------------------------------------+------+----------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+--------+----------------------------------------------------------------+-------------------+---------+------------------------------------------+------+----------+----------------------------------------------+
| 1 | SIMPLE | a | ref | PRIMARY,inxName,inx_entity_id,fk_account_contact1_idx,inx_type | inx_type | 1 | const | 1361 | 100.00 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | mc | ref | inx_entity_id | inx_entity_id | 4 | db.a.contact_entity_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | er | eq_ref | PRIMARY | PRIMARY | 9 | db.a.entity_id,db.a.account_id,const | 1 | 100.00 | Using index |
| 1 | SIMPLE | mc_er | eq_ref | PRIMARY | PRIMARY | 9 | db.mc.entity_id,db.mc.contact_id,const | 1 | 100.00 | Using index |
| 1 | SIMPLE | mc_cp | ref | Relationship1_idx | Relationship1_idx | 4 | db.mc.contact_id | 1 | 100.00 | NULL |
| 1 | SIMPLE | mc_ce | ref | Relationship1_idx | Relationship1_idx | 4 | db.mc.contact_id | 1 | 100.00 | NULL |
| 1 | SIMPLE | mc_cw | ref | Relationship1_idx | Relationship1_idx | 4 | db.mc.contact_id | 1 | 100.00 | NULL |
| 1 | SIMPLE | mc_ca | ref | Relationship1_idx | Relationship1_idx | 4 | db.mc.contact_id | 1 | 100.00 | NULL |
| 1 | SIMPLE | smc | ref | inx_entity_id | inx_entity_id | 4 | db.a.contact_entity_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | smc_er | eq_ref | PRIMARY | PRIMARY | 9 | db.smc.entity_id,db.smc.contact_id,const | 1 | 100.00 | Using index |
| 1 | SIMPLE | smc_cp | ref | Relationship1_idx | Relationship1_idx | 4 | db.smc.contact_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | smc_ce | ref | Relationship1_idx | Relationship1_idx | 4 | db.smc.contact_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | smc_cw | ref | Relationship1_idx | Relationship1_idx | 4 | db.smc.contact_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | smc_ca | ref | Relationship1_idx | Relationship1_idx | 4 | db.smc.contact_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | sac | ref | PRIMARY,fk_account_contact_account1_idx | PRIMARY | 4 | db.a.account_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | sc | ref | inx_entity_id | inx_entity_id | 4 | db.sac.contact_entity_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | ser | eq_ref | PRIMARY | PRIMARY | 9 | db.sc.entity_id,db.sc.contact_id,const | 1 | 100.00 | Using index |
| 1 | SIMPLE | scp | ref | Relationship1_idx | Relationship1_idx | 4 | db.sc.contact_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | sce | ref | Relationship1_idx | Relationship1_idx | 4 | db.sc.contact_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | scw | ref | Relationship1_idx | Relationship1_idx | 4 | db.sc.contact_id | 1 | 100.00 | Using index |
+----+-------------+--------+--------+----------------------------------------------------------------+-------------------+---------+------------------------------------------+------+----------+----------------------------------------------+
20 rows in set, 1 warning (0.00 sec)
这不是很奇怪吗?我遗漏了什么吗?更多的联接意味着更长的查询执行时间,特别是在左侧外部联接的右侧有很多空行的情况下。此外,除非我弄错了,否则第一个查询的where子句中的非相关子查询将被计算一次,结果将被缓存,因此您在性能方面不会受到太大的影响。它不应该以某种方式显示在
解释中吗?您的两个查询都有许多缺陷。首先,即使MySQL允许您将聚合运算符与未按分组的列混合,您也不应该这样做。为了避免这种情况,可以隔离需要group by的查询,并将其与语句的其余部分联接。即使在第一次查询中,您似乎也有许多不必要的联接,特别是在
子句中的。@plalx感谢您的输入。根据我的经验,将连接移动到子查询(在from部分)应该是最后的选择,因为MySQL不包含子查询结果的索引,因此具有返回大量行的子查询将导致性能低下。然而,这不是我的问题。我只是很惊讶为什么在解释中没有显示两者的性能差异!或者至少我看不到它。@plalx顺便说一句,所有的连接都不是不必要的。我刚刚简化了SQL
s来压缩这个问题。连接在那里,因此我可以使用where
子句中的表。我只是没有提到这里的条件,因为它们对所问的问题没有影响。但是谢谢你的精确。