在查询中添加 LIMIT 会使查询时间增加 1000% 以上

发布于 2025-01-02 00:01:07 字数 2807 浏览 1 评论 0原文

我正在运行以下查询:

SELECT 
        MyField,
        COUNT(*) AS MyCount
    FROM
        MyTable
    NATURAL JOIN
        AnotherTable
    WHERE
        Timestamp >= 1000 AND Timestamp <= 10000
    GROUP BY
        MyField
    ORDER BY
        MyCount DESC;

运行良好,大约需要 6 秒才能完成。如果我想将结果限制为仅显示最高的 20 个 MyCount,我会在查询末尾添加 LIMIT 20。突然需要6分钟才能完成!

原始查询的 EXPLAIN 输出:

+----+-------------+-------------+--------+---------------------------+---------+---------+---------------------------+---------+----------------------------------------------+
| id | select_type | table       | type   | possible_keys             | key     | key_len | ref                       | rows    | Extra                                        |
+----+-------------+-------------+--------+---------------------------+---------+---------+---------------------------+---------+----------------------------------------------+
|  1 | SIMPLE      | MyTable     | ALL    | mytable_fkey              | NULL    | NULL    | NULL                      | 6858209 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | AnotherTable| eq_ref | PRIMARY                   | PRIMARY | 4       | test.MyTable.FKeyID       |       1 | Using index                                  |
+----+-------------+-------------+--------+---------------------------+---------+---------+---------------------------+---------+----------------------------------------------+

带有 LIMIT 20 的查询的 EXPLAIN 输出:

+----+-------------+-------------+--------+---------------------------+-------------------------+---------+---------------------------+---------+----------------------------------------------+
| id | select_type | table       | type   | possible_keys             | key                     | key_len | ref                       | rows    | Extra                                        |
+----+-------------+-------------+--------+---------------------------+-------------------------+---------+---------------------------+---------+----------------------------------------------+
|  1 | SIMPLE      | MyTable     | index  | mytable_fkey              | myfield_timestamp_index | 771     | NULL                      | 6858209 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | AnotherTable| eq_ref | PRIMARY                   | PRIMARY                 | 4       | test.MyTable.FKeyID       |       1 | Using index                                  |
+----+-------------+-------------+--------+---------------------------+-------------------------+---------+---------------------------+---------+----------------------------------------------+

对此有何解释?有没有更好的方法可以限制行数?

I am running the following query:

SELECT 
        MyField,
        COUNT(*) AS MyCount
    FROM
        MyTable
    NATURAL JOIN
        AnotherTable
    WHERE
        Timestamp >= 1000 AND Timestamp <= 10000
    GROUP BY
        MyField
    ORDER BY
        MyCount DESC;

This runs fine and takes about 6 seconds to complete. If I want to limit the result to show only the 20 highest MyCounts, I add LIMIT 20 on to the end of the query. Suddenly it takes 6 minutes to complete!

The EXPLAIN output for the original query:

+----+-------------+-------------+--------+---------------------------+---------+---------+---------------------------+---------+----------------------------------------------+
| id | select_type | table       | type   | possible_keys             | key     | key_len | ref                       | rows    | Extra                                        |
+----+-------------+-------------+--------+---------------------------+---------+---------+---------------------------+---------+----------------------------------------------+
|  1 | SIMPLE      | MyTable     | ALL    | mytable_fkey              | NULL    | NULL    | NULL                      | 6858209 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | AnotherTable| eq_ref | PRIMARY                   | PRIMARY | 4       | test.MyTable.FKeyID       |       1 | Using index                                  |
+----+-------------+-------------+--------+---------------------------+---------+---------+---------------------------+---------+----------------------------------------------+

The EXPLAIN output for the query with LIMIT 20:

+----+-------------+-------------+--------+---------------------------+-------------------------+---------+---------------------------+---------+----------------------------------------------+
| id | select_type | table       | type   | possible_keys             | key                     | key_len | ref                       | rows    | Extra                                        |
+----+-------------+-------------+--------+---------------------------+-------------------------+---------+---------------------------+---------+----------------------------------------------+
|  1 | SIMPLE      | MyTable     | index  | mytable_fkey              | myfield_timestamp_index | 771     | NULL                      | 6858209 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | AnotherTable| eq_ref | PRIMARY                   | PRIMARY                 | 4       | test.MyTable.FKeyID       |       1 | Using index                                  |
+----+-------------+-------------+--------+---------------------------+-------------------------+---------+---------------------------+---------+----------------------------------------------+

What is the explanation for this? Is there a better way I can limit the number of rows?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

深居我梦 2025-01-09 00:01:07

如果您看到 Usingtemporary;在 EXPLAIN 输出中使用 filesort ,您可能会缺少合适的索引,并且因此而被杀死。

确保您的 JOINGROUP BY 字段在同一索引中均可用。

If you see Using temporary; Using filesort in your EXPLAIN output, you are probably missing a suitable index and you're getting killed because of it.

Make sure your JOIN and GROUP BY fields are both available in the same index.

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