I have a column of int[] type. now I want to convert int[] value into rows without using unnest() function. I want to create my own function which takes int[] as input and convert it into rows.
How can I do this?
- line_info table
line_id bigint
data text
- unique_wordhash_mappings table
wordhash
line_ids bigint[]
explain(analyze,buffers)
select
line_id , line_text
from line_info
where line_id in (
select unnest(line_ids) as line_id
from unique_wordhash_mappings
where wordhash in (22472126689,24578126689,109225126689,115504126689)
)
and explain is:
Nested Loop (cost=41.23..1455.72 rows=1188170 width=135) (actual time=0.886..10.205 rows=2372 loops=1)
Buffers: shared hit=9511
-> HashAggregate (cost=40.80..42.80 rows=200 width=8) (actual time=0.871..1.298 rows=2372 loops=1)
Group Key: unnest(unique_wordhash_mappings.line_ids)
Buffers: shared hit=19
-> ProjectSet (cost=0.42..35.80 rows=400 width=8) (actual time=0.039..0.274 rows=2383 loops=1)
Buffers: shared hit=19
-> Index Scan using unique_wordhash_mappings_word_hash_idx on unique_wordhash_mappings (cost=0.42..33.77 rows=4 width=32) (actual time=0.033..0.059 rows=4 loops=1)
Index Cond: (word_hash = ANY ('{22472126689,24578126689,109225126689,115504126689}'::bigint[]))
Buffers: shared hit=16
-> Index Scan using line_id_pkey on line_info (cost=0.43..8.39 rows=1 width=135) (actual time=0.003..0.003 rows=1 loops=2372)
Index Cond: (line_id = (unnest(unique_wordhash_mappings.line_ids)))
Buffers: shared hit=9492
Planning time: 0.378 ms
Execution time: 10.401 ms
unnest()? – Vérace May 21 '18 at 08:38VALUESconstructor or something. But do you think you could elaborate a little on why you want to do this? That information might make your question more useful. – Andriy M May 21 '18 at 08:38unnest(). I don't think that's possible. – Erwin Brandstetter May 21 '18 at 14:40unique_wordhash_mappings, but it might be worth a try. Something like: http://dpaste.com/33GXC9G Normalizing the data-model and providing proper indexes could also be beneficial. Another option might be anexistsquery instead: http://dpaste.com/13TP2WY – May 22 '18 at 14:59