MYSQL 在简单查询中 ORDER BY 索引列时使用文件排序

发布于 2024-12-03 00:55:29 字数 2312 浏览 0 评论 0原文

我见过很多类似的问题,但我没有看到适合我的解决方案。或者也许我只是太笨了。 :) 希望有人能帮助我。

我有下表:

CREATE TABLE `table_name` (
  `value1` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `value2` varchar(50) NOT NULL,
  `value3` tinytext,
  `value4` tinytext,
  `value5` tinytext,
  `value6` char(3) DEFAULT NULL,
  `value7` char(3) DEFAULT NULL,
  PRIMARY KEY (`value1`),
  KEY value2_index ('value2')
) ENGINE=MyISAM AUTO_INCREMENT=46 DEFAULT CHARSET=latin1;

为了验证我的索引是否已设置,这是 SHOW INDEX 结果:

+--------------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table        | Non_unique | Key_name       | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| table_name   |          0 | PRIMARY        |            1 | value1      | A         |          43 |     NULL | NULL   |      | BTREE      |         |
| table_name   |          1 | value2_index   |            1 | value2      | A         |        NULL |     NULL | NULL   | YES  | BTREE      |         |
+--------------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
2 rows in set (0.00 sec)

我运行此查询的位置:

SELECT value2, value6
FROM table_name
WHERE value7 = 'Yes'
ORDER BY value2;

我认为通过在 value2 上添加索引,它将阻止查询使用文件排序。然而,解释显示不同:

+----+-------------+--------------+------+---------------+------+---------+------+------+-----------------------------+
| id | select_type | table        | type | possible_keys | key  | key_len | ref  | rows | Extra                       |
+----+-------------+--------------+------+---------------+------+---------+------+------+-----------------------------+
|  1 | SIMPLE      | table_name   | ALL  | NULL          | NULL | NULL    | NULL |   43 | Using where; Using filesort |
+----+-------------+--------------+------+---------------+------+---------+------+------+-----------------------------+
1 row in set (0.00 sec)

我做错了什么?

谢谢!

I've seen a lot of similar questions, but I'm not seeing a solution that will work for me. Or maybe I'm just being dense. :) Hopefully someone can help me out.

I have the following table:

CREATE TABLE `table_name` (
  `value1` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `value2` varchar(50) NOT NULL,
  `value3` tinytext,
  `value4` tinytext,
  `value5` tinytext,
  `value6` char(3) DEFAULT NULL,
  `value7` char(3) DEFAULT NULL,
  PRIMARY KEY (`value1`),
  KEY value2_index ('value2')
) ENGINE=MyISAM AUTO_INCREMENT=46 DEFAULT CHARSET=latin1;

To verify my indexes were set, this is the SHOW INDEX result:

+--------------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table        | Non_unique | Key_name       | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| table_name   |          0 | PRIMARY        |            1 | value1      | A         |          43 |     NULL | NULL   |      | BTREE      |         |
| table_name   |          1 | value2_index   |            1 | value2      | A         |        NULL |     NULL | NULL   | YES  | BTREE      |         |
+--------------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
2 rows in set (0.00 sec)

Where I'm running this query:

SELECT value2, value6
FROM table_name
WHERE value7 = 'Yes'
ORDER BY value2;

I thought by adding an index on value2, it would stop the query from using filesort. However, the EXPLAIN shows differently:

+----+-------------+--------------+------+---------------+------+---------+------+------+-----------------------------+
| id | select_type | table        | type | possible_keys | key  | key_len | ref  | rows | Extra                       |
+----+-------------+--------------+------+---------------+------+---------+------+------+-----------------------------+
|  1 | SIMPLE      | table_name   | ALL  | NULL          | NULL | NULL    | NULL |   43 | Using where; Using filesort |
+----+-------------+--------------+------+---------------+------+---------+------+------+-----------------------------+
1 row in set (0.00 sec)

What am I doing wrong?

Thanks!

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

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

发布评论

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

评论(1

七婞 2024-12-10 00:55:29

该查询首先需要查找 value7 = 'Yes' 的行,这意味着您的索引必须包含 value7 作为要使用的第一列。对于那些匹配的行,需要按 value2 排序。因此该查询需要 (value7, value2) 上的多列索引。

您可以在MySQL 文档中阅读有关索引的更多信息。

The query first needs to find rows with value7 = 'Yes', meaning your index must include value7 as the first column, to be used. For those rows that match, it needs to order by value2. So that query needs a multi-column index on (value7, value2).

You can read more about indexes in the MySQL docs.

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