如何加速postgresql自连接查询
我在同一个表上使用join一次获取50行,但是需要20秒才能获取50行如何加速postgresql自连接查询,postgresql,Postgresql,我在同一个表上使用join一次获取50行,但是需要20秒才能获取50行 Select Distinct ON (S1.service) S1.account,S1.stid,S1.receiver,S1.identifier, S1.binfo,S2.dlr_mask,S2.time,S1.msgdata,S1.msg_cost from sql_sent_sms S1 left join sql_sent_sms S2 on S1.service = S2.s
Select Distinct ON (S1.service) S1.account,S1.stid,S1.receiver,S1.identifier,
S1.binfo,S2.dlr_mask,S2.time,S1.msgdata,S1.msg_cost
from sql_sent_sms S1
left join sql_sent_sms S2
on S1.service = S2.service
where S2.time Between :senttime and :endtime
and S1.userid=:userid
Order By S1.service");
我已经创建了关于时间、服务、用户ID参数的索引
但这没有多大帮助
我还应该做些什么来加速
这是分析查询的结果
EXPLAIN ANALYZE Select Distinct ON (S1.service) S1.account,S1.stid,S1.receiver,S1.identifier,S1.binfo,S2.dlr_mask,S2.time,S1.msgdata,S1.msg_cost
from sql_sent_sms S1
left join sql_sent_sms S2
on S1.service = S2.service
where S2.time Between '1459759193' and '1459849193'
and S1.userid='10412144'
Order By S1.service , S2.dlr_mask asc limit 50;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=56635220.85..56938614.70 rows=50 width=308) (actual time=1.177..1.177 rows=0 loops=1)
-> Unique (cost=56635220.85..57035700.74 rows=66 width=308) (actual time=1.176..1.176 rows=0 loops=1)
-> Sort (cost=56635220.85..56835460.79 rows=80095977 width=308) (actual time=1.176..1.176 rows=0 loops=1)
Sort Key: s1.service, s2.dlr_mask
Sort Method: quicksort Memory: 25kB
-> Nested Loop (cost=1.24..127912.78 rows=80095977 width=308) (actual time=1.166..1.166 rows=0 loops=1)
-> Index Scan using idx_sentmsgbydate on sql_sent_sms s1 (cost=0.43..21247.62 rows=5765 width=292) (actual time=0.035..0.091 rows=34 loops=1)
Index Cond: ((userid)::text = '10412144'::text)
-> Index Only Scan using idx_sentmsgandstauts on sql_sent_sms s2 (cost=0.81..18.45 rows=5 width=126) (actual time=0.030..0.030 rows=0 loops=34)
Index Cond: ((service = (s1.service)::text) AND ("time" >= 1459759193::bigint) AND ("time" <= 1459849193::bigint))
Heap Fetches: 0
Planning time: 2.471 ms
Execution time: 1.269 ms
(13 rows)
这是jpa问题还是postgresql问题?性能问题应包括解释和分析表大小、索引、当前时间性能、期望时间、,等等。Slow是一个相对术语,我们需要一个实际值进行比较。如果要筛选表S2.time介于“1459759193”和“1459849193”之间,则左连接的目的是什么?@kirti其中S2.time介于:senttime和:endtime引用S2 RTE有效地将左连接更改为普通连接。另外:一些关于服务和时间的组合,顺便说一句,在这里可能会有所帮助。预期行数和观察行数表明缺少有效的统计数据。我想他指的是表中两个字段上的复合索引,如创建索引idx\U sql\U发送的\U短信服务上的某物,时间;这是jpa问题还是postgresql问题?性能问题应包括解释和分析表大小、索引、当前时间性能、期望时间、,等等。Slow是一个相对术语,我们需要一个实际值进行比较。如果要筛选表S2.time介于“1459759193”和“1459849193”之间,则左连接的目的是什么?@kirti其中S2.time介于:senttime和:endtime引用S2 RTE有效地将左连接更改为普通连接。另外:一些关于服务和时间的组合,顺便说一句,在这里可能会有所帮助。预期行数和观察行数表明缺少有效的统计数据。我想他指的是表中两个字段上的复合索引,如创建索引idx\U sql\U发送的\U短信服务上的某物,时间;