问题:
这样一条sql应该怎么优化?
select * from sys_user
where user_code = 'zhangyong'
or user_code in
(select grp_code
from sys_grp
where sys_grp.user_code = 'zhangyong')
Execution Plan
----------------------------------------------------------
0SELECT STATEMENT Optimizer=RULE
10 FILTER
21 TABLE Access (FULL) OF 'SYS_USER'
31 INDEX (UNIQUE SCAN) OF 'PK_SYS_GRP' (UNIQUE)
Statistics
----------------------------------------------------------
14 recursive calls
4 db block gets
30590 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
0 sorts (memory)
0 sorts (disk)
3 rows processed
里面的查询返回的记录数一般只有一两条,但sys_user表的数据很多,怎么样才能让这条sql以sys_grp为驱动表?表中记录情况如下:
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
----------------------------------------------------------
0recursive calls
0db block gets
130 consistent gets
0physical reads
0redo size
1723bytes sent via SQL*Net to client
425bytes received via SQL*Net from client
2SQL*Net roundtrips to/from client
1sorts (memory)
0sorts (disk)
3rows processed
Execution Plan
----------------------------------------------------------
0SELECT STATEMENT Optimizer=RULE
10 UNION-ALL
21 TABLE ACCESS (BY INDEX ROWID) OF 'SYS_USER'
32 INDEX (UNIQUE SCAN) OF 'PK_SYS_USER' (UNIQUE)
41 NESTED LOOPS
54 VIEW OF 'VW_NSO_1'
65 SORT (UNIQUE)
76 TABLE ACCESS (BY INDEX ROWID) OF 'SYS_GRP'
87 INDEX (RANGE SCAN) OF 'FK_SYS_USER_CODE' (NON-UNIQUE)
94 TABLE ACCESS (BY INDEX ROWID) OF 'SYS_USER'
109 INDEX (UNIQUE SCAN) OF 'PK_SYS_USER' (UNIQUE)
我们注重到,通过改写,逻辑读减少到130,从30590到130这是一个巨大的提高,减少逻辑读最终会减少资源消耗,
提高SQL的执行效率.
这个改写把Filter改为了Nest LOOP,索引得以充分利用.从而大大提高了性能.
我们同时注重到,这里引入了一个排序
排序来自于这一步:
-----------------------------------------------------------------------------------------
65SORT (UNIQUE)
76TABLE ACCESS (BY INDEX ROWID) OF 'SYS_GRP'
87 INDEX (RANGE SCAN) OF 'FK_SYS_USER_CODE' (NON-UNIQUE)
------------------------------------------------------------------------------------------在'SYS_GRP'表中,user_code 是非唯一键值
在in值判定里,要做sort unique排序,去除重复值
这里的union all是不需要排序的