限制在Oracle中检查的行数

yeotifhr  于 2022-10-04  发布在  Oracle
关注(0)|答案(0)|浏览(210)

我的表中有数百万条记录。在下面的查询中,我是否可以让Oracle 12c只检查前X行,而不是执行全表扫描?

我想X的值应该是Offset+Fetch Next,所以在本例中是15

SELECT * FROM table OFFSET 5 ROWS FETCH NEXT 10 ROWS ONLY;

提前谢谢你

编辑1

这些是涉及的表,这是实际的查询

Orders-这个表在我的测试数据库中有11.3万条记录(像我最初提到的问题一样,在Prod数据库中有超过800万条记录)

--------------------------
| Id | SKUField1|SKUField2|  
--------------------------
| 1  | Value1   | Value2  |
| 2  | Value2   | Value2  |
| 3  | Value1   | Value3  |
--------------------------

Products-此表在我的测试数据库中有200万条记录(Prod数据库类似)

---------------
| PId| SKU_NUM|  
---------------
| 1  | Value1 |
| 2  | Value2 |
| 3  | Value3 |
---------------

请注意,Orders.SKUField1和Orders.SKUField2的值来自Products.SKU_NUM值

实际查询:

SELECT /*+ gather_plan_statistics */ Id, PId, SKUField1, SKUField2, SKU_NUM
FROM Orders 
LEFT JOIN (
-- this inner query reduces size of Products from 2 million rows down to 1462 rows
  select * from Products where SKU_NUM in ( 
    select SKUField1 from Orders
  )
) p1 ON SKUField1 = p1.SKU_NUM 
LEFT JOIN (
-- this inner query reduces size of table B from 2 million rows down to 459 rows
  select * from Products where SKU_NUM in (
    select SKUField2 from Orders
  )
) p4 ON SKUField2  = p4.SKU_NUM
OFFSET 5 ROWS FETCH NEXT 10 ROWS ONLY

执行计划:

--------------------------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                         | Name                  | Starts | E-Time   | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |
--------------------------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                  |                       |      1 |          |     10 |00:00:00.06 |    8013 |       |       |          |
|*  1 |  VIEW                             |                       |      1 | 00:00:01 |     10 |00:00:00.06 |    8013 |       |       |          |
|*  2 |   WINDOW NOSORT STOPKEY           |                       |      1 | 00:00:01 |     15 |00:00:00.06 |    8013 |    27M|  1904K|          |
|*  3 |    HASH JOIN RIGHT OUTER          |                       |      1 | 00:00:01 |     15 |00:00:00.06 |    8013 |  1162K|  1162K| 1344K (0)|
|   4 |     VIEW                          |                       |      1 | 00:00:01 |   1462 |00:00:00.04 |    6795 |       |       |          |
|   5 |      NESTED LOOPS                 |                       |      1 | 00:00:01 |   1462 |00:00:00.04 |    6795 |       |       |          |
|   6 |       NESTED LOOPS                |                       |      1 | 00:00:01 |   1462 |00:00:00.04 |    5333 |       |       |          |
|   7 |        SORT UNIQUE                |                       |      1 | 00:00:01 |   1469 |00:00:00.04 |    3010 | 80896 | 80896 |71680  (0)|
|   8 |         TABLE ACCESS FULL         | Orders                |      1 | 00:00:01 |    113K|00:00:00.02 |    3010 |       |       |          |
|*  9 |        INDEX UNIQUE SCAN          | UIX_Product_SKU_NUM   |   1469 | 00:00:01 |   1462 |00:00:00.01 |    2323 |       |       |          |
|  10 |       TABLE ACCESS BY INDEX ROWID | Products              |   1462 | 00:00:01 |   1462 |00:00:00.01 |    1462 |       |       |          |
|* 11 |     HASH JOIN RIGHT OUTER         |                       |      1 | 00:00:01 |     15 |00:00:00.02 |    1218 |  1142K|  1142K| 1335K (0)|
|  12 |      VIEW                         |                       |      1 | 00:00:01 |    459 |00:00:00.02 |    1213 |       |       |          |
|  13 |       NESTED LOOPS                |                       |      1 | 00:00:01 |    459 |00:00:00.02 |    1213 |       |       |          |
|  14 |        NESTED LOOPS               |                       |      1 | 00:00:01 |    459 |00:00:00.02 |     754 |       |       |          |
|  15 |         SORT UNIQUE               |                       |      1 | 00:00:01 |    462 |00:00:00.02 |     377 | 24576 | 24576 |22528  (0)|
|  16 |          INDEX FAST FULL SCAN     | Orders_SKUField2_IDX6 |      1 | 00:00:01 |    113K|00:00:00.01 |     377 |       |       |          |
|* 17 |         INDEX UNIQUE SCAN         | UIX_Product_SKU_NUM   |    462 | 00:00:01 |    459 |00:00:00.01 |     377 |       |       |          |
|  18 |        TABLE ACCESS BY INDEX ROWID| Products              |    459 | 00:00:01 |    459 |00:00:00.01 |     459 |       |       |          |
|  19 |      TABLE ACCESS FULL            | Orders                |      1 | 00:00:01 |     15 |00:00:00.01 |       5 |       |       |          |
--------------------------------------------------------------------------------------------------------------------------------------------------

因此,根据执行计划中行ID 8和16的“A-ROWS”列值,似乎有对ORDERS表的全表扫描(尽管行ID至少16似乎在使用索引)。所以我的问题是,即使我使用的是Offset/Fetch Next,ORDERS表上的全表扫描是真的吗

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题