[英]Postgres Query taking long time
I have this SQL query which takes long time (~12s) to execute:我有这个 SQL 查询需要很长时间(~12s)才能执行:
select nextval('rec_id_seq_af') as rec_id,ds_id,ds_dt_id,
inv_vcf_id,inv_sample_id,inv_variant_id,ds_dt_line_seq from rs_data_1_af
group by ds_id,ds_dt_id,inv_vcf_id,inv_sample_id,inv_variant_id,ds_dt_line_seq
order by ds_id,ds_dt_id,inv_vcf_id,inv_sample_id,inv_variant_id,ds_dt_line_seq;
When I run:当我运行时:
EXPLAIN (ANALYZE, BUFFERS)
select nextval('rec_id_seq_af') as rec_id,ds_id,ds_dt_id,
inv_vcf_id,inv_sample_id,inv_variant_id,ds_dt_line_seq from rs_data_1_af
group by ds_id,ds_dt_id,inv_vcf_id,inv_sample_id,inv_variant_id,ds_dt_line_seq
order by ds_id,ds_dt_id,inv_vcf_id,inv_sample_id,inv_variant_id,ds_dt_line_seq;
This is the output:这是 output:
Group (cost=724728.48..780477.07 rows=314077 width=88) (actual time=10395.641..12546.322 rows=5703 loops=1)
Group Key: ds_id, ds_dt_id, inv_vcf_id, inv_sample_id, inv_variant_id, ds_dt_line_seq
Buffers: shared hit=80975, temp read=91041 written=91171
-> Sort (cost=724728.48..732580.39 rows=3140766 width=80) (actual time=10395.619..12019.351 rows=3140766 loops=1)
Sort Key: ds_id, ds_dt_id, inv_vcf_id, inv_sample_id, inv_variant_id, ds_dt_line_seq
Sort Method: external merge Disk: 286312kB
Buffers: shared hit=75272, temp read=91041 written=91171
-> Seq Scan on rs_data_1_af (cost=0.00..106679.66 rows=3140766 width=80) (actual time=0.009..575.729 rows=3140766 loops=1)
Buffers: shared hit=75272
Planning Time: 0.478 ms
Execution Time: 12581.964 ms
When the records in rs_data_1_af table increases, I would have million of rows, this query takes hours to execute.当 rs_data_1_af 表中的记录增加时,我会有数百万行,这个查询需要几个小时才能执行。
How can I optimize?我该如何优化?
Why are you running nextval('rec_id_seq_af')
?你为什么要运行
nextval('rec_id_seq_af')
?
nextval()
advances the index and returns the next value. nextval()
推进索引并返回下一个值。 For each row it has to do that, read/write in isolation, no wonder it is slow.对于每一行,它必须这样做,独立地读/写,难怪它很慢。
What is the purpose there, perhaps you can use generate_series()
or something else?那里的目的是什么,也许您可以使用
generate_series()
或其他东西?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.