Showing posts with label parallel processing. Show all posts
Showing posts with label parallel processing. Show all posts

Saturday, 2 May 2015

DBMS_XPLAN can lie!

disclaimer: here

"Don't trust what you see!"

A cliche in spy movies, this sentence can also have a less impressive interpretation: "Don't be over-excited by any finding!"

I came to believe in this interpretation when I was studying for Oracle SQL Tuning Expert exam.

Going through experiments on using different DOP for INSERT...SELECT statement (a more comprehensive post coming up soon), I came across something which looked like a behavior change between 11.2.0.4 and 12.1.0.2 and -more importantly- seemed to contradict the documentation.

All I heard in my brain was this: "Shit! This is going to put me on the map! OakTable! Here I come!"

I got in touch with Martin Widlake, who was kind enough to go through the results of my experiment, and advised me to publish the results -which I'm extremely grateful for it.

DOP in INSERT..SELECT


Just for clarification, I'd like to rewrite the rule for calculation of DOP in INSERT..SELECT as below:

After the decision to parallelize the SELECT or INSERT operation is made, one parallel directive is picked for deciding the DOP of the whole statement, using the following precedence rule:

1.       Insert hint directive
2.       then Session
3.       then Parallel declaration specification of the inserting table
4.       and then Maximum query directive.

Oracle states: "In this context, maximum query directive means that among multiple tables and indexes, the table or index that has the maximum DOP determines the parallelism for the query operation."

(You can find 11gR2's original documentation here and 12.1.0.2's here.)

But the results of the experiment below in 12.1.0.2 appears to suggest that rule 4 overrides the rule 3, in this version.

Tables:

SQL> CREATE TABLE SOURCE_DATA NOLOGGING PARALLEL 4 AS
SELECT ROWNUM ID , MOD(ROWNUM,5) PART_ID, ROUND(DBMS_RANDOM.VALUE(1,ROWNUM)*5,5) VALUE
FROM DUAL
CONNECT BY LEVEL < 1000001;

SQL> EXEC DBMS_STATS.GATHER_TABLE_STATS(USER, 'SOURCE_DATA');

SQL> CREATE TABLE DESTINATION_DATA AS
SELECT * FROM SOURCE_DATA WHERE 1=2;

Session
:

SQL> ALTER SESSION ENABLE PARALLEL DML;


The experiment


SQL> ALTER TABLE DESTINATION_DATA PARALLEL 3;

As you can see below, there are no hints on the statement, and Parallel DML is merely enabled on he session. We are doing a simple direct-path insert into a table with parallel declaration of 3, and selecting from a table which has a parallel declaration of 4. So based on Oracle documentation we expect the DOP to be 3 (i.e. "Parallel declaration specification of the inserting table") despite the higher DOP on the source table.

Using DBMS_XPLAN.DISPLAY_CURSOR, we get:

PLAN_TABLE_OUTPUT                                                                             
-----------------------------------------------------------------------------------------------
SQL_ID  cwrqam2gbx7ds, child number 0                                                         
-------------------------------------                                                         
INSERT /*+ append TEST3 */ INTO DESTINATION_DATA SELECT * FROM                                
SOURCE_DATA                                                                                   
                                                                                              
Plan hash value: 2115782672                                                                   
                                                                                              
-----------------------------------------------------------------------------------------------
| Id  | Operation                          | Name        | Rows  |    TQ  |IN-OUT| PQ Distrib |
-----------------------------------------------------------------------------------------------
|   0 | INSERT STATEMENT                   |             |       |        |      |            |
|   1 |  PX COORDINATOR                    |             |       |        |      |            |
|   2 |   PX SEND QC (RANDOM)              | :TQ10000    |  1000K|  Q1,00 | P->S | QC (RAND)  |
|   3 |    LOAD AS SELECT (HYBRID TSM/HWMB)|             |       |  Q1,00 | PCWP |            |
|   4 |     OPTIMIZER STATISTICS GATHERING |             |  1000K|  Q1,00 | PCWP |            |
|   5 |      PX BLOCK ITERATOR             |             |  1000K|  Q1,00 | PCWC |            |
|*  6 |       TABLE ACCESS FULL            | SOURCE_DATA |  1000K|  Q1,00 | PCWP |            |
-----------------------------------------------------------------------------------------------
                                                                                              
Predicate Information (identified by operation id):                                           
---------------------------------------------------                                           
                                                                                              
   6 - access(:Z>=:Z AND :Z<=:Z)                                                              
                                                                                              
Note                                                                                          
-----                                                                                         
   - Degree of Parallelism is 4 because of table property                                     
                                                                                              
 28 rows selected

WHAT?! DOP is 4?!

Not so fast though ... #sigh


During the comparison of the behavior between 11g and 12c, I kept checking the V$SQL.PX_SERVERS_EXECUTIONS, mainly because the DBMS_XPLAN.DISPLAY_CURSOR was not giving any hints on the employed DOP.

I hadn't done it for 12.1.0.2 because the output seemed to have enough information in that regard. But Martin Widlake suggested I do the same, to be consistent.

SQL> SELECT sql_id, child_number, PX_SERVERS_EXECUTIONS
FROM v$sql
WHERE sql_text LIKE '%TEST3%';

SQL_ID        CHILD_NUMBER PX_SERVERS_EXECUTIONS
------------- ------------ ---------------------
cwrqam2gbx7ds            0                     3

There you go! Oracle's documentation was not wrong after all. The DOP is in fact equal to the parallel declaration on the inserting table (i.e. 3).

One last blow came from OEM. You can clearly see the parallel servers involved in the INSERT...SELECT.


Just a note that seeing only one set of parallel servers proves Oracle's statement that when the decision is made to do both INSERT & SELECT in parallel, ONE parallel directive is used to carry out the operation; i.e. both operation use the same DOP.


Conclusion


Don't be deceived by the glamor of the extra information which is presented in 12.1.0.2. DBMS_XPLAN can lie.

Here I have to mention that I had also sought Jonathan Lewis' opinion about the post.

I'd like to quote this from Jonathan's response, when I got back to him to say that I was wrong:

 "There's always one more thing to check."

Friday, 20 February 2015

Parallel Processing: the Damien Hirst of SQL Tuning Art World

After spending a decade of my life among people whom I eventually came to regard generally as imposters (i.e. journalists), I learned something important about myself: I'm a purist committed to "principles" and I hate bullshitters!

But even if you ignore the pointless self-glorification of the first paragraph, you must have understood from the title that I'm not a fan of Damien Hirst!

Taking The "SQL Tuning Expert" certification exam (1Z0-117) recently reminded me that perhaps Damien Hirst is not the only entity which -in my opinion- is placed in a context that it doesn't belong to; or AT LEAST maybe we shouldn't regard people like him as such "integral" parts of the art world.

The following is a review of subjects covered in the exam, and what was overemphasised.

Here It Goes!


Disclaimer: If you've found your way to this post hoping that you might find some exam dumps or samples, you can press "Back" on your browser RIGHT NOW!

My only aim is to share what I remember (I will update if/when I remember more) from the test about what needs to be learned so that an applicant can pass the exam to achieve "SQL Tuning Expert" certification, mainly because not even a sample test was provided by Oracle.

I divide this post to three parts:

  1. Main Subjects
  2. References and Resources
  3. A Demand for Keeping Things In Perspective

1. Main Subjects 


You can find the exam topics here, but I did not find it representative of the main emphasis points of the exam; so here's what the actual main (not all of them, and not in the order of emphasis in the exam!) topics:
  1. Reading Execution Plans
  2. Query Transformation
  3. Partition Pruning
  4. Parallel Processing
  5. DBMS_MONITOR/trcsess/tkprof 
  6. SQLTuningSets, SQL Tuning Advisor, and SQL Access Advisor
  7. SQL Profiles and SQL Plan Management

1.1. Reading Execution Plans


Obviously!

The plans that you come across in the exam are not complicated (especially the stats related issues, such as cardinality mismatches and manifestations of stale stats are generally obvious), and you must have seen them all, if you have gone through the Oracle documentation mentioned below under "References and Resources."

But:
  • You should be loving this stuff! If you don't like cracking the execution plans code, why on earth are you taking this exam anyway?!
  • You should be familiar with different interpretation methods of execution plans in terms of finding out the chronology of the events and processes involved.
  • More or less every SQL Tuning case that is mentioned in the official exam topic is referenced in the exam through an execution plan.

 

 1.2. Query Transformation

 

  • When do either of View Merging, Subquery Unnesting and Query Rewrite happen?
  • When either of those can't happen?
  • What are the signs of each taking place, in an execution plan?
  • When would you see a "Projection View" during View Merging?
  • How do you distinguish a Bitmap Join an Star Transformation?

 

1.3. Partition Pruning

 

  • What is the sign of Partition Pruning NOT taking place on a partitioned table, in execution plan?
  • Generally how can predicates affect partition pruning in different partition methods? for example what can cause a Full Table Scan of a partitioned table? What kind of predicates will facilitate partition pruning in Range, List, Hash, or System partitioned tables?
  • Full/Partial partition-wise joins: When will they happen in parallel?
  • You need to keep an eye on the "IN-OUT" column of the execution plans on parallel partition-wise joins and "S -> P" operations: not all the tables are read in parallel!
  • What is System Partitioning?

 

1.4. Parallel Processing

 

You need to know about the following concepts:

  • How do different values of PARALLEL_DEGREE_POLICY(AUTO|MANUAL|LIMITED) affect the behaviour of the database?
  • How the DOP for a SQL statement is calculated under either of the modes mentioned above?
  • Under which circumstances the Auto DOP is used?
  • How is DOP calculated in each mode?
  • In each mode of PARALLEL_DEGREE_POLICY  when a statement is run in parallel; i.e. using which syntax (i.e. hint) or using which parallel settings for the segments involved?
  • Under which conditions parallel SQL statements are queued?
  • How/when does the value of PARALLEL_MIN_TIME_THRESHOLD affects the execution of a SQL statement?
  • Under which circumstances CTAS and INSERT ... SELECT can run in parallel? and when does the "SELECT" part of both operations does [not] run in parallel?
  • What could cause either an ORA-7454 or ORA-12827 during parallel execution?
  • This is a tricky one: When does a direct-path insert into an IOT NOT take place in parallel even if IOT is created in parallel mode?
  • How can Resource Management be deployed to provide services with different parallel processing characteristics (e.g. statement queuing)?
  • Under which mode of PARALLEL_DEGREE_POLICY, and how does lack of I/O Calibration affect parallel processing?
  • When should PARALLEL(MANUAL) hint be used?
  • When can In-Memory parallel execution be employed? Where is the data kept in such environment? (PGA vs. Buffer Cache)
  • Under which circumstances DML statements can't be executed in parallel?
  • How does parallel execution in RAC takes place?
  • What's the effect of either of the following parameters?
    • PARALLEL_MIN_SERVERS
    • PARALLEL_MAX_SERVERS
    • PARALLEL_MIN_PERCENT
    • PARALLEL_SERVERS_TARGET

 

1.5. DBMS_MONITOR/trcsess/tkprof

 

  • Which kinds of trace (i.e. which DBMS_MONITOR procs) could produce multiple trace files?
  • You need to be familiar with the sequence of actions from generating trace to using trcsess and eventually tkprof.
  • Which program consolidates trace from different trace files? trcsess? or tkprof?
  • How does tkprof treat recursive calls?
  • tkprof parameters; for example what options are available for sorting the output items?
  • How many trace files are produced by for each

 

 1.6. SQL Tuning Sets, SQL Tuning Adviso, SQL Access Advisor 

 

  • What can be kept in an STS? The syntax of different STS management operations do not matter, as much as a full grasp on the process of STS management matters.
  • What are the data sources for Automatic and Manual invocations of SQL Tuning Advisor?
  • What are the sources for SQL Access advisor?
  • What is the functional relationship between STS, Tuning Advisor, and Access Advisor? Which one is used by which?
  • What are the output of Tuning Advisor and Access Advisor? for example which one issues recommendation regarding deploying materialized views?
  • What is the role of Automatic Tuning Optimizer?

 

1.7. SQL Profiles and SQL Plan Management

 

  •  Which initialization parameters would facilitate the discovery/storage and using of the plans by optimizer?
  • What is the role of Automatic Tuning Optimizer in an ecosystem which also includes SQL Tuning Advisor, SQL Profiles, and SPM mechanism? How are the dots connected to each other? You need to know the data flow between these elements.
  • What are the different steps in the life cycle of a plan? How does a "Fixed" plan differ from an "Accepted" one?
  • What happens when a plan changes? What does "Evolution" entail?
  • How do you manage different plans for different purposes? e.g. Batch processing vs.other times

 

2. References and Resources

 

I suggest the following resources would be adequate for passing the certification exam:

  • Database SQL Tuning Guide in here
  • Database VLDB and Partitioning Guide in here
  • Troubleshooting Oracle Performance, 2nd Ed (2014), by Christian Antognini via Apress

 3. A Demand for Keeping Things In Perspective

 

Although the subject of Parallel Processing is mentioned in less than 20% of subjects outlined in the official exam topics, it actually was referenced in about half of the 75 questions in one way or other! So if you're not proficient in concepts of parallel processing, there's a big chance that you will not pass this exam.

Implications of such huge emphasis on the subject are somewhat confusing; especially considering the fact that "throwing resources at a problem" is not exactly an example of "Tuning."

Parallel Processing is as much of a "SQL Tuning" technique that Damien Hirst is a representative of Art, compared to artists such as Picasso, Dali, or even Anthony Gormley.

If -like me- you have a background which makes you prone to being open to conspiracy theories, you could even call it "a sign of Oracle's hidden agenda to drive the demand for more beefy ('Engineered') servers!"

But less naive exam takers could simply refer to few references to the concept of Resource Management in the exam, and simply call it "disproportionate" to the number of questions about Parallel Processing.

I believe the subject of Parallel Processing could be addressed in an exam such as Performance Tuning (1Z0-054 or 1Z0-064), AND alongside concepts like instance caging.

At least I tend to think that "SQL Tuning" is more of an art form, and Parallel Processing is the sledge hammer that Rodin never used and never would have used for creating "The Thinker."

Update: I'd like to thank Horia Berca for guiding me to slides from Tom Kyte's Optimizer Masterclass. The Optimizer Masterclass is not the preparation/training course for 1Z0-117, but It made me happy to see how the amount of emphasis that one of Oracle's gurus has put on the subject of Parallel Processing seems to bolster my position regarding the subject, and what I've expressed in this post.