How to implement WITH clause in Peewee ORM

20 views
Skip to first unread message

Ramesh Pradhan

unread,
Jun 15, 2021, 4:09:56 AM6/15/21
to peewee-orm
I want to implement following query in Peewee ORM:

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

How can I implement below raw query?

Also,  a quick question. I have lots of values (more than 10K) in IN operations. And it takes lot of time to execute that query and my application freezes out. Is there any better solution replacing postgres IN operation?

For e.g.
all_blogs_ids = [...]  # more than10K ids
Blog.select().where(id.in_(all_blogs_ids)


Charles Leifer

unread,
Jun 15, 2021, 9:01:18 AM6/15/21
to peewe...@googlegroups.com
Have you checked the docs for this? There is a whole section devoted to common table expressions: http://docs.peewee-orm.com/en/latest/peewee/querying.html#common-table-expressions

Performance-wise, why wouldn't you just write:

select t.* from the_table where t.tag IN ('tag1', 'tag2', 'tag3')

Having that many items in your IN clause indicates a bigger design problem, in my opinion, so it's hard to offer much advice.

--
You received this message because you are subscribed to the Google Groups "peewee-orm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to peewee-orm+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/peewee-orm/936461e5-e8cf-4bad-bcf7-b6168b8c062an%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages