如何正确的使用or展开来改写SQL查询

【字号: 日期:2023-08-18浏览:26作者:雯心
问题:

下面的这条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----------------------------------------------------------0 SELECT STATEMENT Optimizer=RULE1 0 FILTER2 1 TABLE ACCESS (FULL) OF 'SYS_USER'3 1 INDEX (UNIQUE SCAN) OF 'PK_SYS_GRP' (UNIQUE)Statistics----------------------------------------------------------14 recursive calls4 db block gets30590 consistent gets0 physical reads0 redo size1723 bytes sent via SQL*Net to client425 bytes received via SQL*Net from client2 SQL*Net roundtrips to/from client0 sorts (memory)0 sorts (disk)3 rows processed

里面的查询返回的记录数一般只有一两条,但sys_user表的数据很多,怎么样才能让这条sql以sys_grp为驱动表?

表中记录情况如下:

SQL> select count(*) from sys_grp;COUNT(*)----------25130SQL> select count(*) from sys_user;COUNT(*)----------15190

优化:

降低逻辑读是优化SQL的基本原则之一

我们尝试通过降低逻辑读来加快SQL的执行.

这里我们使用or展开来改写SQL查询:

select * from sys_user where user_code = 'zhangyong' union allselect * 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 calls0 db block gets 130 consistent gets0 physical reads0 redo size 1723 bytes sent via SQL*Net to client425 bytes received via SQL*Net from client2 SQL*Net roundtrips to/from client1 sorts (memory)0 sorts (disk)3 rows processedExecution Plan----------------------------------------------------------0 SELECT STATEMENT Optimizer=RULE1 0 UNION-ALL2 1 TABLE ACCESS (BY INDEX ROWID) OF 'SYS_USER'3 2 INDEX (UNIQUE SCAN) OF 'PK_SYS_USER' (UNIQUE)4 1 NESTED LOOPS5 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)

我们注意到,通过改写,逻辑读减少到130,从30590到130这是一个巨大的提高,减少逻辑读最终会减少资源消耗,提高SQL的执行效率.

这个改写把Filter改为了Nest LOOP,索引得以充分利用.从而大大提高了性能.

我们同时注意到,这里引入了一个排序

排序来自于这一步:

----------------------------------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)----------------------------------

注释:在'SYS_GRP'表中,user_code 是非唯一键值,在in值判断里,要做sort unique排序,去除重复值,这里的union all是不需要排序的。

相关文章: