Alternative when IN clause is inputed A LOT of values (postgreSQL)

最后都变了- 提交于 2020-01-03 05:51:08

问题


I'm using the IN clause to retrieve places that contains certain tags. For that I simply use

select .. FROM table WHERE tags IN (...)

For now the number of tags I provide in the IN clause is around 500) but soon (in the near future) number tags will probably jump off to easily over 5000 (maybe even more)

I would guess there is some kind of limition in both the size of the query AND in the number values in the IN clause (bonus question for curiosity what is this value?)

So my question is what is a good alternative query that would be future proof even if in the future I would be matching against let's say 10'000 tags ?

ps: I have looked around and see people mentioning "temporary table". I have never used those. How will they be used in my case? Will i need to create a temp table everytime I make a query ?

Thanks, Francesco


回答1:


One option is to join this to a values clause

with parms (tag) as (
  values ('tag1'), ('tag2'), ('tag3')
)
select t.*
from the_table t
  join params p on p.tag = t.tag;



回答2:


You could create a table using:

tablename

id  |   tags
----+----------
1   | tag1
2   | tag2
3   | tag3

And then do:

select .. FROM table WHERE tags IN (SELECT * FROM tablename)


来源:https://stackoverflow.com/questions/30053186/alternative-when-in-clause-is-inputed-a-lot-of-values-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!