简体   繁体   English

为什么相同的确切查询会产生2个不同的MySQL解释结果?

[英]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: 所有这些都应该进行审查,以找出不同之处:

  • Are they running on the same version of MySQL ( SHOW VARIABLES LIKE '%version%' ) 它们是否在相同版本的MySQL上运行(如SHOW VARIABLES LIKE '%version%'
  • Are the instances running the same characterset ( SHOW [GLOBAL] VARIABLES LIKE 'character_set%' ) 实例是否运行相同的字符集( SHOW [GLOBAL] VARIABLES LIKE 'character_set%'
  • Are the table columns using the same characterset ( SHOW CREATE TABLE ) 表列是否使用相同的字符集( SHOW CREATE TABLE
  • Are both tables using the same storage engine? 两个表是否使用相同的存储引擎? ( SHOW CREATE TABLE ) SHOW CREATE TABLE
  • If the primary key is a composite key, are the columns in the same order ( SHOW CREATE TABLE ) 如果主键是复合键,则列的顺序是否相同( SHOW CREATE TABLE
  • Are statistics up to date and accurate? 统计信息是否最新且准确?
  • Is one of the tables fragmented, due to a lot of insert,update,delete activity? 由于插入,更新,删除活动很多,其中一张表是零散的吗?
  • Is the MyISAM key cache or the InnoDB buffers the same size on both servers? 两台服务器上的MyISAM密钥缓存或InnoDB缓冲区是否相同?

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.

相关问题 mysql解释不同服务器上的不同结果,同一查询,同一个数据库 - mysql explain different results on different servers, same query, same db 为什么mysql在同一查询的2个不同服务器上显示2个不同结果 - Why does mysql show 2 different results on 2 different servers for the same query 完全相同的MYSQL查询,但在不同服务器上的结果不同 - Exact same MYSQL query but different results on different servers 完全相同的 MYSQL 查询,一致地返回两个不同的结果 - Exact same MYSQL Query, returns two different results consistently 为什么 MySQL View 和同一个 View 的底层 SELECT 查询返回不同的结果? - Why does MySQL View and the same View's underlying SELECT query return different results? MySQL:相同的查询,不同的结果 - Mysql: same query, different results 同一个 MySQL 查询的不同结果 - Different results on same MySQL query mysql.connector 和常规 MySQL 中的精确查询没有返回相同的结果 - Exact query in mysql.connector and regular MySQL not returning same results 相同的数据库结构和索引,但查询速度和解释结果不同 - same database structure and index, but different query speed and explain results 为什么相同的查询在从 MySQL 服务器上的解释和执行如此不同? - Why does the same query explain & perform so differently on a slave MySQL server than a master?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM