我有這樣定義的表:MySQL多重索引條件變慢?
article | CREATE TABLE `article` (
`id` varchar(64) NOT NULL,
`type` varchar(16) DEFAULT NULL,
`title` varchar(1024) DEFAULT NULL,
`source` varchar(64) DEFAULT NULL,
`over` tinyint(1) DEFAULT NULL,
`taken` tinyint(1) DEFAULT NULL,
`released_at` varchar(32) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_article_over` (`over`),
KEY `idx_article_created_at` (`created_at`),
KEY `idx_article_type` (`type`),
KEY `idx_article_taken` (`taken`),
KEY `idx_article_updated_at` (`updated_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
mysql> select count(1) from article;
+----------+
| count(1) |
+----------+
| 649773 |
+----------+
1 row in set (0.61 sec)
當我做一個查詢:
SELECT * FROM `article` where taken=0 ORDER BY updated_at asc limit 10;
或
SELECT * FROM `article` where over=0 ORDER BY updated_at asc limit 10;
他們都是非常快的。
然而,當我用這個,它變得非常緩慢:
SELECT * FROM `article` where taken=0 and over=0 ORDER BY updated_at asc limit 10;
這需要4.94s。
如果文章表增長到2000萬行,則需要更長的時間。
這裏是2000萬行的解釋:
mysql> explain SELECT * FROM `article` where taken=0 and processed=0 ORDER BY updated_at asc limit 10;
+----+-------------+-----------+------------+-------------+---------------------------------------------+---------------------------------------------+---------+------+---------+----------+-------------------------------------------------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+-------------+---------------------------------------------+---------------------------------------------+---------+------+---------+----------+-------------------------------------------------------------------------------------------+
| 1 | SIMPLE | article | NULL | index_merge | idx_article_processed,idx_article_taken | idx_article_processed,idx_article_taken | 2,2 | NULL | 6234059 | 100.00 | Using intersect(idx_article_processed,idx_article_taken); Using where; Using filesort |
+----+-------------+-----------+------------+-------------+---------------------------------------------+---------------------------------------------+---------+------+---------+----------+-------------------------------------------------------------------------------------------+
mysql> SELECT * FROM `judgement` where taken=0 and processed=0 ORDER BY updated_at asc limit 10;
+--------------------------------------+----------+-----------+---------------------------------------------------------------------------
| id | type | title | source| processed | released_at | created_at | updated_at | taken |
+--------------------------------------+----------+-----------+---------------------------------------------------------------------------
10 rows in set (9 min 15.97 sec)
拍攝,在兩個已經索引了,爲什麼我把它們放在一起,查詢變得更糟?由於更多的索引,它不應該更快嗎?
類似的問題,但在SQL Server中是:[爲什麼多個WHERE條件慢查詢,而不是加速?](https://stackoverflow.com/q/2162621/4519059);)。 –