[英]Why does the same exact query produce 2 different MySQL explain results?
I have a simple SELECT * From tv Where Client = 'ABCD'
query and when I do an EXPLAIN EXTENDED
, it gives me two different results. 我有一个简单的
SELECT * From tv Where Client = 'ABCD'
查询,当我执行EXPLAIN EXTENDED
,它给我两个不同的结果。 When executing the query, one of them take a few milliseconds, while the other takes about 3 seconds. 执行查询时,其中一个花费几毫秒,而另一个花费约3秒。 Why would it give two different explain results and what is causing the slowness?
为什么会给出两种不同的解释结果,是什么导致速度缓慢?
Slow Query: 慢查询:
Fast Query: 快速查询:
Q Why does the same exact query produce 2 different MySQL explain results? 问:为什么相同的确切查询会产生两个不同的MySQL解释结果?
A Because something is different. A因为有些不同。 If not in the query, then between the two tables, or database instances.
如果不在查询中,则在两个表或数据库实例之间。
All of these should be reviewed, to find the difference: 所有这些都应该进行审查,以找出不同之处:
SHOW VARIABLES LIKE '%version%'
) SHOW VARIABLES LIKE '%version%'
) SHOW [GLOBAL] VARIABLES LIKE 'character_set%'
) SHOW [GLOBAL] VARIABLES LIKE 'character_set%'
) SHOW CREATE TABLE
) SHOW CREATE TABLE
) SHOW CREATE TABLE
) SHOW CREATE TABLE
) SHOW CREATE TABLE
) SHOW CREATE TABLE
) I solved by updating table statistics. 我通过更新表统计信息解决了。
On MySQL i did: 在MySQL上,我做到了:
OPTIMIZE TABLE [tablename]
Well the estimated number of row are also different. 那么估计的行数也不同。
So MySQL uses table statistics to determine which indexes to us and how to use them. 因此,MySQL使用表统计信息来确定对我们有哪些索引以及如何使用它们。 Since the tables appears to have a different amount of rows in it it is only reasonable that the query plans would differ as the statistics will be different.
由于表中的行数似乎不同,因此合理的查询计划是不同的,因为统计信息也将有所不同。
Update: 更新:
I did not read the row column correctly. 我没有正确读取行列。 Thus I assumed there is a huge difference in rows.
因此,我认为行之间存在巨大差异。 This is not the case.
不是这种情况。 Seems like the statistics might be out of date on the table with the slow query.
似乎统计信息在查询缓慢的表上可能已过时。 Please run a OPTIMIZE TABLE statement on the slow query table.
请在慢速查询表上运行OPTIMIZE TABLE语句。 This will essentially rebuild the table.
这实际上将重建表。
the slow query shows the type as ref
while the fast query shows the type as range
. 慢速查询将类型显示为
ref
而快速查询将类型显示为range
。 I suspect that you are missing an index on your Client row on the slow table. 我怀疑您在慢速表的“客户”行上缺少索引。
I can confirm this kind of behavior. 我可以确认这种行为。 Just spent whole day to get it.
刚花了一整天就可以得到它。 Sometimes, when you are not using statements PRIMARY = PRIMARY (eg. using just part of the composite primary key), Mysql (resp. MariaDB) is executing MUCH faster queries on DB with a lot of data (production DB), instead of DB with just sample data (production env.)
有时,当你不使用语句PRIMARY = PRIMARY(例如使用复合主键的只是一部分)时,MySQL(相应地,MariaDB的)正在执行对DB 快得多查询了大量的数据(生产DB),而不是DB仅包含样本数据(生产环境)
My solution was to copy part of prod. 我的解决方案是复制产品的一部分。 data to dev database - it made some queries executed with different strategy, and of course, faster.
数据到开发数据库-它使某些查询以不同的策略执行,当然更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.