我有这张桌子,上面有Payment_id的索引(未显示):
CREATE TABLE myschema.payments
(
payment_id bigint NOT NULL,
box_id bigint ,
mov_id bigint ,
code_co character varying(5) NOT NULL,
client_id bigint NOT NULL,
user_created character varying(15) NOT NULL,
date_payment timestamp without time zone NOT NULL,
)
;
此表有近3000万条记录
我有一个这样的测试表
insert into dummy_table (payment_id) values (294343, 5456565);
对此查询的解释分析将在大约4分钟内检索到结果:
select * from myschema.payments where payment_id in (select payment_id from dummy_table )
但是,如果我执行以下操作:
select * from myschema.payments where
payment_id in (294343, 5456565);
我得到的结果以毫秒为单位。
这些payment_id的值是可变的,我如何通过每次执行时使用不同数量的payment_id来提高性能?如果有帮助,我的“ in”语句每次将有大约20个payment_id。
这是对查询select * from myschema.payments的解释分析,其中,payment_id位于(从dummy_table中选择payment_id)
"Nested Loop Semi Join (cost=100.00..6877.47 rows=137 width=274) (actual time=47229.725..215893.809 rows=2 loops=1)"
" Join Filter: (payments.payment_id = dummy_table.payment_id)"
" Rows Removed by Join Filter: 47939387"
" -> Foreign Scan on payments (cost=100.00..118.22 rows=274 width=274) (actual time=1.334..198599.055 rows=23969695 loops=1)"
" -> Materialize (cost=0.00..6751.03 rows=2 width=8) (actual time=0.000..0.000 rows=2 loops=23969695)"
" -> Seq Scan on dummy_table (cost=0.00..6751.02 rows=2 width=8) (actual time=0.009..6.236 rows=2 loops=1)"
"Planning time: 0.238 ms"
"Execution time: 215894.462 ms"
编辑:添加了对联接版本的解释分析:
select p.*
from myschema.payments p join
dummy_table t
on p.payment_id = t.payment_id;
"Nested Loop (cost=100.00..6877.47 rows=3 width=274) (actual time=50680.577..228816.409 rows=2 loops=1)"
" Join Filter: (payments.payment_id = dummy_table.payment_id)"
" Rows Removed by Join Filter: 47939388"
" -> Foreign Scan on payments p (cost=100.00..118.22 rows=274 width=274) (actual time=1.261..211380.739 rows=23969695 loops=1)"
" -> Materialize (cost=0.00..6751.03 rows=2 width=8) (actual time=0.000..0.000 rows=2 loops=23969695)"
" -> Seq Scan on dummy_table t (cost=0.00..6751.02 rows=2 width=8) (actual time=0.022..9.566 rows=2 loops=1)"
"Planning time: 0.311 ms"
"Execution time: 228817.094 ms"
答案 0 :(得分:1)
尝试使用join
:
select p.*
from myschema.payments p join
dummy_table t
on p.payment_id = t.payment_id;
尝试此版本。 。 。蛮力的:
select p.*
from dummy_table t left join
myschema.payments p
on p.payment_id = t.payment_id;