我有一個表:MySQL查詢不使用索引
CREATE TABLE `p` (
`id` bigint(20) unsigned NOT NULL,
`rtime` datetime NOT NULL,
`d` int(10) NOT NULL,
`n` int(10) NOT NULL,
PRIMARY KEY (`rtime`,`id`,`d`) USING BTREE
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
和我有一個查詢:
select id, d, sum(n) from p where rtime between '2012-08-25' and date(now()) group by id, d;
我運行在一個小本上查詢說明表(2條),它告訴我它會用我的PK:
id | select_type | table | type | possible_keys key | key | key_len | ref | rows | Extra
1 | SIMPLE | p | range | PRIMARY | PRIMARY | 8 | NULL | 1 | Using where; Using temporary; Using filesort
但是當我使用相同的查詢在同一個表 - 只是這一次它是巨大的(350萬條記錄) - 它寧願去通過所有的記錄,而忽略了我的鑰匙
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
1 | SIMPLE | p | ALL | PRIMARY | NULL | NULL | NULL | 355465280 | Using where; Using temporary; Using filesort
顯然,這是極其緩慢.. 誰能幫助?
編輯:這個簡單的查詢也正在時間的顯著量:
select count(*) from propagation_delay where rtime > '2012-08-28';
你可能想要考慮在http://dba.stackexchange.com上詢問這個問題,看起來他們可能會更好地配置來解釋這種行爲 - ofcourse我並沒有敲那個人的l33t SQL skillz :) – jammypeach
你修了桌子嗎? http://dev.mysql.com/doc/refman/5.1/en/repair-table.html – jcho360