“选择案例”表现问题

时间:2016-05-11 11:19:20

标签: sql rdbms

我在下面提到的SQL查询中面临性能问题:

select column1, 
   (SELECT CASE fl.ISDELETED WHEN 'T' THEN 'T' ELSE 'F' END  FROM FILE fl
     WHERE lower(fl.FILE_TYPE) LIKE 'doc' AND fl.FILE_ID=doc.FILE_ID   ) AS alias1 ,
   (SELECT CASE fl.ISDELETED  WHEN 'T'  THEN 'T' ELSE 'F'  END FROM FILE fl
     WHERE lower(fl.FILE_TYPE) LIKE 'xls'   AND fl.FILE_ID=doc.FILE_ID ) AS alias2

FROM DOC doc

select case语句是否有任何替代(JOIN或任何其他选项),以便执行时间更短?我从java代码

调用此查询

任何形式的帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

您可以使用明确的join

来编写此内容
select d.column1,
       (case when f.isdeleted = 'T' and lower(f.file_type) = 'doc' then 'T'
             when lower(f.file_type) = 'doc' then 'F'
        end) as alias1,
       (case when f.isdeleted = 'T' and lower(f.file_type) = 'xls' then 'T'
             when lower(f.file_type) = 'doc' then 'F'
        end) as alias2             
from doc d left join
     file f
     on f.file_id = d.file_id;

要获得性能,您需要file(file_id, file_type, isdeleted)上的索引。

答案 1 :(得分:0)

Change your FILE_TYPE to be a definite thing, rather than like i.e. migrate it all as a one-off update to be lower-case so you don't have to do conversions etc.

Remove the subselects and just use regular SQL

SELECT column1,
       f1.FILE_TYPE,
       CASE fl.ISDELETED WHEN 'T' THEN 'T' ELSE 'F' END  
FROM 
       FILE fl
WHERE 
       (fl.FILE_TYPE = 'doc' OR fl.FILE_TYPE = 'xls') AND 
       fl.FILE_ID=doc.FILE_ID