我不确定以下查询有什么问题。
对于提供names
的大小/长度更大的数组,查询似乎运行得很慢(可能呈指数级缓慢,不确定):
以下是 的列mytable
:
name, timestamp, diff, diff2, ratio, minus_one
minus_one
column 只是一个辅助列,-1
对于每个插入的行,它的值总是 。
有人可以帮助优化查询吗?或者你认为这很好吗?也许应该优化我的代码的其他部分以更快地运行它?任何建议都非常感谢。
询问:
SELECT
COUNT(diff) AS count,
IFNULL(AVG(diff), 0) AS avg,
IFNULL(MAX(diff), 0) AS max,
IFNULL(MIN(diff), 0) AS min
FROM
mytable
WHERE
name IN :names
AND timestamp >= :start
AND timestamp <= :end
AND ratio <> -1
AND (minus_one = :ratioStart OR ratio >= :ratioStart)
AND (minus_one = :ratioEnd OR ratio <= :ratioEnd)
AND (minus_one = :diffStart OR diff >= :diffStart)
AND (minus_one = :diffEnd OR diff <= :diffEnd)
AND (minus_one = :diffStart2 OR diff2 >= :diffStart2)
AND (minus_one = :diffEnd2 OR diff2 <= :diffEnd2)
GROUP BY
name
ORDER BY
count DESC