Answers.Body: <p>As <a href="https://craftcms.stackexchange.com/questions/26160/does-craft-do-joined-calls-or-multiple-ones-when-fetching-the-related-items-el#comment34380_26160">Robin</a> mentioned in a comment, this is exactly what <a href="https://docs.craftcms.com/v2/templating/eager-loading-elements.html" rel="nofollow noreferrer">eager loading</a> was designed for... bringing in related elements in a single element query call.</p>
  
  <p>The above link shows how to do it from Twig, but you can do it from PHP as well: <a href="https://straightupcraft.com/articles/examples-of-eager-loading-elements-in-twig-and-php" rel="nofollow noreferrer">https://straightupcraft.com/articles/examples-of-eager-loading-elements-in-twig-and-php</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-15T23:51:29.597Z
Answers.LastActivityDate: 2018-05-15T23:51:29.597Z
Answers.UserDisplayName: Brad Bell
Body: <p>I'm checking <a href="https://github.com/craftcms/element-api" rel="nofollow noreferrer" title="Element API">Element API</a>'s performance to decide if it's a good idea to replace my own custom API with <strong>Element API</strong>. Right now, when fetching the related items (Via fields or matrix elements), my API creates a unique joined query with all the right criteria and executes it at the end.</p>
  
  <p>As Element API is able to use all the nice methods from Craft, maybe the question is related to Craft structure itself.</p>
  
  <p>I've tried using MySQL´s <strong>SHOW STATUS LIKE 'Com_Select'</strong> but it was not really useful.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-15T11:27:20.713Z
Comments.Text: You know you can include a `with` parameter to all `ElementQueries`in order to eager load the elements, thus you'll only have one single query for all elements and all related elements instead of multiple?
Comments.UserDisplayName: Robin Schambach
Tags: <plugin-elementapi><mysql>
Title: Does Craft do joined calls or multiple ones when fetching the related items? [Element API]
ThreadText: # Does Craft do joined calls or multiple ones when fetching the related items? [Element API]
  
  (Asked by: username_0 on 2018-05-15)
  
  I'm checking [Element API](https://github.com/craftcms/element-api "Element API")'s performance to decide if it's a good idea to replace my own custom API with **Element API**. Right now, when fetching the related items (Via fields or matrix elements), my API creates a unique joined query with all the right criteria and executes it at the end.
  
  As Element API is able to use all the nice methods from Craft, maybe the question is related to Craft structure itself.
  
  I've tried using MySQL´s **SHOW STATUS LIKE 'Com\_Select'** but it was not really useful.
  
  **Tags:** plugin-elementapi, mysql
  
  > **Comments:**
  > **username_1**: You know you can include a `with` parameter to all `ElementQueries`in order to eager load the elements, thus you'll only have one single query for all elements and all related elements instead of multiple?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2018-05-15. Score: 2 | Accepted answer
  
  As [username_1](https://craftcms.stackexchange.com/questions/26160/does-craft-do-joined-calls-or-multiple-ones-when-fetching-the-related-items-el#comment34380_26160) mentioned in a comment, this is exactly what [eager loading](https://docs.craftcms.com/v2/templating/eager-loading-elements.html) was designed for... bringing in related elements in a single element query call.
  
  The above link shows how to do it from Twig, but you can do it from PHP as well: <https://straightupcraft.com/articles/examples-of-eager-loading-elements-in-twig-and-php>
  
  
  ---
  
  