我有一个带有子选择的选择where
:
select * from injections where id in([some select]);
它使用索引,一切都很好。但是现在,当我将子选择语句移至函数中并执行以下操作时:
select * from injections where id in(select allowed_resources(...))
它停止对 ID 列使用索引:
Hash Join (cost=22.27..68.29 rows=193 width=759) (actual time=0.318..0.503 rows=1 loops=1)
Hash Cond: ((injections.id)::text = (allowed_resources('{8QxwVyBm8Qc,8QwU6z5DpxY,8QihwxnwHFz}'::text[], '{8Qihv0yDFFA,r}'::text[], 4)))
-> Seq Scan on injections (cost=0.00..42.86 rows=386 width=759) (actual time=0.021..0.166 rows=386 loops=1)
-> Hash (cost=19.77..19.77 rows=200 width=32) (actual time=0.276..0.277 rows=2 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 9kB
-> HashAggregate (cost=17.77..19.77 rows=200 width=32) (actual time=0.272..0.274 rows=2 loops=1)
Group Key: allowed_resources('{8QxwVyBm8Qc,8QwU6z5DpxY,8QihwxnwHFz}'::text[], '{8Qihv0yDFFA,r}'::text[], 4)
Batches: 1 Memory Usage: 40kB
-> ProjectSet (cost=0.00..5.27 rows=1000 width=32) (actual time=0.259..0.268 rows=2 loops=1)
-> Result (cost=0.00..0.01 rows=1 width=0) (actual time=0.001..0.001 rows=1 loops=1)
我认为这是因为它无法分析函数内部的 SQL 并考虑最坏的情况 - 返回大量数据。
是否有可能对此产生影响?