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 > Reducing the cost of a SQL query on live database

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-03-05, 05:59
JAYANTA_DATTA JAYANTA_DATTA is offline
Registered User
 
Join Date: Oct 2004
Location: DELHI INDIA
Posts: 336
Reducing the cost of a SQL query on live database

The following query is doing full table scan on table1 and table2 and thus the total cost on the Live database is becoming very high creating enough trouble for us.

Can anybody suggest any tuning to this query:


SELECT Q3.STUDENT_ID AS "APPROVAL_ID", Q3.INPUT_STUDENT AS "INPUT_STUDENT",
Q3.ROOT_STUDENT AS "ROOT_STUDENT", Q3.LOCK_TOKEN AS "LOCK_TOKEN",
'STUDENT'
AS "APPROVAL_TYPE"
FROM TABLE1 AS Q1, TABLE1 AS Q2, TABLE2 AS Q3
WHERE Q3.WKF_STATE IS NOT NULL AND Q3.TO_DATE IS NULL AND (Q3.INPUT_STUDENT <>
:?) AND Q3.INPUT_STUDENT IS NOT NULL AND (Q2.TYPE = :?) AND (Q2.ID =
Q3.INPUT_STUDENT) AND (Q3.STUDENT_ID = Q1.ID) AND Q3.WKF_STATE IN (:?,
:?,
:?, :?) AND Q1.TYPE IN (:?, :?, :?, :?, :?)
Reply With Quote
  #2 (permalink)  
Old 03-03-05, 06:10
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
What platform are you on ?

have you done runstats on the tables ?

Posting the definitions of the tables and indexes will make it easier for people to suggest

Cheers
Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #3 (permalink)  
Old 03-03-05, 06:19
JAYANTA_DATTA JAYANTA_DATTA is offline
Registered User
 
Join Date: Oct 2004
Location: DELHI INDIA
Posts: 336
Thanks Sath: here are the details:

Table1 has index on Id.
Table2 has indexes on party_id, to_date, wkf_state, input_party
other tan alt_id. But query is still doing full table scan.

Thanks,
Jayanta
Reply With Quote
  #4 (permalink)  
Old 03-03-05, 06:25
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Just to double check , are you on DB2 ?

With the details you provide, it is very difficult, atleast for me, to even guess

Good luck

Cheers
Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #5 (permalink)  
Old 03-03-05, 12:57
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Code:
SELECT 
    Q3.STUDENT_ID AS "APPROVAL_ID", 
    Q3.INPUT_STUDENT AS "INPUT_STUDENT", 
    Q3.ROOT_STUDENT AS "ROOT_STUDENT", 
    Q3.LOCK_TOKEN AS "LOCK_TOKEN", 
    'STUDENT' AS "APPROVAL_TYPE" 
FROM TABLE1 AS Q1, 
    TABLE1 AS Q2, 
    TABLE2 AS Q3 
WHERE Q3.WKF_STATE IS NOT NULL 
    AND Q3.TO_DATE IS NULL 
    AND 
    (
        Q3.INPUT_STUDENT <> :?
    ) 
    AND Q3.INPUT_STUDENT IS NOT NULL 
    AND 
    (
        Q2.TYPE = :?
    ) 
    AND 
    (
        Q2.ID = Q3.INPUT_STUDENT
    ) 
    AND 
    (
        Q3.STUDENT_ID = Q1.ID
    ) 
    AND Q3.WKF_STATE IN (:?, :?, :?, :?) 
    AND Q1.TYPE IN (:?, :?, :?, :?, :?)
Given the above predicates, I suspect that DB2 thinks that a large percentage of the rows from the tables will be returned, which means that it is faster for DB2 to read the entire table(s).

If your query normally returns a small number of rows, then which predicate is the most selective?

When you execute runstats, make sure you are getting distribution on key columns.
__________________
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
  #6 (permalink)  
Old 03-04-05, 02:14
JAYANTA_DATTA JAYANTA_DATTA is offline
Registered User
 
Join Date: Oct 2004
Location: DELHI INDIA
Posts: 336
Thanks a lot Marcus.
That one worked fine and served our purpose upto a great extent.

Jayanta
Reply With Quote
Reply

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