Showing posts with label Explain plans. Show all posts
Showing posts with label Explain plans. Show all posts

Friday, 13 June 2014

Understanding Explain plan for Parallel Queries in Oracle



Hi Guys

Basic

In the previous article we discussed the general rule for applying parallel hint Parallel Hint.  Parallel explain plan are more complex to read compared to serial plans. When we use hint paralle(2) . 4 Processes are created 2 consumers and 2 producers. The general assumption is if some work can be done by 1 process in 1 minute then it can be done by 2 processors in 1/2 minute or with 4 processes in 0.25 .But this is not true due to various things.  Recently I had faced issue with paralle queries. I found the articles by Randolf Geist and Jonathan lewis. Below are the link . This articles is my take away from their articles and what I found in my queries . 

When you create many parallel processes each process is assigned a PGA memory. Consider you have 128 processes than a lot of PGA memory will be used and this memory when full they will start to use  the Temp Space  thus slowing down the sql 


Article by Randolf Geist. ( Very Very good)


Degree of Parallelism

http://www.oracle.com/technetwork/issue-archive/2010/o40parallel-092275.html

Click on the Image or Save the Image if you are not able to read the Text writen on image 











The change involved moving the table join to a subquery. So basically the idea is make sure processes are working as expected. Read the below article about details on how parallel queries internally work

If degree of parallelism is set to 4 it means 8 parallel processes will be used. 4 as producers and 4 as consumers.When we see PX BLOCK ITERATOR Data is accessed not at partition level but at data block level. Since data is accesed at block level it requires distribution of data

In Order for Parallel queries to work efficiently

Efficient parallel execution plan

Good Link on Bloom filters




Some Notes after reading

Due to this Consumer / Producer model Oracle has to deal with the situation that both Parallel Slave Sets are busy (one producing, the other consuming data) but the data according to the execution plan has to be consumed by the next related parent operation. If this next operation is supposed to be executed by a separate Parallel Slave Set (you can tell this from the TQ column of the DBMS_XPLAN output) then there is effectively no slave set / process left that could consume the data, hence Oracle sometimes needs to revert to sync points (or blocking operations that otherwise wouldn't be blocking) where the data produced needs to be "parked" until one of the slave sets is available for picking up the data.

In recent releases of Oracle you can spot these blocking operations quite easily in the execution plan. Either these are separate operations (BUFFER SORT – not to be confused with regular BUFFER SORT operations that are also there in the serial version of the execution plan) or one of the existing operations is turned into a BUFFERED operation, like a HASH JOIN BUFFERED.

Whenever we say something is buffered it is writen to temp and read back.If the amount of data to buffer is large, it cannot be held in memory and therefore has to be written to temporary disk space, only to be re-read by / to be sent to the Parallel Slave set that is supposed to consume / pick up the data. And even if it can be held in memory, the additional PGA memory required holding the data can be significant.


What does this mean --- simply because Oracle cannot have more than two Parallel Slave sets active per Data Flow Operation ????

SQL work areas -- How has join tables are made ????

What is a Data flow operation


DFO means "Data Flow Operator". Actually, “queries” don’t run in parallel, it’s "data flow operations" (DFOs) that run in parallel, and a single query can be made up of several data flow operations. DFO tree is composite with DFOs, usually one query have one DFO tree. such as

The result from v$pq_tqstat:
 
You can just go through the Screenshot below. Follow the comments that all is needed to understand those plans











 
There are Three ways in which parallel join operations can be performed

First

One slave sets the reads the table as shown in example and one slave set joins the data as shown in the figure. Q1,00 reads the data and Q1,04 joins the data this requires buffering

Second
Look at the broadcast being done , it is received by Q1,06 which also does the full scan of the sec acss grp person. This method does not require buffering for hash joining as entire data has being broadcasted

Note -- If any time you see multiple DFO being used then you will see Q1, Q2 in the TQ column .

For Other two read the original article by oracle ACE Randolf Geist which I found helpful

Note

Window Sorts are Analytical functions

Sunday, 14 July 2013

Two Layer Query - Report building technique

Hi Guys,

We all must have built many report.We first build reports then check how its performing.Now lets start report building by looking at Explain plans

The Article below is very good . It explains how we can overcome the lack of Aggregate Navigation feature in cognos. Aggregate navigator means when we have summary table and detailed table you want to Summary table on the fly based on the query.

It also explains two layered query approach for better Explain plans in oracle. Your can also check out my other articles on Oracle explain plans by typing " Cognossimplified Oracle Explain plans"



http://ibmcognosrmug.files.wordpress.com/2012/03/cognos_user_group_presentation.pdf


Friday, 12 July 2013

How to read Explain plans for Sql tuning

Hi Guys,

The main idea to improve speed of query is to get the result by going through as little physical reads ( from disk) as possible.

We do indexing , partition all things so that we can easily identify our data from millions of rows.Without having to go through each row.So the idea behind reading explain plan is identifying which part of sql is not using best way to read from db.There might be one table which is doing full scan(reading entire table) to reach at a particular row (say employee id) .Now how can we make it reach it faster , may be by not having to go through entire table and just reading one row.There are number of techniques out there.

For datawarehouse a Bitmap index may speed up {My Actual case study on DW} .OR indexes on a particular column which is forcing a full table read may speed up. So lets look what is a explain plan and What is Statistics IO.

A word of Caution for reader - Below is a article i have writen on reading Explain plans.If you have good experience ( I mean you can write and debug sql query very very easily ) then only you should read this advance stuff.Its involves a lot of detailed discussion.Not for freshers.I am not discouraging anyone but for less experienced people it wont make much sense.

Some Notes before we start

Two things we need to tune sql (Explain plan and Statistics IO) ......Since Statistics IO is not set by default.Below are steps to enable it

If you are having difficulties understanding terms discussed in this article.Please go through below link
http://cognossimplified.blogspot.in/2013/07/oracle-database-concepts.html)

Below are steps to enable Statistics IO if you dont have it enabled.(Usually DBA would have enabled this )
http://cognossimplified.blogspot.in/2013/07/how-to-enable-statistics-io-in-sql.html

Example of Explain plan and Statistics IO 

  create table t ( a int, b int, c char(20), d char(20), e int );
 
  Insert into t select 1, 1, 1, 1, rownum  from all_objects ;( it will create a dummy table with lot of records)

create index a_indx on t (a) compute statistics;

set autotrace on

select a from t where a =1

Plan hash value: 2016457929

-------------------------------------------------------------------------------
| Id  | Operation            | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |        | 74275 |   942K|    44   (3)| 00:00:01 |
|*  1 |  INDEX FAST FULL SCAN| A_INDX | 74275 |   942K|    44   (3)| 00:00:01 |
-------------------------------------------------------------------------------

  Statistics
-----------------------------------------------------------
              13  user calls
               0  physical read total bytes
               0  physical write total bytes
               0  spare statistic 3
               0  commit cleanout failures: cannot pin
               0  TBS Extension: bytes extended
               0  total number of times SMON posted
               0  SMON posted for undo segment recovery
               0  SMON posted for dropping temp segment
               0  segment prealloc tasks

Very Important link gives the various terms used in explain plan in detail.

http://www.akadia.com/services/ora_interpreting_explain_plan.html

What is Index fast full scan ---They are similar to full table scans that is they read the full index and while doing so oracle will fetch the next multiple blocks in anticipation that they will be required.It makes use of flag db_file_multiblock_read . Similar to full table scan.It is used when we dont even need to touch db to get our data .See in this case the the physical read is zero.we got our result just from index scan as the column requested in select statement is a index column .

In index fast full scan index is read as a table. Normally in a index with mutliple leaf nodes we go from one node to other but in fast full scan we read all nodes. Not necessary in order and it will use multiblock i/o that is it will read from multiple blocks


select count(distinct deptno) from t
and either of EMPNO or DEPTNO is defined as "not null" -- we may very well use the INDEX
via a FAST FULL INDEX SCAN over the table (the index being a "skinny version" of the
table in this case.


Now lets change the column to one which is not indexed let see the response

select b from t where b =1

Notice that it goes for full table scan

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      | 74275 |   942K|   171   (1)| 00:00:03 |
|*  1 |  TABLE ACCESS FULL| T    | 74275 |   942K|   171   (1)| 00:00:03 |
--------------------------------------------------------------------------


Now lets go for condition on A .We have a index on A

select * from t where A >1

--------------------------------------------------------------------------------------
| Id  | Operation                   | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |        |     1 |    83 |     1   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| T      |     1 |    83 |     1   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | A_INDX |     1 |       |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------

Two links which you might like

http://www.dwbiconcepts.com/database/22-database-oracle/26-oracle-query-plan-a-10-minutes-guide.html