I am not as familiar with Oracle as I would like to be. I have some 250k records, and I want to display them 100 per page. Currently I have one stored procedure which retrieves all quarter of a million records to a dataset using a data adapter, and dataset, and the dataadapter.Fill(dataset) method on the results from the stored proc. If I have "Page Number" and "Number of records per page" as integer values I can pass as parameters, what would be the best way to get back just that particular section. Say, if I pass 10 as a page number, and 120 as number of pages, from the select statement it would give me the 1880th through 1200th, or something like that, my math in my head might be off.
I'm doing this in .NET with C#, thought that's not important, if I can get it right on the sql side, then I should be cool.
Update: I was able to use Brian's suggestion, and it is working great. I'd like to work on some optimization, but the pages are coming up in 4 to 5 seconds rather than a minute, and my paging control was able to integrate in very well with my new stored procs.
7条答案
按热度按时间gzszwxb41#
应该可以这样做:From Frans Bouma's Blog
kxxlusnw2#
Ask Tom和非常非常有用的分析函数。
以下是该页摘录:
watbbzwu3#
In the interest of completeness, for people looking for a more modern solution, in Oracle 12c there are some new features including better paging and top handling.
Paging
The paging looks like this:
Top N Records
Getting the top records looks like this:
Notice how both the above query examples have
ORDER BY
clauses. The new commands respect these and are run on the sorted data.I couldn't find a good Oracle reference page for
FETCH
orOFFSET
but this page has a great overview of these new features.Performance
As @wweicker points out in the comments below, performance is an issue with the new syntax in 12c. I didn't have a copy of 18c to test if Oracle has since improved it.
Interestingly enough, my actual results were returned slightly quicker the first time I ran the queries on my table (113 million+ rows) for the new method:
However, as @wweicker mentioned, the explain plan looks much worse for the new method:
The new syntax caused a full scan of the index on my column, which was the entire cost. Chances are, things get much worse when limiting on unindexed data.
Let's have a look when including a single unindexed column on the previous dataset:
Summary: use with caution until Oracle improves this handling. If you have an index to work with, perhaps you can get away with using the new method.
Hopefully I'll have a copy of 18c to play with soon and can update
gopyfrb34#
只是想总结一下答案和评论。分页的方法有很多。
在oracle 12 c之前,没有OFFSET/FETCH功能,所以看一下@jasonk建议的whitepaper。这是我找到的关于不同方法的最完整的文章,详细解释了优点和缺点。在这里复制粘贴它们会花费大量的时间,所以我不做了。
还有一篇来自jooq创建者的好文章,解释了oracle和其他数据库分页的一些常见注意事项。
好消息是,自Oracle 12 c以来,我们有了新的OFFSET/FETCH功能。OracleMagazine 12c new features。请参阅“Top-N查询和分页”
可以通过发出以下语句来检查oracle版本
4bbkushb5#
请尝试以下操作:
通过tecnicume(http://mbfu.it/r/ta)
dfuffjeb6#
In my project I used Oracle 12c and java. The paging code looks like this:
lzfw57am7#