我有一个超过 900 万行的非常大的表,在我的软件中,我需要在循环中快速对其进行选择查询。问题是查询需要将近 4 秒才能完成。这是其中一个查询(它们都是相似的,因为它们都有相同的 WHERE 子句,这会减慢它们的速度:
SELECT MIN(id)
FROM `04c1Tg0M`
WHERE `tried` = 0;
我将尝试过的列用作布尔值。该值为 1 或 0。这是该查询的 EXPLAIN:
--------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------+---------------+------+---------+------+---------+--------------------------+
| 1 | SIMPLE | 04c1Tg0M | index | NULL | pdex | 158 | NULL | 9275107 | Using where; Using index |
+----+-------------+----------+-------+---------------+------+---------+------+---------+--------------------------+
这是表结构:
CREATE TABLE `04c1Tg0M` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`tried` tinyint(1) DEFAULT '0',
PRIMARY KEY (`id`),
KEY `pdex` (`username`,`id`,`tried`)
) ENGINE=MyISAM AUTO_INCREMENT=9275108 DEFAULT CHARSET=utf8
这是显示索引的输出:
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| 04c1Tg0M | 0 | PRIMARY | 1 | id | A | 9275107 | NULL | NULL | | BTREE | |
| 04c1Tg0M | 1 | pdex | 1 | username | A | 9275107 | NULL | NULL | | BTREE | |
| 04c1Tg0M | 1 | pdex | 2 | id | A | 9275107 | NULL | NULL | | BTREE | |
| 04c1Tg0M | 1 | pdex | 3 | tried | A | 9275107 | NULL | NULL | YES | BTREE | |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
这是相关查询的输出:
+---------+
| MIN(id) |
+---------+
| 1 |
+---------+
1 row in set (3.76 sec)
我需要大幅减少查询时间。任何帮助是极大的赞赏。