How to implement WITH clause in Peewee ORM

Skip to first unread message

Ramesh Pradhan

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

Charles Leifer

Jun 15, 2021, 9:01:18 AM6/15/21
Have you checked the docs for this? There is a whole section devoted to 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
To view this discussion on the web visit
Reply all
Reply to author
0 new messages