SQL> select count(*) from sys_grp;
COUNT(*)----------25130
SQL> select count(*) from sys_user;
COUNT(*)
----------
15190
优化:
降低逻辑读是优化SQL的基本原则之一
我们尝试通过降低逻辑读来加快SQL的执行.
这里我们使用or展开来改写SQL查询:
select * from sys_user where user_code = 'zhangyong'
union all
select * from sys_user where user_code <> 'zhangyong'
and user_code in (select grp_code from sys_grp
where sys_grp.user_code = 'zhangyong')
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
130 consistent gets
0 physical reads
0 redo size
1723 bytes sent via SQL*Net to client
425 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
3 rows processed
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=RULE
1 0 UNION-ALL
2 1 TABLE ACCESS (BY INDEX ROWID) OF 'SYS_USER'
3 2 INDEX (UNIQUE SCAN) OF 'PK_SYS_USER' (UNIQUE)
4 1 NESTED LOOPS
5 4 VIEW OF 'VW_NSO_1'
6 5 SORT (UNIQUE)
7 6 TABLE ACCESS (BY INDEX ROWID) OF 'SYS_GRP'
8 7 INDEX (RANGE SCAN) OF 'FK_SYS_USER_CODE' (NON-UNIQUE)
9 4 TABLE ACCESS (BY INDEX ROWID) OF 'SYS_USER'
10 9 INDEX (UNIQUE SCAN) OF 'PK_SYS_USER' (UNIQUE)