Unix Technical Forum

SELECT * WHERE id IN (list of ids)

This is a discussion on SELECT * WHERE id IN (list of ids) within the Pgsql General forums, part of the PostgreSQL category; --> Playing with postgresql I have seen that sometime a very long IN (list of ids) can rise a max_recursion_error ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-09-2008, 11:39 AM
Matteo Bertini
 
Posts: n/a
Default SELECT * WHERE id IN (list of ids)

Playing with postgresql I have seen that sometime a very long IN (list
of ids) can rise a max_recursion_error (or something like that).

An easy workaround when the list is computer generated and EXISTS is
infeasible (too slow), is breaking the list in log(n) OR parts.

Like in this python snippet:

if len(candidates) > 2000:
step = int(len(candidates)/math.log(len(candidates)))
parts = []
for i in range(0,len(candidates),step):
candidates_list = ", ".join(map(str, candidates[i:i+step]))
parts.append("%(space)s_id IN (%(candidates_list)s)" % locals())
where_sql = "\nOR\n".join(parts)

This is an example run:
In [1]:a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

In [2]:for i in range(0,len(a),7):
...: print a[i:i+7]
...: i = i+7

[1, 2, 3, 4, 5, 6, 7]
[8, 9, 10, 11, 12, 13, 14]
[15]

In my (small) experience this trick can speeds-up a lot of queries of
this kind.

Bye,
Matteo Bertini

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:01 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com