Note that there are some explanatory texts on larger screens.

plurals
  1. POA single query with many joins in one API request, or a few queries with some joins in separate API requests?
    primarykey
    data
    text
    <p>What is best practice, what delivers the best performance?</p> <p>I currently have a query with many <code>LEFT JOIN</code>s that fetches a <code>user</code> and all his data, like friends, friend requests, and so on:</p> <pre><code>SELECT `user`.`id` AS `user_id`, `user`.`name` AS `user_name`, `manager`.`id` AS `manager_id`, `competition`.`id` AS `manager_competition_id`, `competition`.`name` AS `manager_competition_name`, `competition`.`week` AS `manager_competition_week`, `country`.`id` AS `manager_competition_country_id`, `country`.`name` AS `manager_competition_country_name`, `club_template`.`id` AS `manager_club_template_id`, `club_template`.`name` AS `manager_club_template_name`, `club`.`id` AS `manager_club_id`, `club`.`name` AS `manager_club_name`, `club`.`ready` AS `manager_club_ready`, `friend`.`friend_id` AS `friend_id`, `friend_user`.`name` AS `friend_name` FROM `users` AS `user` LEFT JOIN `managers` AS `manager` ON `manager`.`user_id` = `user`.`id` LEFT JOIN `competitions` AS `competition` ON `competition`.`id` = `manager`.`competition_id` LEFT JOIN `countries` AS `country` ON `country`.`id` = `competition`.`country_id` LEFT JOIN `club_templates` AS `club_template` ON `club_template`.`id` = `manager`.`club_template_id` LEFT JOIN `clubs` AS `club` ON `club`.`id` = `manager`.`club_id` LEFT JOIN `friends` AS `friend` ON `friend`.`user_id` = `user`.`id` LEFT JOIN `users` AS `friend_user` ON `friend_user`.`id` = `friend`.`friend_id` WHERE `user`.`id` = 1 </code></pre> <p>As you can see, it's a very big query. My reasoning behind this was that it's better to have just one query that can be done in one API request, like this...</p> <pre><code>/api/users/1 </code></pre> <p>...versus a few queries, each in their own API request, like this...</p> <pre><code>/api/users/1 /api/users/1/friends /api/users/1/friend_requests /api/users/1/managers </code></pre> <p>But now I'm worried, that since it's become such a huge query that it will actually hurt performance more than to split it up in separate API requests.</p> <p>What will scale better?</p> <p><strong>Update</strong></p> <p>I've changed the query to the full query. This is not the final query; I plan to add even more joins (or not, depends on the answer).</p> <p>Each table has a <code>PRIMARY KEY</code> on <code>id</code>. All association columns (<code>competition_id</code>, <code>club_id</code>, and so on) have a regular <code>INDEX</code>. The database engine is InnoDB.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload