Вы находитесь на странице: 1из 7

WHICH QUERY & WHY

Author JP Vijay umar


Date Sept 14 2012
Find an efficient query from the available options, under each CASE STUDY.
I had provided elapsed time, autotrace output, to aid readers in choosing:
CASE STUDY 01
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------I WANT TO FIND ALL THE SCHEMA OWNERS IN THE DB, ALONG WITH THEIR DEFAULT
TABLESPACES, EXCEPTING SCHEMA OWNERS WITH OBJECTS IN
('SYSTEM','SYSAUX','SYSTOOLS') TABLESPACES.
--FROM THE FOLLOWING TWO QUERIES, EXPLORE WHICH QUERY IS EFFICIENT AND WHY?
QUERY 01
--------------------------------------------------------------------------------------sqlplus /
SQL> set timing on
SQL> alter system flush shared_pool;
System altered.
Elapsed: 00:00:13.64
SQL> alter system flush buffer_cache;
System altered.
Elapsed: 00:00:01.46
SQL> select owner,default_tablespace from
(select distinct owner owner from dba_tables order by 1) a,
dba_users b
where a.owner = b.username
and b.default_tablespace NOT IN ('SYSTEM','SYSAUX','USERS','SYSTOOLS');
OWNER
DEFAULT_TABLESPACE
------------------------------ -----------------------------........................
........................
7 rows selected.
Elapsed: 00:00:02.42
QUERY 02
--------------------------------------------------------------------------------------SQL> alter system flush shared_pool;
System altered.
Elapsed: 00:00:01.24

SQL> alter system flush buffer_cache;


System altered.
Elapsed: 00:00:01.18
SQL> select owner,default_tablespace from
(select distinct owner owner from dba_tables order by 1) a,
(select username,default_tablespace from dba_users
where default_tablespace NOT IN ('SYSTEM','SYSAUX','USERS','SYSTOOLS') order by
1) b
where a.owner = b.username order by 1;
OWNER
DEFAULT_TABLESPACE
------------------------------ -----------------------------........................
........................
7 rows selected.
Elapsed: 00:00:01.23
CASE STUDY 02
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------I WANT TO FIND ALL THE USERS IN THE DB WITH THE DB_NAME
--OUT OF THE TWO SQL QUERIES, I PRESENTED HERE, FIND OUT THE EFFICIENT QUERY.
QUERY 01
--------------------------------------------------------------------------------------SQL> alter system flush shared_pool;
System altered.
Elapsed: 00:00:00.36
SQL> alter system flush buffer_cache;
System altered.
Elapsed: 00:00:00.76
SQL> select username,(select name from v$database) from dba_users order by 1;
USERNAME
(SELECTNA
------------------------------ --------........................
........................
88 rows selected.
Elapsed: 00:00:00.45
QUERY 02
---------------------------------------------------------------------------------------

SQL> alter system flush shared_pool;


System altered.
Elapsed: 00:00:00.06
SQL> alter system flush buffer_cache;
System altered.
Elapsed: 00:00:00.48
SQL> select username,d.name from dba_users,
(select name from v$database) d order by 1;
USERNAME
NAME
------------------------------ --------........................
........................
88 rows selected.
Elapsed: 00:00:00.32
CASE STUDY 03
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------I WANT TO FIND ALL THE USERS, WITH RESOURCE ROLE GRANTED DIRECTLY OR
THROUGH ANOTHER ROLE
--THE FOLLOWING PL/SQL PROCEDURE GET ME THE REQUIRED INFO
set serverout on size 1000000 timing on
declare
begin
for c1 in (select grantee, granted_role from dba_role_privs,
(select username from dba_users) d
where granted_role='RESOURCE_IIP'
and grantee = d.username order by 1) loop
dbms_output.put_line(c1.grantee||' '||c1.granted_role||' role granted directly
**');
end loop;
for c2 in (select grantee,granted_role from dba_role_privs,
(select username from dba_users) d
where grantee = d.username
and granted_role NOT IN ('RESOURCE_IIP')
AND granted_role IN (select grantee from dba_role_privs
where grantee IN (select role from dba_roles)
and granted_role IN ('RESOURCE_IIP'))
order by grantee) loop
dbms_output.put_line(c2.grantee||' RESOURCE_IIP role granted indirectly through
role '||
c2.granted_role||' ##');
end loop;
end;
/

----------------------------------------------------------------------------------------THE SAME INFO IS OBTAINED FROM THIS SQL SCRIPT ALSO


set echo off feedbac off linesize 120 pagesize 0
column grantee format a20
column granted_role format a30
column message format a20
select grantee,granted_role,
decode(granted_role,'RESOURCE_IIP','GRANTED ROLE DIRECTLY','GRANTED ROLE
INDIRECTLY') mssg
from dba_role_privs,(select username from dba_users) u
where grantee = u.username
and (granted_role = ('RESOURCE_IIP')
or
granted_role in (select grantee from dba_role_privs,
(select role from dba_roles) r
where granted_role='RESOURCE_IIP' and grantee=r.role))
order by grantee,granted_role

CASE STUDY 04
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------HOW TO FIND MISSING PARENT KEYS?
I had three solutions to find the missing parent

eys:

create table temp_jp1(col1 number,col2 varchar2(20));


set serverout on size 1000000 timing on
declare
begin
for i in 1..1000 loop
insert into temp_jp1 values(i,'Sai Vee sha');
end loop;
end;
/
create table temp_jp2 as select * from temp_jp1;
delete from temp_jp2 where col1=888;
commit;
I HAD DELETED ONE RECORD FROM TEMP_JP2 TABLE.
METHOD 01
--------------------------------------------------------------------------------------alter system flush shared_pool;
alter system flush buffer_cache;
set timing on autotrace on linesize 140

select * from temp_jp1


where col1 NOT IN (select col1 from temp_jp2);
COL1 COL2
---------- -------------------888 Sai Vee sha
Elapsed: 00:00:00.12
Execution Plan
---------------------------------------------------------Plan hash value: 3964985480
------------------------------------------------------------------------------| Id | Operation
| Name
| Rows | Bytes | Cost (%CPU)| Time
|
------------------------------------------------------------------------------| 0 | SELECT STATEMENT |
| 968 | 24200 |
66 (0)| 00:00:01 |
|* 1 | FILTER
|
|
|
|
|
|
| 2 | TABLE ACCESS FULL| TEMP_JP1 | 1000 | 25000 |
3 (0)| 00:00:01 |
|* 3 | TABLE ACCESS FULL| TEMP_JP2 | 949 | 12337 |
2 (0)| 00:00:01 |
------------------------------------------------------------------------------Predicate Information (identified by operation id):
--------------------------------------------------1 - filter( NOT EXISTS (SELECT 0 FROM "TEMP_JP2" "TEMP_JP2" WHERE
LNNVL("COL1"<>:B1)))
3 - filter(LNNVL("COL1"<>:B1))
Note
----- dynamic sampling used for this statement
Statistics
---------------------------------------------------------1051 recursive calls
0 db bloc gets
3626 consistent gets
27 physical reads
0 redo size
578 bytes sent via SQL*Net to client
492 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
18 sorts (memory)
0 sorts (dis )
1 rows processed
METHOD 02
--------------------------------------------------------------------------------------alter system flush shared_pool;
alter system flush buffer_cache;
set timing on autotrace on linesize 140
select * from temp_jp1 a
where NOT EXISTS (select 'X' from temp_jp2
where col1 = a.col1);
COL1 COL2

---------- -------------------888 Sai Vee sha


Elapsed: 00:00:00.06
Execution Plan
---------------------------------------------------------Plan hash value: 2895676727
-------------------------------------------------------------------------------| Id | Operation
| Name
| Rows | Bytes | Cost (%CPU)| Time
|
-------------------------------------------------------------------------------| 0 | SELECT STATEMENT
|
|
1 |
38 |
7 (15)| 00:00:01
|
|* 1 | HASH JOIN RIGHT ANTI|
|
1 |
38 |
7 (15)| 00:00:01
|
| 2 | TABLE ACCESS FULL | TEMP_JP2 | 999 | 12987 |
3 (0)| 00:00:01
|
| 3 | TABLE ACCESS FULL | TEMP_JP1 | 1000 | 25000 |
3 (0)| 00:00:01
|
-------------------------------------------------------------------------------Predicate Information (identified by operation id):
--------------------------------------------------1 - access("COL1"="A"."COL1")
Note
----- dynamic sampling used for this statement
Statistics
---------------------------------------------------------478 recursive calls
0 db bloc gets
86 consistent gets
18 physical reads
0 redo size
578 bytes sent via SQL*Net to client
492 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
12 sorts (memory)
0 sorts (dis )
1 rows processed
METHOD 03
--------------------------------------------------------------------------------------alter system flush shared_pool;
alter system flush buffer_cache;
set timing on autotrace on linesize 140
select a.col1,a.col2,b.col1

from temp_jp1 a, temp_jp2 b


where a.col1 = b.col1(+) and b.col1 is null;
COL1 COL2
COL1
---------- -------------------- ---------888 Sai Vee sha
Elapsed: 00:00:00.07
Execution Plan
---------------------------------------------------------Plan hash value: 1282405407
---------------------------------------------------------------------------------| Id | Operation
| Name
| Rows | Bytes | Cost (%CPU)| Time
|
---------------------------------------------------------------------------------| 0 | SELECT STATEMENT
|
|
1 |
38 |
7 (15)| 00:00:0
1 |
|* 1 | FILTER
|
|
|
|
|
|
|* 2 | HASH JOIN RIGHT OUTER|
|
1 |
38 |
7 (15)| 00:00:0
1 |
| 3 |
TABLE ACCESS FULL | TEMP_JP2 | 999 | 12987 |
3 (0)| 00:00:0
1 |
| 4 |
TABLE ACCESS FULL | TEMP_JP1 | 1000 | 25000 |
3 (0)| 00:00:0
1 |
---------------------------------------------------------------------------------Predicate Information (identified by operation id):
--------------------------------------------------1 - filter("B"."COL1" IS NULL)
2 - access("A"."COL1"="B"."COL1"(+))
Note
----- dynamic sampling used for this statement
Statistics
---------------------------------------------------------510 recursive calls
2 db bloc gets
88 consistent gets
20 physical reads
0 redo size
641 bytes sent via SQL*Net to client
492 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
12 sorts (memory)
0 sorts (dis )
1 rows processed

Вам также может понравиться