在同一个表上调整具有多个内部联接的Sql查询

时间:2015-05-23 00:49:23

标签: postgresql

由于数据库表的结构,我必须通过在同一个表上执行一系列内部联接来执行以下查询。有没有办法优化这样的查询?

查询计划在此处:http://explain.depesz.com/s/vAvx

SELECT 
cch.id || '|' || ff.id || '|' || fc.id || '|' || tf.id || '|' || tc.id AS id,
cch.id as compare_cache_header_id, cch.client_id, --[ADDED]
base_env_adapter_id, changed_fund_config_id, type, 
true AS new_money, 'BOTH'::text AS online_transfer, 'BOTH'::text AS automatic_transfer, true AS short_term_fee, true AS rebalancing, '*'::text AS target_units, 'ALL_INTERFACES'::text AS trade_allowed,
from_fund_code, 
ff.en_short_display_name as from_fund_en_short_display_name, ff.en_med_display_name as from_fund_en_med_display_name, ff.en_long_display_name as from_fund_en_long_display_name, 
ff.fr_short_display_name as from_fund_fr_short_display_name, ff.fr_med_display_name as from_fund_fr_med_display_name, ff.fr_long_display_name as from_fund_fr_long_display_name, 
to_fund_code, tf.en_short_display_name as to_fund_en_short_display_name, tf.en_med_display_name as to_fund_en_med_display_name, tf.en_long_display_name as to_fund_en_long_display_name,
tf.fr_short_display_name as to_fund_fr_short_display_name, tf.fr_med_display_name as to_fund_fr_med_display_name, tf.fr_long_display_name as to_fund_fr_long_display_name, 
cct.from_class_code, fc.english_name as from_class_english_name, fc.french_name as from_class_french_name, fc.en_display_name as from_class_en_display_name, fc.fr_display_name as from_class_fr_display_name,
cct.to_class_code, tc.english_name as to_class_english_name, tc.french_name as to_class_french_name, tc.en_display_name as to_class_en_display_name, tc.fr_display_name as to_class_fr_display_name
FROM compare_cache_header cch
INNER JOIN compare_cache_transfer cct
ON cch.id = cct.compare_cache_header_id
INNER JOIN fund ff
ON cch.changed_fund_config_id = ff.fund_config_id and cct.from_fund_code = ff.fund_code
INNER JOIN fund tf 
ON cch.changed_fund_config_id = tf.fund_config_id and cct.to_fund_code = tf.fund_code
INNER JOIN class fc
ON cch.changed_fund_config_id = fc.fund_config_id and cct.from_class_code = fc.class_code
INNER JOIN class tc
ON cch.changed_fund_config_id = tc.fund_config_id and cct.to_class_code = tc.class_code
WHERE cct.type = 'ADD';

1 个答案:

答案 0 :(得分:0)

确保在所有ON子句和所有WHERE子句中的所有字段上都定义了索引。否则应该相当快。