我有一個基於3列的複合索引,其中兩個約束在我的查詢中,第三個約束在子句中,但mysql不使用索引進行排序。優化我的mysql查詢以使用索引進行排序
explain select * from videos where public_private='public' and approved='yes' order by number_of_views desc; +----+-------------+--------+------+--------------------------------+------+---------+------+---------+-----------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+------+--------------------------------+------+---------+------+---------+-----------------------------+ | 1 | SIMPLE | videos | ALL | approved,approved_3,approved_2 | NULL | NULL | NULL | 1476818 | Using where; Using filesort | +----+-------------+--------+------+--------------------------------+------+---------+------+---------+-----------------------------+
表結構如下:
CREATE TABLE `videos` (
`indexer` int(9) NOT NULL auto_increment,
`user_id` int(9) default NULL,
`public_private` varchar(24) default NULL,
`approved` varchar(24) default NULL,
`number_of_views` int(9) default NULL,
PRIMARY KEY (`indexer`),
KEY `approved` (`approved`,`user_id`),
KEY `approved_3` (`approved`,`public_private`,`indexer`),
KEY `approved_2` (`approved`,`public_private`,`number_of_views`),
) ENGINE=MyISAM AUTO_INCREMENT=1969091 DEFAULT CHARSET=utf8 |
我應該怎麼做來強制MySQL使用索引排序的結果?
FORCE INDEX解決了問題。現在查詢執行速度快得多,而且您對查詢匹配大部分數據庫是正確的。 – 2009-07-18 11:02:47
好東西,很高興它的工作。 – zombat 2009-07-18 16:36:20