You are on page 1of 8

Query/SQL Tuning

1. Identify and collect Missing stats based on Join columns, PI/SI Indexes, and filter columns............................2
2. Identify and Refresh Old/Obsolete (more than 10% volume change) and incorrect stats on tables................2
3. Collect statistics on VALUE ORDERED INDEXED columns and DATE columns after loading data.......................3
4. Verify DDL for table - check PIs PI joins are better.........................................................................................3
5. Sample SQL to check for data skew based on PI and joins................................................................................3
6. Look into filtering the data, avoiding functions in joins, changing joins, and splitting the query......................3
7. Sample SQL - Database and Account settings...................................................................................................3
8. Sample SQL - View DDLs for table and view.....................................................................................................4
9. Sample SQL - Space by Database......................................................................................................................4
10. Sample SQL - Perm Space, Skew Ness, LastAccessDate, and LastAlterDate by Table...................................4
11. Sample SQL - Indexes by Table......................................................................................................................4
12. Sample SQL - Database Logs.........................................................................................................................4
13. Sample SQL - Skew ness by Spool Space.......................................................................................................5
14. Sample SQL - High CPU usage and Skew ness by CPU Usage........................................................................5
15. Sample SQL - High IO usage and Skew ness by IO Usage View DDLs for table and view...............................6
16. Sample SQL - Verify roles..............................................................................................................................7
17. Sample SQL - Verify Accounts/Users to which a Role is granted...................................................................7
18. Sample SQL - Verify Roles granted to an Account/User................................................................................7
19. Sample SQL - Verify privileges of a role (database/table level access to roles).............................................7
20. Sample SQL - Verify access granted to users without using roles (direct database/table level privileges)....7
21. Sample SQL - Verify access granted to users using Roles..............................................................................7
22. Sample SQL - Verify Objects created after DDL implementation View DDLs for table and view...................8
23. Sample SQL - Verify data distribution by amp based on existing PI or proposed PI......................................8

Last Updated: Apr 12, 2011 Page 1


1. Identify and collect Missing stats based on Join columns, PI/SI Indexes, and
filter columns.
Example:
diagnostic helpstats on for session;

explain
<SQL>

BEGIN RECOMMENDED STATS ->


21) "COLLECT STATISTICS GLOBAL_SVC_T.SVC_INTERACTION_COMMENT COLUMN SVC_AGENT_ID". (HighConf)
23) "COLLECT STATISTICS GLOBAL_SVC_T.SVC_INTERACTION COLUMN LOAD_SEQ_NUM". (HighConf)
24) "COLLECT STATISTICS GLOBAL_SVC_T.SVC_INTERACTION COLUMN (LOAD_SEQ_NUM , SVC_LEVEL_ID)". (HighConf)
25) "COLLECT STATISTICS GLOBAL_SVC_T.SVC_INTERACTION COLUMN (LOAD_SEQ_NUM ,
SVC_INTERACTION_REASON_ID)". (HighConf)
26) COLLECT STATISTICS "APJ_FIN_T"."ORDER_HEADER_AJ" INDEX ("ORDER_NUM","BUSINESS_UNIT_ID")". (HighConf)
<- END RECOMMENDED STATS

Collect all single column stats recommended.


Collect stats for all Indexed columns.
Collect multi-column stats by verifying explain plan changes that improve run-time only (Contact Project DBA
/ On-call DBA to drop stats).
a. Call sysdba.colstats('GLOBAL_SVC_T','SVC_INTERACTION_COMMENT','SVC_AGENT_ID');
b. Call sysdba.colstats('GLOBAL_SVC_T','CUSTOMER_PRODUCT_LOCATION','CUSTOMER_PRODUCT_ID');
c. Call sysdba.colstats('GLOBAL_SVC_T','SVC_INTERACTION','LOAD_SEQ_NUM');

2. Identify and Refresh Old/Obsolete (more than 10% volume change) and
incorrect stats on tables.
help stats MARCOM_INC.DM_PROMO_HIST;

Old stats:
Date Time Unique values Column Names
09/03/03 00:36:59 0 COMMUNICATION_ID,MARCOM_VENDOR_ID
New stats:
09/12/16 00:18:44 5,141,919 COMMUNICATION_ID,MARCOM_VENDOR_ID

select count(*) from MARCOM_INC.DM_PROMO_HIST;

Last Updated: Apr 12, 2011 Page 2


--5,141,919
select * from dba_tools.Stats_info_TD13_64bit
where trim(databasename)||'.'||trim(tablename)
in ('FIN_SVC_inc_t.rvlvg_acct_tmp1',
'FIN_SVC_inc_t.rvlvg_acct_tmp2',
'FIN_SVC_inc_t.rvlvg_acct_tmp3',
'FIN_SVC_inc_t.RVLVG_ACCT',
'FIN_SVC_BASE_T.RVLVG_ACCT',
'FIN_SVC_PKG_T.RVLVG_ACCT_DTL',
'FIN_SVC_PKG_T.RVLVG_ACCT_FIN_PER_END_XREF',
'FIN_SVC_PKG_T.RVLVG_ACCT_PER_END_XREF',
'FIN_SVC_BASE_T.RVLVG_ACCT',
'FIN_SVC_BASE_T.RVLVG_ACCT_FIN',
'FIN_SVC_BASE_T.UDT_LD_PCTID_DIM');

3. Collect statistics on VALUE ORDERED INDEXED columns and DATE columns


(if used in the joins or where clause) after loading data.

4. Verify DDL for table - check PIs PI joins are better.


Example: show table CE_MART_INC_T.CE_RR_CALC_SUBSQ_CALL_AMER_0;
show table CE_MART_INC_T.CE_RR_CALC_SUBSQ_CALL_AMER;

5. Sample SQL to check for data skew based on PI and joins


select hashamp(hashbucket(hashrow(SVC_TAG_ID))), count(*)
from CE_MART_INC.CE_RR_CALC_SUBSQ_CALL_AMER
group by 1
order by 2 desc;

Example:
HASHAMP(HASHBUCKET(HASHROW(SVC_TAG_ID))) Count(*)
---------------------------------------- -----------
166 120808
104 82918
50 62066
305 25793
16 21117
261 16997
300 11208
191 6881
219 6546
21 5617
299 4472

6. Look into filtering the data, avoiding functions in joins, changing joins, and
splitting the query.

Last Updated: Apr 12, 2011 Page 3


7. Sample SQL - Database and Account settings

select DatabaseName (format 'x(26)'),


OwnerName (format 'x(18)'),
AccountName (format 'x(12)'),
PermSpace, SpoolSpace, TempSpace
from dbc.databases where databasename like '%SRA%'
order by 3, 1;

8. Sample SQL - View DDLs for table and view


show select COMN_BASE.GBL_QTE_STAT.*;

9. Sample SQL - Space by Database

select DatabaseName, sum(CurrentPerm), sum(MaxPerm)


from DBC.DiskSpace
where trim(databasename) like '%DFSDWIR%\_T' escape '\'
group by 1;

10. Sample SQL - Perm Space, Skew Ness, LastAccessDate, and LastAlterDate
by Table
select * from DBA_TOOLS.TableSpace where databasename in ('DFSDWIR_T','DFSDWIR_INC_T')
order by 1,2;

select * from DBA_TOOLS.TableSpace


where trim(databasename)||'.'||trim(tablename)
in ('SLS_APJ_INC_t.QTE_DTL_PART_AJ_STG',
'SLS_APJ_INC_t.QTE_DTL_ITM_DIM_STG',
'SLS_APJ_INC_T.SLS_QTE_STAT_HIST',
'SLS_BASE_T.SLS_TXN_AGNT_ASSOC',
'SLS_BASE_T.SLS_QTE',
'itm_pkg_t.BASE_PROD_HIER_DIM',
'APJ_FIN_T.PART_AJ')
order by 1,2;

11. Sample SQL - Indexes by Table


select * from DBA_TOOLS.Indexes
where trim(databasename)||'.'||trim(tablename)
in ('SLS_APJ_INC_t.QTE_DTL_PART_AJ_STG',
'SLS_APJ_INC_t.QTE_DTL_ITM_DIM_STG',
'SLS_APJ_INC_T.SLS_QTE_STAT_HIST',
'SLS_BASE_T.SLS_TXN_AGNT_ASSOC',
'SLS_BASE_T.SLS_QTE',
'itm_pkg_t.BASE_PROD_HIER_DIM',
'APJ_FIN_T.PART_AJ')
order by 1,2;

Last Updated: Apr 12, 2011 Page 4


12. Sample SQL - Database Logs
select * from pdcrinfo.qrylog_hst
where querytext like '%collect%SLS_QTE_LN%'
and logdate >=date -60;

select * from dbc.qrylog


where querytext like '%collect%SLS_QTE_LN%'
and logdate >=date -60;

13. Sample SQL - Skew ness by Spool Space


select databasename (format 'x(26)') as Databasename
,maxpeakVprocSpool*numVprocs (format 'ZZZZ,ZZZ,ZZZ,ZZ9') as ReqdSpoolDueToskew
,sum(peakspool) (format 'ZZZZ,ZZZ,ZZZ,ZZ9') as TotalPeakSpool
,max(peakspool) (format 'ZZZZ,ZZZ,ZZZ,ZZ9') as MaxPeakVprocSpool
,TotalPeakSpool/numVprocs (format 'ZZZZ,ZZZ,ZZZ,ZZ9') as AvgPeakVprocSpool
,(case when AvgPeakVprocspool=0 then 0 else maxPeakVprocSpool/AvgPeakVprocspool end)
(format 'ZZZ,ZZ9') as PeakSkewFact
,count(*) (format 'ZZZ,ZZ9') as NumVprocs
from dbc.diskspace
where databasename ='SERVICE_SRA_ETL'
group by databasename;

select a.LogDate
,a.UserName
,a.AccountName
,a.PEAKSPOOL
,a.PEAKSPOOLSKEW
,(a.PeakSPOOL/800) AvgVProcSpool
,( (a.PeakSPOOL/800) / (1- (peakspoolskew/100)) ) MaxVprocspool
,(MaxVprocspool*800) SpoolReqDueToSkew
,a.MAXSPOOL
from pdcrinfo.Spoolspace_Hst a
where a.username='SERVICE_SRA_ETL'
and LogDate between '2010-08-01' and '2010-09-12'
order by 1 desc;

14. Sample SQL - High CPU usage and Skew ness by CPU Usage
SEL
LogDate,
UserName,
SessionID,
RequestNum,
AMPCPUTime,
MaxAMPCPUTime,
MinAMPCPUTime,
(AMPCPUTime/800) as AvgCPUTime,
(100 - ((AvgCPUTime/NULLIFZERO(MaxAMPCPUTime) )*100) ) as CPUSkewFactor,
substring(querytext from 1 for 100)
FROM PDCRINFO.DBQLogTbl

Last Updated: Apr 12, 2011 Page 5


WHERE username in ('SERVICE_SRA_ETL')
and LogDate between '2010-08-09' and '2010-08-15'
and AMPCPUTime >800
and CPUSkewFactor>20
group by 1,2,3,4,5,6,7,8,10
order by 9 desc;

SEL
substring(querytext from 1 for 100) as QueryText100,
Avg(100 - (((AMPCPUTime/800)/NULLIFZERO(MaxAMPCPUTime) )*100)) as AvgCPUSkewFactor,
count(*) as QryCount,
Max(LogDate) as MaxLogDate,
Min(LogDate) as MinLogDate,
Avg(AMPCPUTime) as AvgAMPCPUTime,
Avg(MaxAMPCPUTime) as AvgMaxAMPCPUTime,
Avg(MinAMPCPUTime) as AvgMinAMPCPUTime,
Avg((AMPCPUTime/800)) as AvgAvgCPUTime
FROM PDCRINFO.DBQLogTbl
WHERE username in ('SERVICE_SRA_ETL')
and LogDate between '2010-08-01' and '2010-09-12'
and AMPCPUTime >800
and (100 - (((AMPCPUTime/800)/NULLIFZERO(MaxAMPCPUTime) )*100) ) >30
group by 1

order by 1,2,3,4 desc;

15. Sample SQL - High IO usage and Skew ness by IO Usage View DDLs for
table and view
SEL
LogDate,
UserName,
SessionID,
RequestNum,
TotalIOCount,
MaxAMPIO,
MinAMPIO,
(TotalIOCount/800) as AvgIO,
(100 - ((AvgIO/NULLIFZERO(MaxAMPIO) )*100) ) as IOSkewFactor,
substring(querytext from 1 for 100)
FROM PDCRINFO.DBQLogTbl
WHERE username in ('SERVICE_SRA_ETL')
and LogDate between '2010-08-09' and '2010-08-15'
and TotalIOCount >100000
and IOSkewFactor>20
group by 1,2,3,4,5,6,7,8,10
order by 9 desc;

SEL
substring(querytext from 1 for 100) as QueryText100,

Last Updated: Apr 12, 2011 Page 6


Avg(100 - (((TotalIOCount/800)/NULLIFZERO(MaxAMPIO) )*100)) as AvgIOSkewFactor,
count(*) as QryCount,
Max(LogDate) as MaxLogDate,
Min(LogDate) as MinLogDate,
Avg(TotalIOCount) as AvgTotalIOCount,
Avg(MaxAMPIO) as AvgMaxAMPIOCount,
Avg(MinAMPIO) as AvgMinAMPIOCount,
Avg((TotalIOCount/800)) as AvgAvgAMPIOCount
FROM PDCRINFO.DBQLogTbl
WHERE username in ('SERVICE_SRA_ETL')
and LogDate between '2010-08-01' and '2010-09-12'
and TotalIOCount >100000
and (100 - (((TotalIOCount/800)/NULLIFZERO(MaxAMPIO) )*100) ) >30
group by 1
order by 1,2,3,4 desc;

16. Sample SQL - Verify roles


select rolename from dbc.roles where rolename like 'role_dss%';

select rolename from dbc.roles where rolename like '%DFSDWIR%' order by 1;

17. Sample SQL - Verify Accounts/Users to which a Role is granted


select RoleName,Grantee,DefaultRole,WithAdmin
from dbc.RoleMembers
where RoleName in ('role_dss_req', 'role_dss_ud_app');
order by RoleName,Grantee,DefaultRole,WithAdmin ;

18. Sample SQL - Verify Roles granted to an Account/User


select RoleName,Grantee,DefaultRole,WithAdmin
from dbc.RoleMembers where Grantee in ('GIS_ADMIN')
order by RoleName,Grantee,DefaultRole,WithAdmin ;

19. Sample SQL - Verify privileges of a role (database/table level access to


roles)
select * from dba_tools.GIS_AllRoleRights where RoleName in ('role_dss_req', 'role_dss_ud_app');

select * from dba_tools.GIS_AllRoleRights where databasename in ('GBL_CAMP_MGMT', 'FINANCE');

select rolename, databasename from dba_tools.GIS_AllRoleRights


where databasename in ('euro_umlt','emea_tel','emea_tel_anom')
and trim(rolename) like '%\_s' escape '\'
group by 1,2;

select * from dba_tools.GIS_AllRoleRights


where databasename ='FINANCE'
and tablename in ('EMAIL_NOTIFICATION', 'all');

Last Updated: Apr 12, 2011 Page 7


20. Sample SQL - Verify access granted to users without using roles (direct
database/table level privileges)
select * from dba_tools.GIS_AllRights where username='GIS_ADMIN';

21. Sample SQL - Verify access granted to users using Roles


select * from dba_tools.GIS_AllRoleRights where databasename in ('GIS_ADMIN');

22. Sample SQL - Verify Objects created after DDL implementation View
DDLs for table and view
select databasename (format 'x(18)') , tablename (format 'x(27)') as ObjectName,
(case when TableKind ='T' then 'table'
when TableKind ='V' then 'view'
when TableKind ='M' then 'macro'
when TableKind ='P' then 'Procedure'
else null
end) as ObjectKind,
lastaltertimestamp, createtimestamp
from dbc.tables
where ( cast(createtimestamp as date) =date or cast(LastAlterTimeStamp as date) =date )
and trim(databasename) like 'DFSDWIR%'
order by 1,2,3,4;

23. Sample SQL - Verify data distribution by amp based on existing PI or


proposed PI (assuming data is populated)

select HASHAMP(HASHBUCKET(HASHROW(PI columns))), count(*)


from DatabaseName.TableNAme
group by 1
order by 2;

Ex:
select HASHAMP(HASHBUCKET(HASHROW(SVC_TAG_ID ,ORDER_BUSINESS_UNIT_ID))),count(*)
from GBL_APOS_MART_INC_T.APOS_EMEA_4_FINAL
group by 1
order by 2;

Last Updated: Apr 12, 2011 Page 8

You might also like