简体   繁体   English

在mysql 5.1中优化查询和表

[英]optimizing query and table in mysql 5.1

I have a question regarding optimizing the following query/table 我有一个关于优化以下查询/表的问题

SELECT playitemid,MAX(playdatetime) 
        FROM   buma
        WHERE  licenseid = 1 AND playdatetime > Date_sub(Curdate(), INTERVAL 1 month) 
        GROUP  BY playitemid 

For a table with 11 million records this can sometimes take over 30 seconds. 对于具有1100万条记录的表,有时可能需要30秒钟以上。

Here is the create statement for the table. 这是该表的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;

Is there any way to define a better key or index to speed up the query 有什么方法可以定义更好的键或索引来加快查询速度

Kind regards, 亲切的问候,

Bjørn 比约恩

Try to index only playdatetime, licenseid, playitemid alone or groupe only licenseid , playitemid . 尝试仅对playdatetime,licenseid,playitemid单独编制索引,或仅对licenseidplayitemid

MySQL 5.1 is very old (10 years old). MySQL 5.1非常旧(10年)。 Upgrade to 5.7 for a good performance increase (or use a mysql fork like mariadb 10.1). 升级到5.7可提高性能(或使用像mariadb 10.1这样的mysql fork)。

You can also try to calculate this : Date_sub(Curdate(), INTERVAL 1 month) without mysql (php strtotime or other), and use mysql cache for instant request if you call it more than one time per day, be carefull dont use more than 128MB, it can decrease performances too). 您也可以尝试计算以下内容:Date_sub(Curdate(),INTERVAL 1 month)without mysql(php strtotime或其他),如果每天调用多次,请使用mysql缓存进行即时请求,请注意不要使用更多超过128MB,也会降低性能)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM