需要通过避免联合来优化查询。请帮助查询的其他选项如下

时间:2013-08-27 09:06:45

标签: sql db2

SELECT  image_name
    ,   document_id
    ,   document_name
    ,   document_category
    ,   workflow_state_name
    ,   container_name
    ,   modelyear
    ,   build_phase
    ,   revision_level
    ,   owner
    ,   container_id
    ,   document_description description
FROM container_document_view
WHERE document_id IS NOT NULL
UNION
SELECT DISTINCT 'icon_folder.gif' image_name
            ,   container_id document_id
            ,   container_name document_name
            ,   '' document_category
            ,   workflow_state_name
            ,   container_name
            ,   modelyear
            ,   build_phase
            ,   revision_level
            ,   owner
            ,   container_id
            ,   container_description description
FROM container_details_view
WHERE container_id NOT IN (
        SELECT container_id
        FROM document
    )
UNION
SELECT  image_name
    ,   document_id
    ,   document_name
    ,   document_category
    ,   workflow_state_name
    ,   container_name
    ,   modelyear
    ,   build_phase
    ,   revision_level
    ,   owner
    ,   container_id
    ,   document_description description
FROM container_link_view
WHERE container_id IS NOT NULL
ORDER BY container_name, container_id, document_name, modelyear, build_phase, revision_level

1 个答案:

答案 0 :(得分:0)

你为什么要避免它?

如果速度过慢,您可能会检查是否可以更改为UNION ALL,这样可以避免代价高昂的DISTINCT

另外,当您将NOT IN重写为NOT EXISTS时,{{1}}可能会更有效。