MySQL 避免在内部查询中进行文件排序

发布于 2024-12-05 00:04:48 字数 1563 浏览 0 评论 0原文

我试图避免文件排序,但没有成功地将其从内部查询中删除。如果我将条件移至外部查询,那么它不会向我显示任何内容。

Create table articles (
   article_id Int UNSIGNED NOT NULL AUTO_INCREMENT,
   editor_id Int UNSIGNED NOT NULL,
   published_date Datetime,
Primary Key (book_id)) ENGINE = InnoDB;

Create Index published_date_INX ON articles (published_date);
Create Index editor_id_INX ON articles (editor_id);

EXPLAIN SELECT article_id, article_date FROM articles AA INNER JOIN 
    (
        SELECT article_id 
          FROM articles 
         WHERE editor_id=1 
         ORDER BY published_date DESC 
         LIMIT 100, 5
    ) ART USING (article_id);

+----+-------------+------------+--------+---------------+-----------+---------+----------------+--------+----------------+
| id | select_type | table      | type   | possible_keys | key       | key_len | ref            | rows   | Extra          |
+----+-------------+------------+--------+---------------+-----------+---------+----------------+--------+----------------+
|  1 |  | PRIMARY     | <derived2> | ALL    | NULL          | NULL      | NULL   NULL           |      5 |                | 
|  1 |  | PRIMARY     | AA         | eq_ref | PRIMARY       | PRIMARY   | 4      ART.article_id |      1 |                | 
|  2 |  | DERIVED     | articles   | ALL    | editor_id     | editor_id | 5                     | 114311 | Using filesort | 
+----+-------------+------------+--------+---------------+-----------+---------+----------------+--------+----------------+

3 rows in set (30.31 sec)

有什么建议如何从此查询中删除文件排序吗?

I am trying to avoid filesort but not getting luck in removing it from inner query. If I move condition to outer query then it shows me nothing.

Create table articles (
   article_id Int UNSIGNED NOT NULL AUTO_INCREMENT,
   editor_id Int UNSIGNED NOT NULL,
   published_date Datetime,
Primary Key (book_id)) ENGINE = InnoDB;

Create Index published_date_INX ON articles (published_date);
Create Index editor_id_INX ON articles (editor_id);

EXPLAIN SELECT article_id, article_date FROM articles AA INNER JOIN 
    (
        SELECT article_id 
          FROM articles 
         WHERE editor_id=1 
         ORDER BY published_date DESC 
         LIMIT 100, 5
    ) ART USING (article_id);

+----+-------------+------------+--------+---------------+-----------+---------+----------------+--------+----------------+
| id | select_type | table      | type   | possible_keys | key       | key_len | ref            | rows   | Extra          |
+----+-------------+------------+--------+---------------+-----------+---------+----------------+--------+----------------+
|  1 |  | PRIMARY     | <derived2> | ALL    | NULL          | NULL      | NULL   NULL           |      5 |                | 
|  1 |  | PRIMARY     | AA         | eq_ref | PRIMARY       | PRIMARY   | 4      ART.article_id |      1 |                | 
|  2 |  | DERIVED     | articles   | ALL    | editor_id     | editor_id | 5                     | 114311 | Using filesort | 
+----+-------------+------------+--------+---------------+-----------+---------+----------------+--------+----------------+

3 rows in set (30.31 sec)

Any suggestion how to remove filesort from this query?

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

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

发布评论

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

评论(1

姜生凉生 2024-12-12 00:04:48

也许您可以尝试在editor_id,published_date上添加索引。

create index edpub_INX on articles (editor_id, published_date);

关于您的内部查询:

SELECT article_id 
  FROM articles 
 WHERE editor_id=1 
 ORDER BY published_date DESC 
 LIMIT 100, 5

MySQL 的查询规划器可能认为按 editor_id 过滤(使用索引),然后按 published_date 排序比使用 published_date_INX 更好> 然后按 editor_id 进行过滤。查询规划器可能是正确的。

因此,如果您想“帮助”该特定查询,请在 editor_id,published_date 上创建索引,看看它是否可以帮助您的查询运行得更快。

Maybe you can try adding an index on editor_id, published_date.

create index edpub_INX on articles (editor_id, published_date);

On your inner query:

SELECT article_id 
  FROM articles 
 WHERE editor_id=1 
 ORDER BY published_date DESC 
 LIMIT 100, 5

MySQL's query planner is probably thinking that filtering by editor_id (using the index) and then ordering by published_date is better than using published_date_INX and then filtering by editor_id. And query planner is probably right.

So, if you want to "help" on that specific query, create an index on editor_id, published_date and see if it helps your query run faster.

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