Oracle SQL 选择不同查询返回特定行数
我在 Oracle 数据库中有一个表,其中包含时间戳列 nextTime 和字符串列 destName。还有更多列,但在本例中只有这两列相关。我正在尝试设计一个查询,该查询将返回具有特定时间间隔内的 nextTime 的不同 destName,并且返回的行数应最大为 1000。当间隔内有超过一千个不同的 destName 时,我希望查询返回一千行,不多也不少。
我实际上有一个正在运行的查询,但它太慢了:
select destName
from (select /*+ index(tblDestNames tbldestnames_destname)*/ distinct destName
from (select /*+ index(tblDestNames tbldestnames_nextTime)*/ destName
from tblDestNames
where nextTime < :1 and nextTime >= :2 and destName is not null))
where rownum <= 1000;
非常感谢任何有关如何设计更智能的查询或如何优化现有查询的想法。
I have a table in an Oracle database with a timestamp column nextTime and a string column destName. There are more columns as well, but only these two are relevant in this case. I'm trying to design a query that will return distinct destName having nextTime within a specific interval and the returned number of rows should be maximum one thousand. When there are more than one thousand distinct destName within the interval I want the query to return one thousand rows, netiher more nor less.
I actually have a query that is working, but it is way too slow:
select destName
from (select /*+ index(tblDestNames tbldestnames_destname)*/ distinct destName
from (select /*+ index(tblDestNames tbldestnames_nextTime)*/ destName
from tblDestNames
where nextTime < :1 and nextTime >= :2 and destName is not null))
where rownum <= 1000;
Any ideas on how to design a smarter query or how to optimize this exisiting one are very much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定是否有理由不将查询简化为这样:
但是,这不会解决您的性能问题。问题是这样的:
通过用“rank”和“over”替换 rownum,您会得到类似的结果:
好处是,使用“over”您可以选择将显示的结果的顺序和不会显示的结果的顺序。
编辑:实际上它可以进一步简化为:
I am not sure that there is a reason not to simplify the query to this:
However, this will not solve your performance issue. The problem is this:
By replacing rownum with 'rank' and 'over' you would get something like:
The bonus is that with 'over' you choose the order of the results that will show and the ones that will not show.
EDIT: Actually it can be further simplified into:
我学到的东西
简单的查询具有稍微不同的语义:
请注意,
BETWEEN
是包容性的,即< code>x BETWEEN y AND z 等于 y <= x <= z。要排除上限,您必须按照您的方式进行操作,或者将参数 :2 减少 nextTime 的单位之一。Things I picked up
Simpler query with slighly different semantics:
Note that
BETWEEN
is inclusive i.e.x BETWEEN y AND z
equalsy <= x <= z
. To exclude the upper bound you'll either have to do it the way you did or decrement the parameter :2 by one of what ever unit nextTime is.