![]() |
|
首页 │ Apache │ Linux│ Java│ MySQL│ 注册│帮助 | |||
分别建立两个复合索引:
ALTER TABLE tablename ADD INDEX `cid_ttime` ( `cid` , `torder` , `ttime` )
ALTER TABLE tablename ADD INDEX `cid_tviews` ( `cid` , `torder` , `tviews` )
为什么只有第一个生效,后面那个不生效呢?SQL 语句:
使用第一个:
[php]EXPLAIN SELECT *
FROM site_threads t
WHERE 1
AND t.cid =121
ORDER BY torder DESC , ttime DESC
LIMIT 0 , 25[/php]
显示:
table type possible_keys key key_len ref rows Extra
t ref cid,tweekviews_cid,cid_tviews cid 2 const 6 Using where
使用第二个:
[php]EXPLAIN SELECT *
FROM site_threads t
WHERE 1
AND t.cid =121
ORDER BY torder DESC , tviews DESC
LIMIT 0 , 25[/php]
显示:
table type possible_keys key key_len ref rows Extra
t ref cid,tweekviews_cid,cid_tviews cid 2 const 6 Using where; Using filesort
多了个 Using filesort 出来,就是说第二条SQL的 ORDER BY没有使用应有的索引,郁闷中。。。
该怎么办?

