gpt4 book ai didi

MySQL 不使用 ORDER BY 的索引

转载 作者:IT老高 更新时间:2023-10-29 00:11:35 25 4
gpt4 key购买 nike

我有一个名为“test”的简单 MySQL 表,有两列:

  1. 名为“id”的自动递增 int 列
  2. 名为“textcol”的 Varchar(3000) 列

我根据“textcol”列在表中创建索引。但是,ORDER BY 查询似乎没有使用索引,即在 textcol 上使用 ORDER BY 的简单查询上的 EXPLAIN 语句在其输出的 Key 列中显示 NULL,并且还使用文件排序。

任何用于进行更改以帮助使用 ORDER by 查询的索引的指针都会对我有用。

“mysql --version”命令给出的MySQL版本:

mysql Ver 14.14 Distrib 5.1.58,适用于使用 readline 6.2 的 debian-linux-gnu (x86_64)

mysql> CREATE TABLE test (id INTEGER NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), textcol VARCHAR(3000));
Query OK, 0 rows affected (0.05 sec)

mysql> DESCRIBE test;
+---------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| textcol | varchar(3000) | YES | | NULL | |
+---------+---------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> CREATE INDEX textcolindex ON test (textcol);
Query OK, 0 rows affected, 2 warnings (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> SHOW INDEX FROM test;
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| test | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | |
| test | 1 | textcolindex | 1 | textcol | A | NULL | 1000 | NULL | YES | BTREE | |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
2 rows in set (0.00 sec)

mysql> INSERT INTO test (textcol) VALUES ('test1');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO test (textcol) VALUES ('test2');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO test (textcol) VALUES ('test3');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO test (textcol) VALUES ('test4');
Query OK, 1 row affected (0.00 sec)


mysql> EXPLAIN SELECT * FROM test ORDER BY textcol;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| 1 | SIMPLE | test | ALL | NULL | NULL | NULL | NULL | 4 | Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)

mysql> EXPLAIN SELECT * FROM test ORDER BY id;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| 1 | SIMPLE | test | ALL | NULL | NULL | NULL | NULL | 4 | Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)

最佳答案

因为它必须加载整个表来回答查询并且对 4 个元素进行排序很便宜,查询优化器可能只是避免触及索引。更大的 table 还会发生这种情况吗?

请注意,varchar(3000) 列不能是覆盖索引,因为 MySQL 不会在索引中包含超过前 768 个左右字节的 varchar。

如果您希望查询只读取索引,则索引必须包含您要SELECT的每一列。在 innodb 上,一旦你使 textcol 足够小,它应该开始为你的两列表工作;在 MyISAM 上,您需要自己包含主键列,例如 CREATE INDEX textcolindex ON test (textcol,id);

关于MySQL 不使用 ORDER BY 的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9641463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com