Reputation: 161
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
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
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
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
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
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