In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "the analysis of the sorting problem of the database paging query". In the daily operation, I believe that many people have doubts about the sorting problem of the database paging query. The editor consulted all kinds of data and sorted out the simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "the sorting problem analysis of the database paging query". Next, please follow the editor to study!
SQL > CREATE TABLE TEST AS SELECT ROWNUM ID, A.* FROM DBA_OBJECTS A
The table has been created.
SQL > EXEC DBMS_STATS.GATHER_TABLE_STATS (USER, 'TEST')
The PL/SQL process completed successfully.
SQL > SET AUTOT ON EXP
SQL > SELECT ID, OBJECT_NAME, OWNER
2 FROM
3 (
4 SELECT ROWNUM RN, ID, OBJECT_NAME, OWNER
5 FROM
6 (
7 SELECT OWNER, OBJECT_NAME, ID
8 FROM TEST ORDER BY OWNER
9)
10 WHERE ROWNUM = 1
ID OBJECT_NAME OWNER
-
6231 AL CCC
6232 AL_I_2 CCC
6233 AL_I_FNAME_STATUS CCC
6236 BCB CCC
6235 AL_U1 CCC
6234 AL_P CCC
6240 BCF_U1 CCC
6239 BCF_P CCC
6238 BCF CCC
6237 BCB_U1 CCC
Ten rows have been selected.
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=72 Card=10 Bytes=1090)
10 VIEW (Cost=72 Card=10 Bytes=1090)
2 1 COUNT (STOPKEY)
3 2 VIEW (Cost=72 Card=6363 Bytes=610848)
43 SORT (ORDER BY STOPKEY) (Cost=72 Card=6363 Bytes=165438)
54 TABLE ACCESS (FULL) OF 'TEST' (Cost=20 Card=6363 Bytes=165438)
The above example shows the standard writing method of paging query, which has the highest efficiency for querying the first N pieces of data.
But there is a problem with this paging sort statement:
SQL > SELECT ID, OBJECT_NAME, OWNER
2 FROM
3 (
4 SELECT ROWNUM RN, ID, OBJECT_NAME, OWNER
5 FROM
6 (
7 SELECT OWNER, OBJECT_NAME, ID
8 FROM TEST ORDER BY OWNER
9)
10 WHERE ROWNUM = 11
ID OBJECT_NAME OWNER
-
6249 BP_P CCC
6248 BP_I_DEVICE_HANDLE_STATUS CCC
6247 BP CCC
6245 BDF_P CCC
6243 BDF_I_BS_KEY CCC
6241 BCF_U2 CCC
6239 BCF_P CCC
6237 BCB_U1 CCC
6236 BCB CCC
6235 AL_U1 CCC
Ten rows have been selected.
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=72 Card=20 Bytes=2180)
1 0 VIEW (Cost=72 Card=20 Bytes=2180)
2 1 COUNT (STOPKEY)
3 2 VIEW (Cost=72 Card=6363 Bytes=610848)
43 SORT (ORDER BY STOPKEY) (Cost=72 Card=6363 Bytes=165438)
54 TABLE ACCESS (FULL) OF 'TEST' (Cost=20 Card=6363 Bytes=165438)
Comparing this result with the first one, you will find that the data with an ID of 6235 appears twice. For the first time in the first 10 return records, 6235 appeared, and the second time in 11 to 20 records, 6235 appeared again. If a piece of data is repeated twice, it must mean that there is data that will not appear in both queries.
In fact, the reason for this problem is very simple, because the sort column is not unique. Oracle the sorting algorithm used here is not stable, that is to say, for data with equal key values, after sorting, this algorithm does not guarantee that the data with equal key values will remain in the order before sorting.
In this example, the OWNER column contains a large amount of data with a key value of CCC, and the sorting algorithm of Oracle is not stable, so the order of key values in the first 10 rows of records and the first 20 rows of records is not guaranteed to be consistent. As a result, some data will be repeated and some data will not appear.
Solving this problem is actually very simple. There are two ways to consider.
First, when sorting with non-unique fields, it is followed by a unique field.
SQL > SELECT ID, OBJECT_NAME, OWNER
2 FROM
3 (
4 SELECT ROWNUM RN, ID, OBJECT_NAME, OWNER
5 FROM
6 (
7 SELECT OWNER, OBJECT_NAME, ID
8 FROM TEST ORDER BY OWNER, ID
9)
10 WHERE ROWNUM = 1
ID OBJECT_NAME OWNER
-
6231 AL CCC
6232 AL_I_2 CCC
6233 AL_I_FNAME_STATUS CCC
6234 AL_P CCC
6235 AL_U1 CCC
6236 BCB CCC
6237 BCB_U1 CCC
6238 BCF CCC
6239 BCF_P CCC
6240 BCF_U1 CCC
Ten rows have been selected.
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=72 Card=10 Bytes=1090)
10 VIEW (Cost=72 Card=10 Bytes=1090)
2 1 COUNT (STOPKEY)
3 2 VIEW (Cost=72 Card=6363 Bytes=610848)
43 SORT (ORDER BY STOPKEY) (Cost=72 Card=6363 Bytes=165438)
54 TABLE ACCESS (FULL) OF 'TEST' (Cost=20 Card=6363 Bytes=165438)
SQL > SELECT ID, OBJECT_NAME, OWNER
2 FROM
3 (
4 SELECT ROWNUM RN, ID, OBJECT_NAME, OWNER
5 FROM
6 (
7 SELECT OWNER, OBJECT_NAME, ID
8 FROM TEST ORDER BY OWNER, ID
9)
10 WHERE ROWNUM = 11
ID OBJECT_NAME OWNER
-
6241 BCF_U2 CCC
6242 BDF CCC
6243 BDF_I_BS_KEY CCC
6244 BDF_I_DF_KEY CCC
6245 BDF_P CCC
6246 BDF_U1 CCC
6247 BP CCC
6248 BP_I_DEVICE_HANDLE_STATUS CCC
6249 BP_P CCC
6250 BP_U1 CCC
Ten rows have been selected.
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=72 Card=20 Bytes=2180)
1 0 VIEW (Cost=72 Card=20 Bytes=2180)
2 1 COUNT (STOPKEY)
3 2 VIEW (Cost=72 Card=6363 Bytes=610848)
43 SORT (ORDER BY STOPKEY) (Cost=72 Card=6363 Bytes=165438)
54 TABLE ACCESS (FULL) OF 'TEST' (Cost=20 Card=6363 Bytes=165438)
It is usually fine to follow a primary key after the sort field, or with ROWID if there is no primary key in the table.
This method is the simplest and has the least impact on performance. Another way is to use the BETWEEN AND method that has been given many times before.
SQL > SELECT ID, OBJECT_NAME, OWNER
2 FROM
3 (
4 SELECT ROWNUM RN, ID, OBJECT_NAME, OWNER
5 FROM
6 (
7 SELECT OWNER, OBJECT_NAME, ID
8 FROM TEST ORDER BY OWNER
9)
10)
11 WHERE RN BETWEEN 1 AND 10
ID OBJECT_NAME OWNER
-
6231 AL CCC
6232 AL_I_2 CCC
6233 AL_I_FNAME_STATUS CCC
6234 AL_P CCC
6238 BCF CCC
6240 BCF_U1 CCC
6242 BDF CCC
6244 BDF_I_DF_KEY CCC
6246 BDF_U1 CCC
6255 BRL_U1 CCC
Ten rows have been selected.
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=72 Card=6363 Bytes=693567)
1 0 VIEW (Cost=72 Card=6363 Bytes=693567)
2 1 COUNT
3 2 VIEW (Cost=72 Card=6363 Bytes=610848)
43 SORT (ORDER BY) (Cost=72 Card=6363 Bytes=165438)
54 TABLE ACCESS (FULL) OF 'TEST' (Cost=20 Card=6363 Bytes=165438)
SQL > SELECT ID, OBJECT_NAME, OWNER
2 FROM
3 (
4 SELECT ROWNUM RN, ID, OBJECT_NAME, OWNER
5 FROM
6 (
7 SELECT OWNER, OBJECT_NAME, ID
8 FROM TEST ORDER BY OWNER
9)
10)
11 WHERE RN BETWEEN 11 AND 20
ID OBJECT_NAME OWNER
-
6254 BRL_P CCC
6253 BRL_I_DTS CCC
6252 BRL_I_BS_KEY CCC
6251 BRL CCC
6250 BP_U1 CCC
6249 BP_P CCC
6248 BP_I_DEVICE_HANDLE_STATUS CCC
6247 BP CCC
6264 CCF CCC
6263 CCB_U1 CCC
Ten rows have been selected.
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=72 Card=6363 Bytes=693567)
1 0 VIEW (Cost=72 Card=6363 Bytes=693567)
2 1 COUNT
3 2 VIEW (Cost=72 Card=6363 Bytes=610848)
43 SORT (ORDER BY) (Cost=72 Card=6363 Bytes=165438)
54 TABLE ACCESS (FULL) OF 'TEST' (Cost=20 Card=6363 Bytes=165438)
In this way, due to the use of full sorting of table data, only a certain part of the data in the full sort is taken at a time, so the problem of duplicate data mentioned above will not occur.
However, due to the use of full sorting, and the ROWNUM information can not be pushed to the inside of the query, the execution efficiency of this writing is very low.
At this point, the study on the sorting problem analysis of database paging query is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.