oracle_sql_processing

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
oracle_sql_processing [2019/08/11 07:49] – [Overview] andonovjoracle_sql_processing [2020/10/19 07:29] (current) 86.49.253.137
Line 1: Line 1:
 =====Overview===== =====Overview=====
-SQL Processing is one of the biggest problems with any database because it is the hearth of performance tuningIn that chapter I will try to summarize, how Oracle processes an SQL.  +The SQL processing like any other processing of codeIt comes from a human and needs to go back to a human :) The problem isthrough how many stops it will pass 
-Firstly the statement has to be processed and there are several steps which should be executed.+Below you can see a basic representation on the possible wait times for any query:
  
-  * Open Cursor +{{ :sqlprocessing.jpg?500 |}}
-  * Syntax Check +
-  * Semmantic Check +
-  * Shared Pool check +
-  * *Binding +
-  * *Optimization +
-  * *Row source generation +
-  * Execution +
-  * Fetch results +
-  * Close Cursor+
  
-Below you can check a brief picture of Cursor Processing:+This picture, clearly displays why it isn't ALWAYS A DATABASE FAULT :) Often the database is intermediary point between the User and the data. 
  
-{{ :cursorparsing.jpg?600 |}}+=====Calls===== 
 +Before we start speaking of SQL processing, we should discuss something very important and that is how ORACLE communicate and operate in general. 
 +Oracle kernel is using requests called "calls". These "calls" represent request to certain structures and are generally devided into two groups:
  
-=====Parsing===== +DB Calls: 
-Parsing can start and end the processing of SQL, depending on type of parse. Generally there are 4 types of parsing:+  * Parse 
 +  * Execute 
 +  * Fetch
  
-  - No Parse +Operation System (OS) Calls: 
-  - Soft-Soft Parse +  * WAIT 
-  - Soft Parse +  * STAT
-  - Hard Parse+
  
-If the query doesn't need parsing, the processing of SQL will go directly to EXECUTING phase (however that is rarely the case, we will see in a sec:+These calls are accomplished using different functions:
  
-====No Parse==== +{{ :sql_process_calls.gif?600 |}}
-The most significant difference of this one from the previous three is it will not increase the parse count. The reason is obviously “No parse at all ” – execute the code by using given bind variables. It the best if a same SQL run multiple times. +
-This is the same example as presviouly but moved the open/parse/close cursor outside.+
  
-===Example=== +=====SQL Processing ===== 
-<sxh bash> +SQL Processing is one of the biggest problems with any database because it is the hearth of performance tuningIn that chapter I will try to summarizehow Oracle processes an SQL.  
-m_cursor :dbms_sql.open_cursor; +Firstly the statement has to be processed and there are several steps which should be executed
-dbms_sql.parse( +Below you can check a brief picture of Cursor Processing:
-               m_cursor, +
-               'select n1 from t1 where id :n', +
-               dbms_sql.native +
-               ); +
-dbms_sql.define_column(m_cursor,1,m_n); +
-  +
-for i in 1..1000 loop +
-  dbms_sql.bind_variable(m_cursor, ':n', i); +
-  m_rows_processed := dbms_sql.execute(m_cursor); +
-  if dbms_sql.fetch_rows(m_cursor) > 0 then +
-    dbms_sql.column_value(m_cursor, 1, m_n); +
-  end if; +
-end loop; +
-  +
-dbms_sql.close_cursor(m_cursor); +
-</sxh>+
  
-====Soft-Soft Parse==== +{{ :cursorparsing.jpg?600 |}}
-This one is still called soft parse in Oracle’s documentI don’t think Oracle give it a proper name to make it easy to understand. Sometimes DBA call it soft-soft parse. When you run a SQL a few times in the same session (with session_cache_cursors enabled), Oracle will create a short cut in your session memory, so when you run it next time, oracle don’t need to do the search work. It knows where it’s already. It’s cheaper in cost than soft parse. +
-Here is an example in the book(although it’s using pl/sql cursor cache, the effect is the same):+
  
-===Example=== +[[oracle_sql_processing_parse| Parsing]] 
-<sxh bash> +  * Open Cursor 
-for i in 1..1000 loop +  * Syntax Check 
-  m_cursor := dbms_sql.open_cursor; +  * Semmantic Check 
-  dbms_sql.parse( +  * [[oracle_sql_processing_shared_pool_check| Shared Pool check]] 
-                 m_cursor, +  * Binding 
-                 'select n1 from t1 where id = :n', +  * *Optimization 
-                 dbms_sql.native +  * *Row source generation
-                 ); +
-  dbms_sql.define_column(m_cursor,1,m_n); +
-  +
-  dbms_sql.bind_variable(m_cursor, ':n', i); +
-  m_rows_processed := dbms_sql.execute(m_cursor); +
-  if dbms_sql.fetch_rows(m_cursor) > 0 then +
-    dbms_sql.column_value(m_cursor, 1, m_n); +
-  end if; +
-  +
-  dbms_sql.close_cursor(m_cursor); +
-end loop; +
-</sxh> +
- +
- +
-====Soft Parse==== +
-This one is the second easiest to understand. It’s also the second expensive one. When you submit a SQL, oracle searches the library cache, and it found a plan matches your request exactly. Oracle didn’t need to create a plan but need to do the search work. Some notes from the book about the work oracle need to do: +
-When you pass a piece of text to Oracle it will do a syntax check to decide if it is legal, then it will search the library cache for a matching text (using a hash value computed from the text).  +
-If it finds a textual match Oracle starts the semantic check—checking to see if the new text actually means the same as the existing text (same objects, same privileges, etc.); this is known as cursor authentication.  +
-If everything matches, then the session need not optimize the statement again.+
  
 +[[oracle_sql_processing_execute| Execute]]
 +  * Execution
  
 +[[oracle_sql_processing_fetch| Fetching]]
 +  * Fetch results
 +  * Close Cursor
  
-====Hard Parse==== +*- Only when hard parse is needed
-This one is the easiest one to understand. It’s also the most expensive one.  +
-Whenever the fist time oracle run a SQL, there is no plan about this SQL in the memory.  +
-Oracle needs to create a plan(optimizing) and save it in library cache. That’s the hard parse. Oracle will go through the entire three and generate all possible plans. Then Oracle will compare each of the plans to find the  best. +
-As you can imagine this requires A LOT OF resources, we will discuss in a bit.+
  
-{{ :hard_parse.gif?500 |}} 
  • oracle_sql_processing.1565509750.txt.gz
  • Last modified: 2019/10/18 20:04
  • (external edit)