Sunday, July 06, 2008
Oracle and bind variable peeking
Select * from order where date between ?1 and ?2
the execution plan depends on the values of ?1 and ?2 while you execute the first time. It could be a index range scan if the dates are narrow or it could be a full table scan if the dates are wider. During subsequent executions even if the date range varies considerably the execution plan remain same. A side effect of execution plan re-use. This also implies that first execution has an impact on execution plan. Would it be a good practice to initialize the queries with sample values during application start up?
What will you do if you have a query which will have varying cardinality? I would recommend two options
1) if your requirement is two different scenarios one for displaying current orders (with narrow date range and smaller cardinality) and other for statistics processing program (with wider date range and larger cardinality). You may consider writing two separate SQLs
Select * from order o_small where date between ?1 and ?2
Select * from order o_large where date between ?1 and ?2
2) If you cannot predict the cardinality, Don't use bind variable
I think this feature is available from Oracle 9i
However Oracle thin driver 9.x doesn't support bind variable peeking. You need to use thin driver 10g (refer Ask Tom )
Monday, June 30, 2008
Execution plan for the queries in the statspack report and queries in top sql
Similarly many times I wanted to view the top N expensive queries and it's plan in report. It is possible to navigate to the plan from top sql in Oracle Enterprise Manager. However you have to do it one by one. also you may not get the result in report form.
The following two scripts can be used for these purposes.
stats_plan.sql
topsql_plan.sql
Note: Report for stats_plan needs to be run immediately after taking the snap otherwise you may not get all queries listed.
Saturday, April 26, 2008
Update Query with Join in Oracle Database
Employee(empid, empname, salary, location)
and the table
Address(addId, houseNo, Street, city, country, empId)
We want to update the location column with city column wherever the location is null.
Update Employee e
SET location = (select city from Address a where e.empid = a.empId)
where location is null
However if you have one million record in Employee table the sub query will execute as many times. It is going to take long.
You can re-write the query as follows:
Update
(Select location ll, city cc from Employee e, Address a where e.empid = A.empId and e.location is null)
SET ll = cc
There are two conditions for above query
1) You can update only columns from one table
2) The second table (that is not being modified) must be key preserved - that is, there can be at most one in second table for each record in first table - that is, there should be a unique constraint in second table for the columns used join condition
Second query is much cheaper than the first one when we have large number of records in both table (or in first table)
Wednesday, January 09, 2008
Transaction Isolation level and Concurrency in Oracle
ISO supports following isolation levels
read uncommitted
read committed
repeatable read
serializable
Oracle supports following
read committed
serializable
However oracle's serializable is neither repeatable, nor serializable according to ISO specifications.
Oracle serializable provides the same data as of when the start of the transaction irrespective of any committed changes (yes oracle allows data modification even if you specify serializable) not even the read rows are locked. Only thing is the modifications are hide from you so that you think the access is serializable (cheating?)
Consider the following two cases which require serializable and repeatable (according to ISO). Scenario 1 (requires Serializable)
Table emp contain Columns empid and Role.
There is one script which check if there is any employee with President, if none, they choose one employee randomly and set the role as President.
Assume this script runs concurrently.
| time | Operation of Thread 1 | Operation of Thread 2 |
| 1 | select empid from emp where role = 'President' | |
| 2 | select empid from emp where role = 'President' | |
| 3 | Choose a emp randomly | |
| 4 | Choose a emp randomly | |
| 5 | update emp set role = 'President' where empid = 3 | |
| 6 | update emp set role = 'President' where empid = 5 | |
| 7 | Commit | |
| 8 | Commit |
Now there are two employees with Role as President is was indented
Scenario 2 (requires Repeatable Read)
Emp contains empid, role and backup
There are two scrips
Script 1 - If there are no Vice Presidents, make the backup person as Vice President
Script 2 - Change the backup person (and hence change the vice president also)
If these two scripts run concurrently it will lead to existence of Vice presidents
| time | Operation of thread1 | Operation of thread2 |
| 1 | select empid from emp where role = 'Vice President' | |
| 2 | backup, oldemp = select backup, empid from emp where role ='President' | |
| 3 | backup = select backup from emp where role = 'President' | |
| 4 | update emp set role = 'Vice President' where empid = backup | |
| 5 | update emp set role = null where empid = backup | |
| 6 | update emp set backup = 11 where empid = oldemp | |
| 7 | update emp set role = 'Vice President' where empid = 11 | |
| 8 | commit | |
| 9 | Commit |
|
Now we have old backup as well as new backup having Role as Vice President which was not indented
References:
1) http://www.oracle.com/technology/oramag/oracle/05-nov/o65asktom.html
2) http://download.oracle.com/docs/cd/B10500_01/server.920/a96524/c21cnsis.htm
Thursday, October 25, 2007
Concurrency Levels - WebLogic and Oracle
Most of the applications require 'Repeatable-Read' concurrency level (consider the banking fund transfer transaction - you read the balance, reduce the balance and then update the reduced balance to database). (I don't know why Oracle doesn't support 'Repeatable-Read'!!!)
WebLogic Server has the following isolation-level for Oracle to support 'Repeatable-Read' semantics - '
READ_COMMITTED_FOR_UPDATE'.
However there is one issue; It supports only CMP entity beans (obviously - what WebLogic does it it uses 'select for update' for all select queries to get row lock which it can do only for container generated queries). Currently for direct SQLs and BMP entity beans has to hard code 'select for update' in its code.You may want one entity bean 'Repeatable-read' in one transaction and 'Read-Committed' in another transaction. How will you achieve this?
You have to specify the isolation-level for session bean (where actual transaction starts) than for the entity bean.
However in a business transaction there may be many entity beans involved however you may want only one entity bean to have 'Repeatable-Read', all other to use 'read-committed'. How to achieve this?
You are asking too much! There is no way to achieve that currently. You have to either specify isolation level for entity bean and lock the bean whenever it participate in a transaction OR specify isolation level at session bean level and lock all the entity beans participated.
You have a (better?) third choice! Not using entity beans (for that matter any OM mapping solutions)!!!
'READ_COMMITTED_FOR_UPDATE' is supposed to be supported only for CMP entity beans however I have observed that it works for session beans as well (if you specify this for session beans, then all the entity beans involved in that transaction will be using 'READ_COMMITTED_FOR_UPDATE'.
if a request propagate from on method with one isolation level to another method with another isolation level, does the isolation level changes when it enter the new method? Don't know!!! I should, but I am not sure if it does so now!
Thursday, October 04, 2007
ORA-01000: maximum open cursors exceeded
Read the following post by Natalka
http://orafaq.com/node/758
v$open_cursor and v$sesstat doesn't show the currently open cursors correctly. It includes the cursors 'marked for close' but not yet closed. Looks like there is no safe way to find the cursors really open!
Monday, October 01, 2007
Issues with Oracle, Time and JDBC
2) 'DATE' and 'TIMESTAMP' data types doesn't contain time zone information. If you want to include time zone, you need to use either 'TIMESTAMP WITH TIME ZONE' or 'TIMESTAMP WITH LOCAL TIME ZONE'.
2) If the time zone of JVM and time zone of session are different, the time will not be saved correctly into Oracle. It will pick time component from JVM and time zone component from session (without conversion). Hence you need to keep the session time zone as same as the JVM time zone.
3) There is no direct way to retrieve the time with time zone from 'TIMESTAMP WITH TIME ZONE'. You need to retrieve as string and parse it.
4) If you use 'TIMESTAMP WITH LOCAL TIME ZONE' you can retrieve the time with time zone correctly. However you need to set the time zone explicitly on the session. You need to use the Oracle specific API OracleConnection.setSessionTimeZone().
5)You cannot alter database timezone when database has TIMESTAMP WITH LOCAL TIME ZONE columns ( ORA-30079)
One possible solution would be keep Oracle, Application server and session are in GMT. Make time zone conversions at all input output points.