mysql解释不同服务器上的不同结果,相同的查询,相同的数据库

发布于 2024-07-14 09:40:00 字数 3019 浏览 7 评论 0原文

经过大量工作,我终于得到了一个相当复杂的查询,可以非常顺利地工作并很快返回结果。

它在开发和测试方面都运行良好,但现在测试速度明显减慢。 解释查询在开发中需要 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

¢好甜 2024-07-21 09:40:01

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.

有木有妳兜一样 2024-07-21 09:40:01

第一个计划不在 shows 上使用索引。

如果您确定该索引会对您有帮助,请强制执行:

SELECT ...
FROM ..., shows FORCE INDEX (biddate_idx) , ...
WHERE ...

同时,收集表的统计信息。

The first plan doesn't use index on shows.

If you are sure this index will help you, force it:

SELECT ...
FROM ..., shows FORCE INDEX (biddate_idx) , ...
WHERE ...

Meanwhile, collect statistics for your tables.

夏日浅笑〃 2024-07-21 09:40:01

我会尝试重新生成统计信息并重建所有表的索引,看看这是否可以解决您的问题 - 这很可能就是计划不同的原因。

还有很多其他原因(内存、磁盘、操作系统差异、其他负载等),但我假设这些可能不是问题,因为您之前提到过它运行良好。

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.

奢望 2024-07-21 09:40:01

您确定这些来自同一个查询吗? 解释不仅略有不同,它们之间还存在相当大的差异:

  1. WHERE 子句命中不同的表(开发中的艺术家,测试中的表演)
  2. 它在流派中命中的行数不同(开发中为 2,测试中为 531281) )。
  3. 第一个和第二个之间的其他杂项差异解释(主要是 EXTRA 中的内容)。

Are you sure these are from the same query? The explains aren't just slightly different, there are considerable differences between them:

  1. The WHERE clause is hitting different tables (artists on dev, shows on testing)
  2. The number of rows it's hitting in genres is different (2 on dev, 531281 on testing).
  3. Other miscellaneous differences between the first and second explains (stuff in EXTRA mainly).
森林迷了鹿 2024-07-21 09:40:01

我们刚刚遇到了一个非常类似的问题,新构建的主服务器需要几分钟才能执行旧主服务器(功率较小)在不到一秒内完成的相同查询。 我们在查询中的两个 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文