1

我正在修改一个旧的 MySQL 数据库,结果证明它的设计不适合它存储的数据类型。我对 SQL 不是很熟悉,所以我使用 SHOW CREATE TABLE 来获取用于旧表('interaction_old')的 CREATE 语句并几乎完全复制它,唯一的变化是一些列名和数据类型,创建一个新表('interaction_new')。现在一些在旧表中使用索引的查询不再使用新表中的索引,我不知道为什么。

以下是两个表的索引:

mysql> SHOW KEYS FROM interaction_old;

+-----------------+------------+---------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+
| Table           | Non_unique | Key_name      | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-----------------+------------+---------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+
| interaction_old |          0 | PRIMARY       |            1 | interactionid | A         |   138996006 |     NULL | NULL   |      | BTREE      |         |
| interaction_old |          1 | Complex_pdbid |            1 | Complex_pdbid | A         |        1338 |     NULL | NULL   |      | BTREE      |         |
| interaction_old |          1 | Protein_id    |            1 | Protein_id    | A         |       13737 |     NULL | NULL   |      | BTREE      |         |
| interaction_old |          1 | RNA_id        |            1 | RNA_id        | A         |        2806 |     NULL | NULL   |      | BTREE      |         |
+-----------------+------------+---------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+

mysql> SHOW KEYS FROM interaction_new;

+-----------------+------------+------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+
| Table           | Non_unique | Key_name   | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-----------------+------------+------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+
| interaction_new |          0 | PRIMARY    |            1 | interactionid | A         |   152311144 |     NULL | NULL   |      | BTREE      |         |
| interaction_new |          1 | pdbid      |            1 | pdbid         | A         |        2924 |     NULL | NULL   |      | BTREE      |         |
| interaction_new |          1 | pchainname |            1 | pchainname    | A         |         472 |     NULL | NULL   |      | BTREE      |         |
| interaction_new |          1 | rchainname |            1 | rchainname    | A         |         487 |     NULL | NULL   |      | BTREE      |         |
+-----------------+------------+------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+

还有一个在两者之间表现不同的示例查询:

mysql> EXPLAIN SELECT DISTINCT Complex_pdbid FROM interaction_old;

+----+-------------+-----------------+-------+---------------+---------------+---------+------+------+--------------------------+
| id | select_type | table           | type  | possible_keys | key           | key_len | ref  | rows | Extra                    |
+----+-------------+-----------------+-------+---------------+---------------+---------+------+------+--------------------------+
|  1 | SIMPLE      | interaction_old | range | NULL          | Complex_pdbid | 6       | NULL | 1339 | Using index for group-by |
+----+-------------+-----------------+-------+---------------+---------------+---------+------+------+--------------------------+

mysql> EXPLAIN SELECT DISTINCT pdbid FROM interaction_new;

+----+-------------+-----------------+------+---------------+------+---------+------+-----------+-----------------+
| id | select_type | table           | type | possible_keys | key  | key_len | ref  | rows      | Extra           |
+----+-------------+-----------------+------+---------------+------+---------+------+-----------+-----------------+
|  1 | SIMPLE      | interaction_new | ALL  | NULL          | NULL | NULL    | NULL | 152311144 | Using temporary |
+----+-------------+-----------------+------+---------------+------+---------+------+-----------+-----------------+

如您所料,interaction_old 上的查询在几分之一秒内完成,而我让interaction_new 上的查询运行了大约 20 分钟,然后将其终止。interaction_old.Complex_pdbid 和interaction_new.pdbid 是相同的数据类型(并且存储几乎完全相同的数据)。USE INDEX 和/或 FORCE INDEX 似乎没有任何效果。是什么导致了不同的行为?

编辑:根据文档,第一个表使用松散的索引扫描来提高速度——但是,该页面中的任何内容都没有让我清楚为什么这在第二个表上不起作用。

4

0 回答 0