SQL 查询速度太慢(flutter mobile)
SQL query is too slow (flutter mobile)
我不确定以下查询有什么问题。
对于 size/length 中提供的更大的 names
数组,查询似乎 运行 非常慢(可能呈指数级缓慢,不确定):
以下是 mytable
的列:
name, timestamp, diff, diff2, ratio, minus_one
minus_one
列只是一个辅助列,对于每个插入的行,它始终具有 -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
那是因为where子句的查询时间比较长。
您可以通过执行以下操作之一改进查询:
使用多列索引:
CREATE INDEX index_name ON table(col1, col2);
使用 sqflite 插件
我不确定以下查询有什么问题。
对于 size/length 中提供的更大的 names
数组,查询似乎 运行 非常慢(可能呈指数级缓慢,不确定):
以下是 mytable
的列:
name, timestamp, diff, diff2, ratio, minus_one
minus_one
列只是一个辅助列,对于每个插入的行,它始终具有 -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
那是因为where子句的查询时间比较长。 您可以通过执行以下操作之一改进查询:
使用多列索引:
CREATE INDEX index_name ON table(col1, col2);
使用 sqflite 插件