[英]optimizing query and table in mysql 5.1
我有一個關於優化以下查詢/表的問題
SELECT playitemid,MAX(playdatetime)
FROM buma
WHERE licenseid = 1 AND playdatetime > Date_sub(Curdate(), INTERVAL 1 month)
GROUP BY playitemid
對於具有1100萬條記錄的表,有時可能需要30秒鍾以上。
這是該表的create語句。
CREATE TABLE `buma` (
`bumaid` int(11) NOT NULL AUTO_INCREMENT,
`playitemid` int(11) NOT NULL,
`playdatetime` datetime DEFAULT NULL,
`stopdatetime` datetime DEFAULT NULL, `licenseid` int(11) NOT NULL, editionid` int(11) DEFAULT NULL,
PRIMARY KEY (`bumaid`),
KEY `ind_buma`(`playdatetime`,`licenseid`,`playitemid`) USING BTREE) ENGINE=InnoDB AUTO_INCREMENT=68644363 DEFAULT CHARSET=latin1;
有什么方法可以定義更好的鍵或索引來加快查詢速度
親切的問候,
比約恩
嘗試僅對playdatetime,licenseid,playitemid單獨編制索引,或僅對licenseid
, playitemid
。
MySQL 5.1非常舊(10年)。 升級到5.7可提高性能(或使用像mariadb 10.1這樣的mysql fork)。
您也可以嘗試計算以下內容:Date_sub(Curdate(),INTERVAL 1 month)without mysql(php strtotime或其他),如果每天調用多次,請使用mysql緩存進行即時請求,請注意不要使用更多超過128MB,也會降低性能)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.