Bajwa Consulting Need senior Oracle DBA help? Performance tuning, Cloud migrations, GoldenGate, backup and DR. Plus 70+ free DBA scripts.
Visit bajwaconsulting.com →

Wednesday, January 21, 2015

SQL tuning Key concepts



SQL tuning Key concepts
In order to optimize a SQL statement these are the steps that CBO (oracle sql query optimizer called Cost Based Optimizer) has to go through.
1.       Open Cursor : A memory structure for cursor is allocated in PGA.  In computer science lingo a Cursor is a pointer type that is used to fetch rows from a result set.  At this time SQL statement is not associated with the opened cursor.
2.       Parse Statement: In this phase the SQL statement is attached to the opened cursor.  In this phase CBO decides whether to Hard Parse (create new execution plan) or Soft Parse (Get the already existing execution plan from Shared pool.  The plan is then also saved in the library cache of Shared pool Area.  This shared pool area is a memory structure area in database reserved as a part of SHMMAX (shared memory segment reserved on the operating systems).  SQL plans are kept in Shared pool as per Touch Count algorithm.  TC algorithm is to age out the plans with the least count of each SQL statement execution.
3.       Define Output Variables: If the statement returns the data then variables must be defined (standard output or other types).
4.       Bind Input Variables: If the SQL statement uses bind variables then their values must be provided, error for invalid bind variables is raised only at run time.
5.       Execute Cursor: The SQL statement is executed in this phase by the database engine.
6.       Fetch Cursor: This is a data retrieval step i.e. If the SQL statement returns any data then this step gets it.  This fetch could be from a Disk or from the Buffer Cache also part of the SHMMAX.  Data blocks from the buffer cache again aged out based on TC algorithm.
7.       Close Cursor: Cursor in PGA is closed and the shareable cursor in Share Pool is not removed (until it ages out or keeps in the buffer through touch count algorithm).
The second step of Parsing the SQL statement is crucial.  This how Parsing works
  1. VPD predicates: If a Virtual private database is enabled on the table then the where clause of the query is altered with added defined predicate.
  2. Check Syntax, semantics and access rights.  CBO here checks the syntax of the SQL statement, all objects that it refers to are correct and session running the SQL has all the rights require to run this SQL.
  3. If a New SQL then Store the parent cursor in library cache, else get the shareable cursor from library cache.  If a new SQL then do the rest of the steps below else use the available shareable cursor.
    1. If new SQL then Logical optimization. New and semantically same SQL statements are produced by applying different transformation techniques.  This is where CURSOR_SHARING parameter is enforced. 
      By using these transformation techniques number of candidate execution plans is reduced.
    2. If new SQL then Physical optimization.  All execution plans discovered in step 4 are generated first, then based on the Statistical data (dbms_stats) of the objects as well as System or dynamic sampling a cost is associated with each plan.  Then plan with the lowest cost is picked.
  4. Store the cursor in library cache for future use until it is aged out or rises up due to touch count algorithm.

No comments:

Post a Comment