表有大约140万行。
1年查询:
SELECT windDir,
count(windDir)/(
SELECT COUNT(windDir)
FROM archive
WHERE windDir is not NULL AND dateTime >= 1466719200) as frequency
FROM weewx.archive
WHERE windDir is not NULL AND dateTime >= 1466719200
GROUP BY windDir
ORDER BY windDir ASC;
返回正确的结果(cba以复制频率结果)
+---------+-----------+
| windDir | frequency |
+---------+-----------+
| 0 | x |
| 22.5 | x |
| 45 | x |
| 67.5 | x |
| 90 | x |
| 112.5 | x |
| 135 | x |
| 157.5 | x |
| 180 | x |
| 202.5 | x |
| 225 | x |
| 247.5 | x |
| 270 | x |
| 292.5 | x |
| 315 | x |
| 337.5 | x |
+---------+-----------+
2年查询:
SELECT windDir,
count(windDir)/(
SELECT COUNT(windDir)
FROM archive
WHERE windDir IS NOT NULL AND dateTime >= 1435096800) as frequency
FROM weewx.archive
WHERE windDir IS NOT NULL AND dateTime >= 1435096800
GROUP BY windDir
ORDER BY windDir ASC;
唯一的区别是dateTime,它会更改计算的行数。
这可能是某种内存问题吗?
甚至将查询简化为类似
的内容SELECT DISTINCT windDir
from weewx.archive
WHERE dateTime >= 1435096800
ORDER BY windDir ASC;
返回格式错误的查询,如上图所示。
这是怎么回事?
答案 0 :(得分:0)
列winDir
的类型为double,因此那些"奇怪的格式错误"结果只是存储在表中的double类型的值。 (另请注意,它们的范围从0到360,因此它们是"度"。)如果您想要更容易阅读的更可预测的结果,那么可能通过使用调整winDir
输出的精度像这样的ROUND():
select
w.windir
, w.numerator / f.denominator AS frequency
from (
SELECT
ROUND(windDir,1) AS windDir
, COUNT(windDir) AS numerator
FROM weewx.archive
WHERE windDir IS NOT NULL
AND DATETIME >= 1435096800
GROUP BY
ROUND(windDir,1)
) AS w
CROSS JOIN (
SELECT
COUNT(windDir) * 1.0 AS denominator
FROM archive
WHERE windDir IS NOT NULL
AND DATETIME >= 1435096800
) AS f
ORDER BY windDir ASC
;
注意我已将分母的计算推送到交叉连接中,我认为它会提高效率。