如何减少查询时间-Postgresql

问题描述

Postgresql 9.6.9,由Visual C ++ build 1800 64位编译

我有一个关于查询间的问题。该“视图”具有+ 1M数据,当您执行view *的select *时,其查询时间为+400秒。在我的查询中,它返回10k数据,查询时间约为8-10秒。我正在寻找一种将其减少到1或2秒的方法。我尝试在where或select子句中更改或删除内容,但是我想到的唯一解决方案是限制“时间”部分之间的时间。

如果有人知道更好的解决方案,将不胜感激!

谢谢, 可以

注意:此视图包含三个不同的表(cl_segments,cl_participants,cl_party_info)。 我找不到这些表的定义,但是我将添加索引和字段的图片。如果您需要其他任何信息,请告诉我。

clparticipantfields

clparticipantindexes

clpartyinfofields

clpartyinfoindexes

clsegmentsfields

clsegmentsindexes

EXPLAIN ANALYZE BUFFERS

select distinct on (clsgview.call_id)clsgview.call_id as call_id,case when clsgview.src_dn_type = '0' and clsgview.dst_dn_type = '0' then
right(clsgview.src_dn,11)
when clsgview.src_dn_type = '1' and clsgview.dst_dn_type = '0' then
right(clsgview.src_caller_number,11)
when clsgview.src_dn_type = '0' and clsgview.dst_dn_type = ANY (ARRAY[1,12,13]) then 
right(clsgview.src_dn,11) end as from,case when clsgview.src_dn_type = '0' and clsgview.dst_dn_type = '0' then
right(clsgview.dst_dn,11)
when clsgview.src_dn_type = '1' and clsgview.dst_dn_type = '0' then
right(clsgview.dst_dn,13]) then
right(clsgview.dst_display_name,11) end as to,case when clsgview.act = ANY (ARRAY[5,6]) and clsgview.dst_dn_type != ANY (ARRAY[12]) then (end_time-start_time) else '00:00:00' end as talktime

from 
( SELECT s.call_id,s.id AS seg_id,s.type AS seg_type,s.seq_order AS seg_order,s.start_time,s.end_time,si.dn_type AS src_dn_type,si.dn AS src_dn,si.display_name AS src_display_name,si.firstlastname AS src_firstlastname,si.caller_number AS src_caller_number,dp.start_time AS dst_start_time,dp.answer_time AS dst_answer_time,dp.end_time AS dst_end_time,dp.billing_rate AS dst_billing_rate,di.dn_type AS dst_dn_type,di.dn AS dst_dn,di.display_name AS dst_display_name,di.firstlastname AS dst_firstlastname,di.caller_number AS dst_caller_number,di.dn_class AS dst_dn_class,s.action_id AS act,ai.dn_type AS act_dn_type,ai.dn AS act_dn,ai.display_name AS act_display_name,ai.firstlastname AS act_firstlastname
   FROM ((((((cl_segments s
     JOIN cl_participants sp ON ((sp.id = s.src_part_id)))
     JOIN cl_participants dp ON ((dp.id = s.dst_part_id)))
     JOIN cl_party_info si ON ((si.id = sp.info_id)))
     JOIN cl_party_info di ON ((di.id = dp.info_id)))
     LEFT JOIN cl_participants ap ON ((ap.id = s.action_party_id)))
     LEFT JOIN cl_party_info ai ON ((ai.id = ap.info_id)))
         order by seg_id desc
) as clsgview
where clsgview.src_dn_type = ANY (ARRAY[0,1]) 
and clsgview.dst_dn_type = ANY (ARRAY[0,1,13]) 
and clsgview.act != ANY (ARRAY[1]) and ((clsgview.act = ANY (ARRAY[15,101,102,103,107,400,409,418,419,421,423])) or (clsgview.act = ANY (ARRAY[5,6]) and clsgview.dst_dn_type = ANY (ARRAY[12])))
and clsgview.dst_display_name not like '*%' and clsgview.dst_caller_number not like '*%'
and (clsgview.src_dn = '1004' or clsgview.dst_dn = '1004')  
and start_time::date BETWEEN '2020-01-01'::DATE and Now()::DATE  
order by clsgview.call_id desc

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)