数据库查询需要大量时间

时间:2017-06-05 05:01:13

标签: php html mysql

我目前正在运行此查询,加载需要大约15秒才能优化查询

SELECT *, `points`.`players` as `players` ,
FROM_UNIXTIME(`points`.`timestamp`, '%Y-%m-%d %H:%i') as `date`,
        (SELECT   `points`.`players`
        FROM `points`
        WHERE   FROM_UNIXTIME(`points`.`timestamp`, '%Y-%m-%d %H:%i') > `date` - INTERVAL 7 DAY 
                AND FROM_UNIXTIME(`points`.`timestamp`, '%Y-%m-%d %H:%i') < `date` - INTERVAL 167 HOUR 
                AND `server_id` = {$server_id} 
                AND `type` = 2 
                LIMIT 1 
        ) as `prevplayers`
FROM `points`
WHERE   `points`.`timestamp` > UNIX_TIMESTAMP(NOW() - INTERVAL 7 DAY) 
        AND `server_id` = {$server_id} 
        AND `type`=2 
GROUP BY DATE_FORMAT(FROM_UNIXTIME(`points`.`timestamp`), '%Y-%m-%d %H') 
ORDER BY DATE_FORMAT(FROM_UNIXTIME(`points`.`timestamp`),'%Y-%m-%d %H')

表格结构

enter image description here

索引enter image description here

使用说明输出enter image description here

理想时间是1-2秒

任何建议?

由于 Somdeb

1 个答案:

答案 0 :(得分:1)

您可以按照以下顺序创建包含列的附加索引并重试查询吗?

  1. SERVER_ID
  2. 时间戳(提及降序)