How to speed up the following query execution? it took 65s just to retrieve 37 records. Any solution guys? (I'm using PostgreSQL 9.1.6 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.4.6 20120305 (Red Hat 4.4.6-4), 64-bit) This is the query:
如何加快以下查询执行速度?它需要花费65秒来检索37条记录。任何解决方案? (我在x86_64-unknown-linux-gnu上使用PostgreSQL 9.1.6,由gcc(GCC)4.4.6 20120305(Red Hat 4.4.6-4)编译,64位)这是查询:
select cc.claim_id,
cc.claim_date,
CONCAT(cc.user_id, ' | ', uu.user_name) as user_name,
CONCAT(f_get_channel_id_by_territory_id(cc.territory_id), ' | ', f_get_channel_name(f_get_channel_id_by_territory_id(cc.territory_id))) AS channel_name,
f_get_cluster(cc.user_id) AS cluster_id,
ff.frontliner_name
from t_trx_card_claim cc join t_mtr_user uu on uu.user_id = cc.user_id
left join t_mtr_outlet_frontliner ff on f_get_territory_id(ff.outlet_id) = cc.territory_id
where f_get_cluster(cc.user_id) = '36'
And this is an explain analyze output (see also on explain.depesz.com):
这是一个解释分析输出(另见explain.depesz.com):
Nested Loop Left Join (cost=0.00..83503.84 rows=646 width=47) (actual time=2000.830..65689.982 rows=37 loops=1)
Join Filter: (f_get_territory_id(ff.outlet_id) = cc.territory_id)
-> Nested Loop (cost=0.00..433.50 rows=7 width=35) (actual time=174.417..198.364 rows=37 loops=1)
-> Seq Scan on t_trx_card_claim cc (cost=0.00..375.53 rows=7 width=21) (actual time=174.407..197.932 rows=37 loops=1)
Filter: (f_get_cluster(user_id) = 36)
-> Index Scan using ix_user_8 on t_mtr_user uu (cost=0.00..8.27 rows=1 width=22) (actual time=0.008..0.009 rows=1 loops=37)
Index Cond: ((user_id)::text = (cc.user_id)::text)
-> Materialize (cost=0.00..1811.51 rows=42701 width=21) (actual time=0.006..30.225 rows=42701 loops=37)
-> Seq Scan on t_mtr_outlet_frontliner ff (cost=0.00..1347.01 rows=42701 width=21) (actual time=0.003..27.457 rows=42701 loops=1)
Total runtime: 65690.524 ms
1 个解决方案
#1
3
Significant problem can be in functions f_get_territory_id and f_get_cluster - using functions in WHERE and FROM (JOIN predicate) clause is not recommended strongly (with exception if you use functional indexes / so these functions must be immutable).
重要问题可能出在函数f_get_territory_id和f_get_cluster中 - 强烈建议不要使用WHERE和FROM(JOIN谓词)子句中的函数(如果使用函数索引/,则异常,因此这些函数必须是不可变的)。
#1
3
Significant problem can be in functions f_get_territory_id and f_get_cluster - using functions in WHERE and FROM (JOIN predicate) clause is not recommended strongly (with exception if you use functional indexes / so these functions must be immutable).
重要问题可能出在函数f_get_territory_id和f_get_cluster中 - 强烈建议不要使用WHERE和FROM(JOIN谓词)子句中的函数(如果使用函数索引/,则异常,因此这些函数必须是不可变的)。