coldholic
coldholic

Reputation: 161

Improve performance of the SQL query (DB2)

I need to get the file name with records meeting a certain criteria, but I realize that the query is very slow when executing. I have added index, but it is still very slow, how can improve the performance??? I am using DB2. Please help, thank you.

Table (FILE) (Data volume - around 100000)

INDEX (CATEGORY, FILE_ID)

Table (RECORD) (Data volume - around 50000000)

INDEX(CREATE_DATE, TYPE, FILE_ID)

SELECT NAME 
FROM FILE
WHERE CATEGORY = ? AND 
      FILE_ID IN (SELECT FILE_ID FROM RECORD WHERE CREATE_DATE = ? AND TYPE = ? )

Upvotes: 0

Views: 2343

Answers (5)

Bob
Bob

Reputation: 153

You might find that putting the "table file" into a temp table with the where clause speeds things up...

eg...

create a temp table then insert all records from Table File "Where Cateogry = ?" and then join that table onto Record

Upvotes: 1

Seb.B.
Seb.B.

Reputation: 151

the join should solve your issue.

also sometimes you need to reorganize (REORG) tables and indexes to improve performance on slow queries.

Upvotes: 1

Ethan Li
Ethan Li

Reputation: 1001

this wouldn't give you duplicate result

SELECT NAME 
FROM FILE F
JOIN (
    SELECT FILE_ID FROM RECORD WHERE CREATE_DATE = ? AND TYPE = ? 
    GROUP BY FILE_ID
) Q ON F.FILE_ID = Q.FILE_ID
WHERE CATEGORY = ?

Upvotes: 1

Jack
Jack

Reputation: 1902

Try with INNER JOIN syntax:

select file.name
    from file inner join record using(file_id)
where file.category_id=? and record.create_date=? and record.type=?

with index(file_id, create_date, type) for record table.

Upvotes: 1

Dan Bracuk
Dan Bracuk

Reputation: 20794

Join the tables.

select f.name
from f join record r on f.file_id = r.file_id
where f.category = ?

Upvotes: 1

Related Questions