Filter for articles with at least one comment
I am trying to migrate to Drizzle from Prisma and I am having issue with being able to query for all Articles that has at least one Comment as the title says.
Here's a simplified version of my schema:
I have tried a bunch of iterations of the following:
Except this, gives me a syntax error at "(" ........
I hope someone can help me out understand my mistake and misunderstanding here as if I can handle this, every other query filter
should easily be migrated from Prisma in my code base. (hopefully!)
Thanks in advance!
11 Replies
There a couple ways to do this
The easiest is using the RQB like you
Another one is with an inner join
You can also do it with a subquery:
Thanks a lot, the first simple option worked fantasticly! I have a follow up question concerning the subquery option. When would this be a better option? Is it perhaps in conjunction with prepared statements or something? Having serveral options is fantastic but even better if I would get an understanding of when 🙂
The problem I see with this approach is that it probably won't scale well, as the comments get more numerous the array in which you have to search will be a lot larger
BTW, the first and third approach are equivalent, notice how they use exactly the same
inArray
yep
You'll have to run for benchmarks if you want to be sure with one you should use
I think if you use selectDistict in the subquery it might be more efficient
Run select analyze on the resulting queries to see how bad they are
Alright, just feels wierd fetching articles from comments.
oH, how would I do that?
sorry not select analyze, explain analyze
Oh wait, in sqlite it's explain query plan
https://www.sqlite.org/eqp.html
I'll check that out, thanks! I suppose it might not be outrageous to add some boolean flag or count on the articles table for when there are comments perhaps. Making this query filter even easier
Don't optimize too early
This would be worrisome after 500_000 comments I guess
Thanks for the warning. In fact, these tables are not actually articles and comments, it's about things that I don't ever reach the 100_000'nds. Thank you very much Angelelz, taken down all your answers as reference notes 🙂 You're brilliant!
Is there an analyze command for Drizzle (and SQLite better-sqlite3)?