mysql解释不同服务器上的不同结果,相同的查询,相同的数据库
经过大量工作,我终于得到了一个相当复杂的查询,可以非常顺利地工作并很快返回结果。
它在开发和测试方面都运行良好,但现在测试速度明显减慢。 解释查询在开发中需要 0.06 秒,在测试中几乎相同,现在在测试中需要 7 秒。
解释略有不同,我不确定为什么会这样 的解释
-+---------+------------------------------+------+------------------------------ ---+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+-------------------------+------------ -+---------+------------------------------+------+------------------------------ ---+ | 1 | PRIMARY | | ALL | NULL | NULL | NULL | NULL | 5 | | | 1 | PRIMARY | tickets | ref | biddate_idx | biddate_idx | 7 | showsdate.bid,showsdate.date | 78 | | | 2 | DERIVED | shows | ALL | biddate_idx,latlong_idx | NULL | NULL | NULL | 3089 | Using temporary; Using fileso rt | | 2 | DERIVED | genres | ref | bandid_idx | bandid_idx | 4 | activehw.shows.bid | 2 | Using index | | 2 | DERIVED | artists | eq_ref | bid_idx | bid_idx | 4 | activehw.genres.bid | 1 | Using where | +----+-------------+------------+--------+-------------------------+------------
开发人员和测试人员
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+-------------------------+-------------+---------+------------------------------+--------+----------------------------------------------+ | 1 | PRIMARY | | ALL | NULL | NULL | NULL | NULL | 5 | | | 1 | PRIMARY | tickets | ref | biddate_idx | biddate_idx | 7 | showsdate.bid,showsdate.date | 78 | | | 2 | DERIVED | genres | index | bandid_idx | bandid_idx | 139 | NULL | 531281 | Using index; Using temporary; Using filesort | | 2 | DERIVED | artists | eq_ref | bid_idx | bid_idx | 4 | activeHW.genres.bid | 1 | | | 2 | DERIVED | shows | eq_ref | biddate_idx,latlong_idx | biddate_idx | 7 | activeHW.artists.bid | 1 | Using where | +----+-------------+------------+--------+-------------------------+-------------+---------+------------------------------+--------+----------------------------------------------+ 5 rows in set (6.99 sec)
尽管查询完全相同,但表的顺序不同。 这是导致速度放缓的原因吗? 如果是这样,我该如何解决? 开发是windows,测试是centOs。 两者都运行相同版本的 mysql 5.0,正如我所说,测试运行完美,我没有对数据库进行任何结构性更改。
我运行了 mysqlcheck,所有表都恢复正常。
After much work I finally got a rather complicated query to work very smootly and return results very quickly.
It was running well on both dev and testing, but now testing has slowed considerably.
The explain query which takes 0.06 second on dev and was about the same in testing is now 7 seconds in testing.
The explains are slightly different, and I'm not sure why this would be
The explain from dev
-+---------+------------------------------+------+------------------------------ ---+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+-------------------------+------------ -+---------+------------------------------+------+------------------------------ ---+ | 1 | PRIMARY | | ALL | NULL | NULL | NULL | NULL | 5 | | | 1 | PRIMARY | tickets | ref | biddate_idx | biddate_idx | 7 | showsdate.bid,showsdate.date | 78 | | | 2 | DERIVED | shows | ALL | biddate_idx,latlong_idx | NULL | NULL | NULL | 3089 | Using temporary; Using fileso rt | | 2 | DERIVED | genres | ref | bandid_idx | bandid_idx | 4 | activehw.shows.bid | 2 | Using index | | 2 | DERIVED | artists | eq_ref | bid_idx | bid_idx | 4 | activehw.genres.bid | 1 | Using where | +----+-------------+------------+--------+-------------------------+------------
and in the testing
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+-------------------------+-------------+---------+------------------------------+--------+----------------------------------------------+ | 1 | PRIMARY | | ALL | NULL | NULL | NULL | NULL | 5 | | | 1 | PRIMARY | tickets | ref | biddate_idx | biddate_idx | 7 | showsdate.bid,showsdate.date | 78 | | | 2 | DERIVED | genres | index | bandid_idx | bandid_idx | 139 | NULL | 531281 | Using index; Using temporary; Using filesort | | 2 | DERIVED | artists | eq_ref | bid_idx | bid_idx | 4 | activeHW.genres.bid | 1 | | | 2 | DERIVED | shows | eq_ref | biddate_idx,latlong_idx | biddate_idx | 7 | activeHW.artists.bid | 1 | Using where | +----+-------------+------------+--------+-------------------------+-------------+---------+------------------------------+--------+----------------------------------------------+ 5 rows in set (6.99 sec)
The order of the tables is different, even though the queries are exactly the same.
Is this what would cause the slowdown? if so, how would I fix it?
The dev is windows, testing is centOs.
both running same version of mysql 5.0, and like I said, testing was running perfectly and I haven't made any structural changes to the database.
I ran mysqlcheck and all tables came back ok.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
MySQL 会查看表中的数据以及查询本身来决定使用哪个执行计划。
如果两个数据库中的数据相同,我建议对查询中的所有表使用 ANALYZE 或 OPTIMIZE。
MySQL looks at the data in the tables as well as the query itself to decide which execution plan to use.
If the data is the same in both databases, I'd suggest using ANALYZE or OPTIMIZE on all the tables in your query.
第一个计划不在
shows
上使用索引。如果您确定该索引会对您有帮助,请强制执行:
同时,收集表的统计信息。
The first plan doesn't use index on
shows
.If you are sure this index will help you, force it:
Meanwhile, collect statistics for your tables.
我会尝试重新生成统计信息并重建所有表的索引,看看这是否可以解决您的问题 - 这很可能就是计划不同的原因。
还有很多其他原因(内存、磁盘、操作系统差异、其他负载等),但我假设这些可能不是问题,因为您之前提到过它运行良好。
I would try regenerating statistics and rebuilding the indexes for all the tables and see if that fixes your problem - it's likely that is why the plans would be different.
There are lots of other things it could be (memory, disk, os differences, other loads, etc) but I'm assuming those probably aren't the issue since you mentioned that it ran fine before.
您确定这些来自同一个查询吗? 解释不仅略有不同,它们之间还存在相当大的差异:
Are you sure these are from the same query? The explains aren't just slightly different, there are considerable differences between them:
我们刚刚遇到了一个非常类似的问题,新构建的主服务器需要几分钟才能执行旧主服务器(功率较小)在不到一秒内完成的相同查询。 我们在查询中的两个 myisam 表上快速运行了 Repair table,现在新的 master 执行查询的速度至少与旧的一样快。
谢谢!
We just experienced a very similar problem with a newly built master taking several minutes to execute the same query that old master (with less power) completed in a fraction of a second. We ran repair table quick on two of the myisam tables in the query and now the new master executes the query at least as fast as the old one.
Thanks!