2012-11-21 4 views
2

다음 쿼리 실행 속도를 높이려면 어떻게해야합니까? 37 개의 레코드를 검색하는 데 단지 65 초가 걸렸습니다. 어떤 솔루션 녀석들? (저는 gcc (GCC) 4.4.6 20120305 (Red Hat 4.4.6-4), 64-bit에서 컴파일 된 x86_64-unknown-linux-gnu에서 PostgreSQL 9.1.6을 사용하고 있습니다. 이것은 쿼리입니다 :PostgreSQL에서 복잡한 쿼리의 속도를 높이는 방법

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' 

그리고이 인 분석 설명 출력 (see also on 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 
+0

이 쿼리에 대한 Explain 분석 결과를 게시 할 수 있습니까? 내가 볼 수있는 잠재적 인 문제는 postgres가 인덱스의 올바른 값을 직접 조회 할 수 없기 때문에 where 절에서 함수를 사용하는 것입니다. – Eelke

+0

-1 당신이 설치에 대한 정보를 제공해야하기 때문에 :'explain analyze', tables 구조로 시작하십시오. 'postgresql'태그는 좋은 팁으로 연결됩니다 : http://wiki.postgresql.org/wiki/ Guide_to_reporting_problems – mys

+0

제공 할 정보에 대한 조언은 http://stackoverflow.com/tags/postgresql-performance/info를 참조하십시오. –

답변

3

중요한 문제 f_get_territory_id 및 f_get_cluster 기능에있을 수 있습니다 -로 (WHERE에 강하게 (술어 가입) 절을하지 않는 것이 좋습니다 FROM 기능을 사용하여 함수 인덱스를 사용하는 경우 예외이므로 이러한 함수는 변경 불가능해야합니다.

관련 문제