Information Needed in Tuning A SQL Query
Information Needed in Tuning A SQL Query
Information Needed in Tuning A SQL Query
The point of this document is NOT to teach you how to tune SQL; it is to help you collect the information you need
in order to tune or have someone else help you tune SQL.
In order to tune a SQL Query, specific pieces of information are needed. Some of this is obvious yet all of us can point to hundreds of
posts on the WWW where someone has asked for help tuning a SQL Statement and then for example,they did not actually post the
statement. How does one tune a query when one has never seen it?
So to improve the overall state of affairs in the SQL Tuning space, here are the details of what information is needed to tune a problem
query. I will provide SQL Scripts that gather most of this information needed to tune a SQL Query, so as to make it easier for you to
provide this information in your discussions with others.
If you are interested in improving your own SQL Tuning skills, then I refer you to my book: Oracle SQL Performance Tuning
and Optimization: It’s all about the Cardinalities. This can be found on Amazon (Ctrl+Click to follow the link).
http://www.amazon.com/Oracle-Performance-Tuning-Optimization-Cardinalities/dp/1501022695
This book like most books, being relevant to your career development, islikely covered under your company’s reimbursement
policy. So if you want the book, ask your Boss if you can get it and expense it. After your boss gives you verbal approval,
buy it from Amazon and submit an expense report to get your money back. There is nothing wrong with letting your company
invest a few more dollars in your professional growth, especially since you will be spending your personal time reading this
book to improve skills which they will be quick to exploit.
In addition, the first chapter of the book is FREE,and the scripts from the book are FREE too.You do not need to buy the book
to get these. You can get them from this link below. Reading the first chapter before you buy the book and using the scripts a
little, are both good ways to satisfy yourself and your Boss that the investment is worth it. Go here to OraFAQ to download
them.
http://www.orafaq.com/forum/m/634324/#msg_634324
So, if you are planning on doing a tuning session or asking someone else to do a tuning session with you or for you, then you will need
at some point to gather together the following bits of information.
First there is the BASIC information that is needed to understand the problem space. This basic information gives us an appreciation
of the performance issue, and what some potential problems might be. For example, this tell us the join order of tables and lets us
judge if this order is a good one or not. Then once an understanding of the query is had, additional information may be needed in
order to suggest possible fixes. For example, maybe cardinalities in the plan suggest indexes would help. If so, then we would need
first to check the list of indexes that already exist on the plan’s tables.Next, for very difficult problems, it may be necessary to monitor
specific aspects of an actively running query or consult AWR data for the same data only historical. For example, we might want to
check the efficiency of hash joins to see if they are OPTIMAL or ONE-PASS or if they are the potentially problematic MULTI-PASS.
So we need to run the query and monitor its temp space usage, or check AWR for historical usage data. Last, we may want additional
documentary types of information to make explaining things easier to others. For example, a data model and query diagram are
pictures which provide good artifacts for you to talk to when explaining how you solved the problem.
With this information, most Oracle Professionals can engage in a meaningful discourse over; what problems might exist, what
possible strategies and solutions might be appropriate, and ultimately -- how a query can be made faster.
DESCRIPTION: Shows basic employment status of every employee at all active locations.
4 - access("EMP_GID"="EL"."EMP_LOC_GID")
5 - filter("PRTY_REF_ID_RNK"=1)
6 - filter(RANK() OVER ( PARTITION BY "GRP_BEN_CASE_ID","PRTY_REF_ID" ORDER BY CASE
"EE"."EMPLMT_STAT_CD" WHEN 'T' THEN 2 WHEN 'R' THEN 2 WHEN 'D' THEN 2 WHEN 'I' THEN 2 ELSE 1 END
,INTERNAL_FUNCTION("EE"."SRCE_EFF_START_TMSP") DESC ,INTERNAL_FUNCTION("EE"."EMP_PK_ID") DESC )<=1)
7 - filter(SYS_CONTEXT('APP1','CURRENT_SCHEMA')='CLDW_THPA_DLV1')
8 - storage("EE"."SRCE_EFF_END_TMSP"=TIMESTAMP' 9999-12-31 00:00:00' AND
"EE"."SRCE_APP_SYS_CD"='ELIG')
filter("EE"."SRCE_EFF_END_TMSP"=TIMESTAMP' 9999-12-31 00:00:00' AND
"EE"."SRCE_APP_SYS_CD"='ELIG')
9 - storage("EL"."POPULATION_STATUS_CD"<>'D')
filter("EL"."POPULATION_STATUS_CD"<>'D')
11 - access("A"."GRP_BEN_CASE_ID"="B"."GRP_BEN_CASE_ID")
14 - filter(SYS_CONTEXT('APP1','CURRENT_SCHEMA')='CLDW_THPA_DLV1')
15 - storage("E"."EMPLR_LOC_PK_ID"<>(-1) AND "E"."EMPLR_LOC_PK_ID"<>(-2) AND
"E"."SRCE_EFF_END_TMSP"=TIMESTAMP' 9999-12-31 00:00:00')
filter("E"."EMPLR_LOC_PK_ID"<>(-1) AND "E"."EMPLR_LOC_PK_ID"<>(-2) AND
"E"."SRCE_EFF_END_TMSP"=TIMESTAMP' 9999-12-31 00:00:00')
19 - storage("EE"."SRCE_EFF_END_TMSP"=TIMESTAMP' 9999-12-31 00:00:00' AND
"EE"."POPULATION_STATUS_CD"<>'D' AND "EE"."EMP_PK_ID"<>(-1) AND "EE"."EMP_PK_ID"<>(-2))
filter("EE"."SRCE_EFF_END_TMSP"=TIMESTAMP' 9999-12-31 00:00:00' AND
"EE"."POPULATION_STATUS_CD"<>'D' AND "EE"."EMP_PK_ID"<>(-1) AND "EE"."EMP_PK_ID"<>(-2))
21 - storage("EL"."SRCE_EFF_END_TMSP"=TIMESTAMP' 9999-12-31 00:00:00' AND
"EL"."POPULATION_STATUS_CD"<>'D')
filter("EL"."SRCE_EFF_END_TMSP"=TIMESTAMP' 9999-12-31 00:00:00' AND
"EL"."POPULATION_STATUS_CD"<>'D')
Note
-----
- dynamic sampling used for this statement (level=4)
- automatic DOP: Computed Degree of Parallelism is 1
@genfrpspreadsheetcode411g.sql
Plan Filtered Actual
ID TABLE_NAME NUM_ROWS ROWCOUNT Cardinality Cardinality FRP
----- ------------- ---------- ---------- ----------- ----------- ------
8 EMP_DIM 6243035 6243035 240117 215414 3.5
9 EMP_LOC_DIM 329699 329699 296337 329699 100.0
15 EMPLR_LOC_DIM 8874 8874 8874 8872 100.0
19 EMP_DIM 6243035 6243035 240117 236469 3.8
21 EMP_LOC_DIM 329699 329699 251761 212993 64.6
5 rows selected.
Given the above information, people can see the important parts of your problem and start to answer significant questions like:
After reviewing this basic information, additional information might be relevant. This could include any of the above, which will be
used to consider alternative ideas that will allow you to change something to make your query go faster.
@showplantables11g
5 rows selected.
@showplantablesunique11g
OBJECT_OWNER OBJECT_NAME
------------------------------ ------------------------------
SCOTT EMPLR_LOC_DIM
SCOTT EMP_DIM
SCOTT EMP_LOC_DIM
3 rows selected.
@showplanindexesunique11g
3 rows selected.
@showplanindexes11g
3 rows selected.
Elapsed: 00:00:00.14
@showplanconstraints11g
2 rows selected.
I apologize for the line wraps here. Just not much to do about it if the artifact is going to show enough for you to understand it. The
script prints better in SQL*Plus.
@showcolstats SCOTT EMPLR_LOC_DIM
22 rows selected.
I had to use a different schema here since the objects from the view we are using as our example here, did not have any histograms.
@showhistogram dbsnmp bsln_baselines instance_name
4 rows selected.
Elapsed: 00:00:00.13
43 rows selected.
With the above information, SQL Tuners can make comparisons between what the plan is using, and what the plan could be using,
and what they think might be missing that the plan could have used. They can answer questions like these.
When documenting your results, or when providing context for others, these pictures provide good references. The help people
visualize where your query sits with respect to the entire database. Perspective is usually a good thing.
So as you can see, it is not difficult at all to acquire the information needed for a tuning session, when you use the scripts. This
information will help you and those who assist you, to tune you problem SQL. Please refer back to page 1 above for the location of
the FREE scripts.
Good luck, and remember to provide this information whenever you are asking for help.
Kevin Meade