快速查询最新记录的方法?
我有一个这样的表:
USER | PLAN | START_DATE | END_DATE
1 | A | 20110101 | NULL
1 | B | 20100101 | 20101231
2 | A | 20100101 | 20100505
如果 END_DATE
为 null
,则意味着该用户当前有该计划处于活动状态。
我想查询的是: (a) 他当前正在执行的计划,或 (b) 他最近执行的计划。我只需要为每个给定用户返回一行。
现在,我设法通过使用联合和子查询来做到这一点,但碰巧表很大并且这些效率不够高。 你们中有人有更快的方法来查询吗?
谢谢,
[编辑] 这里的大多数答案都会返回一个值。那是我的错。我的意思是为每个用户返回一个值,但同时返回所有用户。我已经调整了我能给出的答案(并纠正了问题),但只是为了将来的参考而清楚地说明了这一点。
I have a table of the sort:
USER | PLAN | START_DATE | END_DATE
1 | A | 20110101 | NULL
1 | B | 20100101 | 20101231
2 | A | 20100101 | 20100505
In a way that if END_DATE
is null
, means that this user has that plan currently active.
What I want to query is:
(a) the current plan he has active, or (b) the lastest plan he was into. I need only one row returned for each given user.
Now, I managed to do that in using unions and sub queries, but it happens that table is massive and these are not efficient enough.
Would any of you guys have a quicker way to query that?
Thanks,
[EDIT]
Most answers here return a single value. That was my bad. What I meant was to return a single value per user but all users at once. I've adapted the answers I could (and corrected the question) but just making it clear for future reference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果没有有关数据和表格的进一步信息,这个问题有点难以回答。当您在评论中说您拥有所需的所有索引时,这些索引是什么?
另外,时间段是否相邻且不重叠?您能否获取最新 START_DATE 的时间段?
查看 END_DATE 的问题是普通的 B 树索引不会索引 NULL。因此,
where end_date is nulll
形式的谓词不太可能使用索引。您可以对列使用位图索引,因为这些类型的索引会对空值进行索引,但这可能并不理想,因为位图索引还有一些其他缺点。由于上面给出的原因,我可能会使用类似于以下查询的查询:
根据具体要求,您可以在此处使用
row_num_1
或row_num_2
列。或者,
无论您是尝试恢复所有用户还是仅恢复一个用户,第一个查询都应该有效。第二个查询仅适用于一个用户。
如果您可以使用架构的更多详细信息(索引、开始/结束日期的含义)来补充问题,您可能会得到更好的答案。
This question is a little hard to answer without further information about the data and the table. When you say in your comment that you have all the indexes that you need, what are these indexes?
Also, are the time periods abutting and non-overlapping? Can you just get the period with the latest START_DATE?
The problem with looking at END_DATE is that a normal B-Tree index doesn't index NULLs. So, a predicate of the form
where end_date is nulll
is unlikely to use the index. You could use a bitmap index with the column as those type of indexes do index nulls but that might not be ideal because of some of the other drawbacks of bitmap indexes.For the reasons given above, I would probably use a query similar to the one below:
You could probably use either the
row_num_1
or therow_num_2
column here depending on the exact requirements.OR
The first query should work whether you are trying get all the users back or just one. The second query will only work with one user.
If you can augment the question with more details of the schema (indexes, meaning of the start/end date) you are likely to get better answers.
这可能会有所帮助:
This may help:
您是否尝试过使用 rownum 来限制结果集?
Have you tried to limit the resultset with
rownum
?AFAIK 使用
CASE
和子查询会导致查询变得非常慢。所以最好小心使用它们。怎么样:我不是 SQL 专家。将此视为一个建议。
希望这有帮助。
AFAIK Using
CASE
and sub queries will cause your query to become very slow. So better to use them with care. How About:I'm not a SQL guru. consider this just as a suggestion.
Hope this helps.
这有效吗?
如果速度很慢,请将查询的 EXPLAIN 输出发送给我们。
Does this work?
If it is slow, please send us the EXPLAIN output for the query.
这个怎么样?
How about this?
我建议如下:
I suggest the following :