复杂查询优化

时间:2011-07-08 19:46:13

标签: sql postgresql query-optimization

运行Postres 7.4(是的升级)

这是一个视图查询,它使用了很多但需要的时间比我想要的要长。任何优化建议?名称中包含Id的任何字段都已编入索引

SELECT  
db_tbl_1.field_id, s."Field ID" AS foreign_field_id,
s."Field Name" AS field_name,
CASE    
    WHEN SUBSTRING(s."Field ID" FROM 0 FOR 3) = '01' THEN 'Field Label 1'
    WHEN SUBSTRING(s."Field ID" FROM 0 FOR 4) = '321' THEN 'Field Label 1' 
    WHEN SUBSTRING(s."Field ID" FROM 0 FOR 5) = '1234' THEN 'Field Label 1'
    WHEN SUBSTRING(s."Field ID" FROM 0 FOR 6) = '55555' THEN 'Field Label 1'    
    WHEN SUBSTRING(s."Field ID" FROM 0 FOR 3) = '76' THEN 'Field Label 2' 
END AS new_field, s.field_1,
db_tbl_2.field_2, db_tbl_1.field_2_a, 
db_tbl_1.field_3, db_tbl_1.field_4,
db_tbl_1.field_5, db_tbl_1.field_6, db_tbl_1.field_date_1,
db_tbl_1.field_7, db_tbl_2.field_8 AS new_field_name,
s."Field Date" AS field_date, db_tbl_1.created_date,
CASE
    WHEN (((DATE_TRUNC('month', "Field Date") + INTERVAL '2 MONTH') - "Field Date" > 60) AND 
         ((DATE_TRUNC('month', "Field Date") + INTERVAL '2 MONTH') - "Field Date" <= 92))
    THEN (DATE_TRUNC('month', "Field Date") + INTERVAL '2 MONTH')
    -- the most days in a three month span can be 92
    ELSE (DATE_TRUNC('month', "Field Date") + INTERVAL '3 MONTH')
END AS next_date
FROM db_schema_1.db_tbl_1, db_schema_1.db_tbl_2, "Table S" AS s
WHERE db_tbl_1.program_level_id = db_tbl_2.id
AND db_tbl_1.field_id = s."Another ID"
AND (CASE   
    WHEN SUBSTRING(s."Field ID" FROM 0 FOR 3) = '01' THEN 1
    WHEN SUBSTRING(s."Field ID" FROM 0 FOR 4) = '321' THEN 1 
    WHEN SUBSTRING(s."Field ID" FROM 0 FOR 5) = '1234' THEN 1
    WHEN SUBSTRING(s."Field ID" FROM 0 FOR 6) = '55555' THEN 1  
    WHEN SUBSTRING(s."Field ID" FROM 0 FOR 3) = '76' THEN 1
    ELSE 0 
END) = 1

1 个答案:

答案 0 :(得分:2)

尝试更改最后一个语句,使用case语句,因为查询将评估所有语句。然后检查是否等于1,你应该尝试“OR”语句,因为如果一个是正确的,它将不会评估其他选择。它应该对你有所帮助。这样的事情。

AND ( SUBSTRING(s."Field ID" FROM 0 FOR 3) = '01' OR
    SUBSTRING(s."Field ID" FROM 0 FOR 4) = '321' OR
    SUBSTRING(s."Field ID" FROM 0 FOR 5) = '1234' OR
    SUBSTRING(s."Field ID" FROM 0 FOR 6) = '55555' OR
    SUBSTRING(s."Field ID" FROM 0 FOR 3) = '76'
)