集群是一种存储密切相关的表的方法,这些表通常连接到磁盘上的同一区域。集群键是在查询中通常用来连接表以节省 IO 的一列或多列。但是,如果单个 Cluster Row 中所有表行的总大小超过一个磁盘块的大小,那么您最终将处于链接状态,并且有一天,您将失去集群的所有优势。在我看来,最好避免,因为考虑到集群中所有 3 个表的滚动量为 1.2 M,这显然会产生 HWM 的影响,这将是一个开销。
最好去JOINS。
例如。
CREATE TABLE BUILDING_C ( BUILDING_ID NUMBER PRIMARY KEY,
ADDRESS_FIELD VARCHAR2 ( 25 ) );
CREATE TABLE PEOPLE_C ( BUILDING_ID NUMBER PRIMARY KEY,
CUSTOMER_ID NUMBER,
ROOM_ID NUMBER,
CUSTOMER_DETAILS VARCHAR2 ( 25 ) );
CREATE TABLE ROOM_C ( BUILDING_ID NUMBER PRIMARY KEY,
ROOM_ID NUMBER,
OPEN_DATE DATE,
CURRENT_OCCUPANCY CHAR ( 1 ) );
BEGIN
DBMS_STATS.SET_TABLE_STATS ( OWNNAME => 'REALSPIRITUALS',
TABNAME => 'BUILDING_C',
NUMROWS => 20000000 );
END;
/
BEGIN
DBMS_STATS.SET_TABLE_STATS ( OWNNAME => 'REALSPIRITUALS',
TABNAME => 'PEOPLE_C',
NUMROWS => 20000000 );
END;
/
BEGIN
DBMS_STATS.SET_TABLE_STATS ( OWNNAME => 'REALSPIRITUALS',
TABNAME => 'ROOM_C',
NUMROWS => 20000000 );
END;
/
在您的查询中,您的提示将不会生效,因为您使用SELECT * /*+ FIRST_ROWS(200)*/
了而不是SELECT /*+ FIRST_ROWS(200)*/ *
所以您最终会使用 OPTIMIZER MODE=ALL_ROWS 而不是 OPTIMIZER MODE=FIRST_ROWS
SET AUTOTRACE ON
SELECT
*
FROM
(SELECT
/*+ FIRST_ROWS(200)*/
*
FROM
BUILDING_C R
INNER JOIN PEOPLE_C C
ON ( R.BUILDING_ID = C.BUILDING_ID )
INNER JOIN ROOM_C S
ON ( S.BUILDING_ID = R.BUILDING_ID )
WHERE
S.OPEN_DATE >= SYSDATE - 60 - 1
AND S.OPEN_DATE <= SYSDATE - 60
ORDER BY
S.OPEN_DATE)
WHERE
ROWNUM < 200;
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer Mode=HINT: FIRST_ROWS (Cost=54189 Card=199 Bytes=38 K)
1 0 COUNT STOPKEY
2 1 VIEW (Cost=54189 Card=50 K Bytes=9 M)
3 2 SORT ORDER BY STOPKEY (Cost=54189 Card=50 K Bytes=9 M)
4 3 FILTER
5 4 NESTED LOOPS
6 5 NESTED LOOPS (Cost=52041 Card=50 K Bytes=9 M)
7 6 MERGE JOIN (Cost=2020 Card=50 K Bytes=5 M)
8 7 TABLE ACCESS BY INDEX ROWID REALSPIRITUALS.BUILDING_C (Cost=826 Card=20 M Bytes=1G)
9 8 INDEX FULL SCAN REALSPIRITUALS.SYS_C00504893 (Cost=26 Card=20 M)
10 7 SORT JOIN (Cost=1194 Card=50 K Bytes=1 M)
11 10 TABLE ACCESS FULL REALSPIRITUALS.ROOM_C (Cost=660 Card=50 K Bytes=1 M)
12 6 INDEX UNIQUE SCAN REALSPIRITUALS.SYS_C00504894 (Cost=0 Card=1)
13 5 TABLE ACCESS BY INDEX ROWID REALSPIRITUALS.PEOPLE_C (Cost=1 Card=1 Bytes=91)
Statistics
----------------------------------------------------------
1 recursive calls
0 spare statistic 3
0 gcs messages sent
0 db block gets from cache
0 physical reads direct (lob)
0 queue position update
0 queue single row
0 queue ocp pages
0 HSC OLTP Compressed Blocks
0 HSC IDL Compressed Blocks
0 rows processed
建议:
- 在 OPEN_DATE 列上使用索引
- 如果需要加速使用并行提示 /*+ parallel (table,n) */
- 尝试范围分区