按行和日期范围过滤 SQL 查询
我有一个时间索引的 Oracle DB,我正在尝试按日期范围查询它。我还想在查询中减少数据,这样我就不会被太多的数据淹没。
独立日期查询(0.203 秒内 2352 行):
select oracle_time from t_ssv_soh_packets0
where oracle_time >= TIMESTAMP '2009-01-01 00:00:00'
AND oracle_time <= TIMESTAMP '2009-01-31 00:00:00'
独立缩减查询(0.89 秒内 1017 行):
select oracle_time from t_ssv_soh_packets0
where (rowid,0) in (select rowid, mod(rownum,50) from t_ssv_soh_packets0)
当我尝试将它们组合起来时,它需要永远(32.547 秒内 48 行):
select oracle_time from t_ssv_soh_packets0
where oracle_time >= TIMESTAMP '2009-01-01 00:00:00'
AND oracle_time <= TIMESTAMP '2009-01-31 00:00:00'
AND (rowid,0) in (select rowid, mod(rownum,50) from t_ssv_soh_packets0)
显然我正在做一些根本错误的事情在这里,但我不知道如何按日期查询并减少数据。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在针对不在所选日期范围内的行评估您的缩减逻辑。
将缩减逻辑应用到包含日期范围的子查询。
之后:
这就是我的意思。
You are evaluating your reduction logic against rows that are not in your chosen date range.
Apply the reduction logic to a subquery containing your date range.
LATER:
Here's what I meant.
摆脱
in
为什么使用这个?:
你唯一的条件是 mod(rownum, 50)
最后一行应该是
AND mod(rownnum,50)=0
不是 self使用in
加入。Get rid of the
in
Why use this?:
Your only condition is mod(rownum, 50)
last line should be
AND mod(rownnum,50)=0
not a self join with anin
.您还可以通过应用 SAMPLE() 子句:
将随机返回 50% 的行
You can also let Oracle choose a random sample from the result by applying the SAMPLE() clause:
Will return randomly 50% percent of the rows
感谢“Narveson”和“nate c”的指点,我终于弄清楚了。这是我提出的(可能是 Oracle 特定的)查询:
select oracle_time from t_ssv_soh_packets0 where oracle_time >= TIMESTAMP '2009-01-01 00:00:00' AND oracle_time <= TIMESTAMP '2009-01 -31 00:00:00' group by oracle_time, rownumhaving mod(rownum, 50)=0
此查询在 0.031 秒内返回 47 行。原始时间查询有 2352 行,因此这是有道理的。
ORAFAQ 帮助我找到了最终的解决方案。
Thanks to both 'Narveson' and 'nate c' for the pointers I finally figured it out. Here is the (probably Oracle specific) query that I came up with:
select oracle_time from t_ssv_soh_packets0 where oracle_time >= TIMESTAMP '2009-01-01 00:00:00' AND oracle_time <= TIMESTAMP '2009-01-31 00:00:00' group by oracle_time, rownum having mod(rownum, 50)=0
This query returns 47 rows in 0.031s. The original time query had 2352 rows so that makes sense.
The ORAFAQ helped me get to the final solution.