If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > sql query processing : strange behaviour

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-18-10, 14:32
faisal.inam faisal.inam is offline
Registered User
 
Join Date: Jun 2010
Posts: 4
sql query processing : strange behaviour

Hi,

I have a question regarding processing of sql queries in databases. I have implemented a two audit columns approach for change data capture. The result of the following views is same under experimental conditions. However, I am getting strange performance results when i extract data from the following views using cursors.

1. CREATE VIEW V_AUTHOR_UPD AS SELECT A_ID, A_FNAME, A_LNAME FROM AUTHOR WHERE (A_TIMESTAMP_UPD > CDCTS ) AND (A_TIMESTAMP_INS < CDCTS )@

vs

2. CREATE VIEW V_AUTHOR_UPD AS SELECT A_ID, A_FNAME, A_LNAME FROM AUTHOR WHERE A_TIMESTAMP_UPD > CDCTS@

Now the first query is more complex than the second one but is giving results much much faster than the second one.

The expectation was that the second query should have performed much better than the first one as it is less complex.

I have observed a similar behaviour in another scenario where a complex query is giving better results than a simple one.

Any reasonable explaination about the behaviour of sql queries?

Thanks in advance.
Faisal

Last edited by faisal.inam; 09-18-10 at 14:35.
Reply With Quote
  #2 (permalink)  
Old 09-18-10, 20:00
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
DB2 may be interpreting the 1st query as one that will have fewer qualifying rows, and therefore DB2 will use an index; whereas the second query will need to read a significant percentage of the table and a table scan (instead of index access) may be faster.

It is possible that DB2 is mistaken in its assumptions and has selected the wrong access path for the second query, but you didn't say how many rows each of the two queries returns.

The access path chosen may depend on the accuracy of the statistics from runstats command and the sequence of the rows (reorg sequence).
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 09-18-10, 21:01
faisal.inam faisal.inam is offline
Registered User
 
Join Date: Jun 2010
Posts: 4
Thanks for the reply!

The qualifying rows for both cases is same. And the the point is, for case 1 we need to look at both insert and update timestamps whereas for case 2 only the update timestamp is enough. Consider the following statistics:

Number of rows inserted = 186624 (updates A_TIMESTAMP_INS)
Number of rows updated = 186624 (updates A_TIMESTAMP_UPD)
Rows retrieved for both cases = 186624
number of inserts plus updates is equal to 80% of the total size of data.

Time taken by case 1 : 6 sec
Time taken by case 2: 132 sec

I can provide more details if required.
Reply With Quote
  #4 (permalink)  
Old 09-19-10, 00:16
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
DB2 has decide the access path (table scan vs using an index) based on the statistics gathered with runstats. If no statistics have ever been gathered, it will use default statistics.

Generally, all other things being equal, DB2 will assume that a GT and LT will return fewer rows than GT alone.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #5 (permalink)  
Old 09-20-10, 07:09
faisal.inam faisal.inam is offline
Registered User
 
Join Date: Jun 2010
Posts: 4
Is there a way that I can disable the query optimizer? ... I dont want DB2 to use Indexes by itself until i create indexes by myself. In a study i have to show the performance gains with indexes which i cant do with db2 doing it automatically.

Thanks in advance ...
Reply With Quote
Reply

Tags
query processing

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On