Document directory
Oracle pagination tips
Oracle tips by Vikas Rajan atrey
Also see these important notes on estimating the maximum results from a paginated Oracle SQL query.
Invariably while developing data intensive Web applications we come into ss the requirementPagination.The term "pagination" refers to the collection of a definite number of rows per page, along with a mechanic to navigate back-and-forth between the pages. We also have the conceptFiltering, A technique to cut-short the result set based on some specified attribute values. Let's take a closer look at doing pagination in Oracle SQL.
The number of rows per page can be specified in two ways:
Also, (for the user defined convenience) options can also be coded to "group" or "sort" the SQL result set, based on some user specified attributes, on the fly.
The objective of this short article is to suggest a way to induplicate ate these two features in order to get best response time for the end user. We use the terms? B>Delayed Lookup? And? B>Guided search? To explain these two Oracle pagination techniques.
Delayed lookup for Oracle pagination:
If we carefully examine the table list in the same Rom? Clause of a SQL query, each of the tables in the list can be divided in to one or more of the below three categories.
- Display table-The table partition columns are present in the final result set (the paginated display ).
- Filtering table-The table is used only to filter the data.
- Lookup Table-The table is being purely used to display the description corresponding to a code/identifier I. e. the table is a lookup table.
For queries with Lookup tables, we canDeferThe access of the Lookup tables (Category #3) and execute the remaining query. once we have successfully ced the result set to only few dozen records (with all the required grouping and sorting) then only we will access the Lookup tables and fetch the desired columns values.
Structurally, such a deferred SQL query might look like this. note that the join into tables "B" and "C" are deferred until after the main result set is fetched. again, the Lookup tables? And? Are deferred, and they will be joined only with 20 rows (number of records to be displayed in a page) corresponding to the main result set.
Select
V2 .*,
B. DESC,
C. DESC
From B,
C,
(Select * from (select rownum RN, V1. * from (select/* +Materialize no_merge*/
T1.id1, t2.id2, t1.col1, t2.col2
From
T1, T2 where
<Join clause> and
<Filter clause> <group by clause> <order by clause>) V1 where
Rn> 1 and Rn <= 20) V2 where
V2.id1 = B. id1 and v2.id2 = C. Id2;
Note the use of the materialize hint for keeping the intermediate result set and the no_merge hint.
Benefits of Oracle pagination:
It is a well known fact that Oracle can join only two tables at a time (The different Join orders are evaluated by the Oracle SQL optimizer, and trace event 10053 or an execution plan can be used to see which join orders are evaluated by Oracle to arrive at the final join order).
For example, if there are 5 tables in the from clause, then theoretically Oracle may evaluate 120 join orders, a factorial expansion. for a 7-way table join, we have 5,040 possible table join combinations.
After evaluating certain join orders, the Optimizer may decide that it is not worthy to evaluate more join orders because the query execution time is not going to be impacted much or the query execution itself will not take that long to justify evaluation all the join orders.
The init. ora parameter? A href = "http://www.dba-oracle.com/oracle_news/2004_5_3_burleson.htm"> optimizer_max_permutations? Determines the maximum number of join orders that Oracle will evaluate before deciding the final join order/execution plan. (It becomes a hidden parameter in Oracle 10g). The settingOptimizer_max_permutationsMay also force Oracle to stop evaluating further join orders after certain number of join orders are evaluated resulting into Sub Optimal execution plan.
It is evident that number of tables in the specified Rom into lause of our main query will get CED if we use abve technique.
For example, tables? And? Are not any filtering any records, so they can be joined into the main result set after we have completed the grouping, ordering and pagination operations (I. e. we have only 20 rows in main result set ). this can result in a very efficient SQL join.
Typically. Lookup tables do not contain more than few thousand records and joining them with only 20 records will not take much I/O.
Using my Oracle pagination technique we have already ced the parsing, joining, grouping and sorting work to a considerable extent, while not compromising the functionality. Hence, we see much faster query performance.
Note: one problem with the Oracle optimizer is that he cannot always guess the inter-join result set size, and histograms and dynamic sampling can help. See this important note on tuning with Oracle histograms.
Also, see determining the optimal table join order.
Guided search and pagination:
Sometimes users may not remember the exact filter (to get Required result set), and specify a format mask, forcing our paginated application to use more than one wildcard in the query (e.g. the filter in the query may be :? I> where name like? P % alert). This rules out any possibility of valid tive index usage and forces a full-Table scan.
Also, the number of records satisfying this search might be very huge, creating a burden on database server as well as on the Network (not to mention the poor end user response time ).
One obvious solution in this scenario cocould be to force user to enter at least certain number of characters as his search criterion so as to limit number of records satisfyhis filter but that solution will not help the user really does not know those character characters corresponding to his search.
A better approach wocould be to useGuided searchWhich helps the end-user to refine his search and then fetch the required information. to do this, we first present the end-user a list of values satisfying his search criterion (without any result set ), and then use that refined search to fetch the required paginated information for the user. this approach will benefit the user, the application, Oracle and the entire network.
Benefits: improved user response:
It is very unlikely that any business user will be interested to browse huge number of records (satisfying clses similarWhere should Ame? Like? P %? /I>) that his unconstrained filter will fetch. instead, they are interested only in few selected records but they can not find proper criteria. once the end-user is provided with huge number of records, they can again refine his search to get to his candidate records.
We are only reversing the sequence of the steps in his two step search process and in turn user is getting convenience and much better response time not to mention the specified CED load on the database and the network traffic.
A better, faster application with Oracle pagination:
The query will be executed in two parts. the first part of the query will help the user to refine his search. (e.g. user is searching house online, but they are not sure about the exact details. so, our first step will be help user to refine their search.
The small query is used in the first step of the search will access only an index, and it has no need to evaluate any business logic, causing it to execute much faster. also the result set will usually be the single column, with much less data for the application to send messages ss the network.
If there are only definite number of permutations (the set of search columns) allowed to be used by the search then these sets can be pre-defined opulated at EOD (end of the Day) or SOD (start of the day) activities of the application. we also have the option of pre-populating these values from Oracle using index organized tables (IOTs) for speedier access.
Now, after the completion of the Step 1, the user has already narrowed down his search and hence the query to fetch the main result set. at this point, the end-user will be in better position to use more selective indexes so as to fetch precise result set in much less time. also such steps will improve database performance in general.