Back

Database experts, should I use a separate query or a subquery? πŸ€”

For example, I want to retrieve the author's name for all the posts. Should I add a subquery to fetch that name, or use a separate query to get the names and then map them with the posts? 🧐

6 β€’ β€’

275

β€’
  • No matching results...
  • Searching...

/ 1000

neither nor. use joins within your query.
- have you considered primary and foreign keys in the database schema?

β€’ β€’

50

β€’

Basically if we talk about laravel then it was
with() vs withAggregate().

First case $post->author->name and 2nd case $post->author_name 🧐

β€’ β€’

50

β€’

Well depends on the context

I would go with seperate queries. If the seperate model needs to be used somewhere down the line.

Otherwise I would go with joins

β€’ β€’

52

β€’

For small database we can use joins.

For large data you want to Denormalise and make separate queries, you can partition , you can even keep data in separate databases, maybe you can keep 1 part of the data in cache which relatively changes less.

β€’ β€’

63

β€’

Make it complicated πŸ˜€
- Create a new col in post with author name
- Add auto update when author model change in a async queue
After that, u can own a lambo πŸš€

1 β€’ β€’

34

β€’

It depends! I would go with `withAggregate()` or `join`

β€’ β€’

110

β€’