When a SQL statement is executed, it passes through three phases. Those three phases are:
- Parse
- Execution Plan
- Fetching
By parsing means to check whether the syntax is correct. If that is done then Oracle generates execution plans, determines the cost of each plan and finally decides which plan the query will follow. Remember here that the actual fetching is not done here. The only decision made here is the semantics of the query. The exact logic is determined and plan is generated. Before this Oracle computes the hash value of the query based on some predefined formula and checks whether the query with the same hash values is present or not. If it is present then Oracle skips the second phase and goes to the third phase.
Bind variables can play a vital role in this scenario. Let’s clarify it how? Suppose you run a query:
SQL>select * from emp where empno=7349;
When you run this query for the first time Oracle performs all the three phases and returns the result back. Now immediately after that you gave the following command:
SQL> select * from emp where empno=7548;
This query is very similar to the first one, but only for us. For Oracle it is a different query for which all the three phases are performed. By using bind variables instead of hard coded values you can make Oracle treat both queries similar and thus skip the second phase. With bind variable the above queries will look like:
SQL> select * from emp where empno=:v_emp;