- 論壇徽章:
- 0
|
先說表結(jié)構(gòu)
CREATE TABLE `keyword` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`key` char(64) NOT NULL,
`value` char(32) DEFAULT NULL,
`type` char(16) NOT NULL,
`update_time` datetime DEFAULT NULL,
`weighing` tinyint(4) DEFAULT '0',
`count` int(11) DEFAULT '0',
PRIMARY KEY (`id`,`key`),
KEY `dudu_keyword_key` (`key`,`value`,`count`)
) ENGINE=MyISAM AUTO_INCREMENT=950 DEFAULT CHARSET=utf8
每次根據(jù)key做like,然后對value做distinct操作,根據(jù)count以及weighing做排序,取前10條,key和value是多對一的關(guān)系,例如'sina'=>'新浪','新浪'=>'新浪','xinlang'=>'新浪'....
大家肯定很奇怪,為什么要不id和key做聯(lián)合主鍵,我本意是想用innodb,在主鍵上做查詢,效率應(yīng)該要高些,結(jié)果我失望了,like的時(shí)候壓根就沒有用主鍵.
于是對sql中用到的key,value,count做聯(lián)合索引
最開始設(shè)想sql如下:select distinct value from keyword where key like "x%" order by count limit 10;
mysql> explain select distinct value from keyword where `key` like "x%" order by count limit 10\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: dudu_keyword
type: range
possible_keys: dudu_keyword_key
key: dudu_keyword_key
key_len: 192
ref: NULL
rows: 38
Extra: Using where; Using temporary; Using filesort
1 row in set (0.00 sec)
杯具,使用了臨時(shí)表還進(jìn)行了排序操作
于是繼續(xù)嘗試:SELECT value FROM dudu_keyword where `key` like 'x%' group by value order by count limit 10
explain還是基本和上面一樣:1, 'SIMPLE', 'dudu_keyword', 'range', 'dudu_keyword_key', 'dudu_keyword_key', '192', '', 38, 'Using where; Using index; Using temporary; Using filesort'
確是沒辦法不讓group by或distinct不使用臨時(shí)表!有經(jīng)驗(yàn)的同仁停下腳,呵呵 |
|