简体   繁体   English

提高SQL查询(DB2)的性能

[英]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. 我正在使用DB2。 Please help, thank you. 请帮忙,谢谢。

Table (FILE) (Data volume - around 100000) 表格(FILE)(数据量-大约100000)

  • FILE_ID FILE_ID
  • NAME 名称
  • CATEGORY 类别

INDEX (CATEGORY, FILE_ID) INDEX(类别,FILE_ID)

Table (RECORD) (Data volume - around 50000000) 表(记录)(数据量-约50000000)

  • RECORD_ID RECORD_ID
  • DATA 数据
  • CREATE_DATE CREATE_DATE
  • TYPE 类型
  • FILE_ID FILE_ID

INDEX(CREATE_DATE, TYPE, FILE_ID) 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 = ? )

Join the tables. 联接表。

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

Try with INNER JOIN syntax: 尝试使用INNER JOIN语法:

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. 带有记录表的索引(文件ID,创建日期,类型)。

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 = ?

the join should solve your issue. 加入应该可以解决您的问题。

also sometimes you need to reorganize (REORG) tables and indexes to improve performance on slow queries. 有时您还需要重新组织(REORG)表和索引,以提高慢查询的性能。

You might find that putting the "table file" into a temp table with the where clause speeds things up... 您可能会发现,使用where子句将“表文件”放入临时表中可以加快处理速度……

eg... 例如...

create a temp table then insert all records from Table File "Where Cateogry = ?" 创建一个临时表,然后从表文件“ Where Cateogry =?”中插入所有记录 and then join that table onto Record 然后将该表连接到记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM