Body: <p>I'm trying to show the nutrition information for restaurants using the following snippet</p>
  
  <pre><code>                    {% set nutrition = craft.entries.section('nutrition').Ids() %}
  
                      {% set entries = craft.entries ({
                          section: 'restaurants',
                          orderBy: 'title',
                          limit: 50,
                          relatedTo: nutrition
                      }) %}
  </code></pre>
  
  <p>The nutriton channel has around 300k entries and around 200k entries in restaurants channel. Dedicated server with 16gb ram and 8 vcpus can't really handle it. Load average is above 4 and CPU utilization by mysql is 100%-300%. Page loads forever with 550 ram used according to dev toolbar.</p>
  
  <p>Is there a way to speed the things up?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-05-09T02:21:14.337Z
Comments.Text: I think we'll need to know a bit more about the results you're expecting, in order to guide you... it looks like you're just trying to find the first 50 restaurants that have _any kind of nutrition information_ associated with them?
Comments.UserDisplayName: August Miller
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-05-09T12:57:47.600Z
Comments.Text: Correct. The goal is to display restaurants that have nutrition data associated with it.
Comments.UserDisplayName: Mark H.
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-05-11T03:58:58.867Z
Comments.Text: How are the restaurant entries being related to the nutrition entries? Depending on how you're relating them, would [``:notempty:``](https://docs.craftcms.com/v3/entries-fields.html#templating) work on the related field instead? That would eliminate having to pull down 300K entry IDs right off the bat...
Comments.UserDisplayName: RitterKnight
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-05-11T11:12:29.757Z
Comments.Text: They are not related. Nutrition entries have "entries" field pointing to the proper restaurant instead.
Comments.UserDisplayName: Mark H.
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-05-11T19:33:10.460Z
Comments.Text: Mark H: But they are related "the other way around" (that is, the nutrition entries point to or relate to the restaurant entries).  Are there nutrition entries that don't point to any restaurant? If so you can help Craft save some CPU by only querying for the IDs that point back to restaurants.
Comments.UserDisplayName: RitterKnight
Tags: <performance><speed><optimization>
Title: Optimize heavy query
ThreadText: # Optimize heavy query
  
  (Asked by: username_0 on 2020-05-09)
  
  I'm trying to show the nutrition information for restaurants using the following snippet
  
  ```
                      {% set nutrition = craft.entries.section('nutrition').Ids() %}
  
                      {% set entries = craft.entries ({
                          section: 'restaurants',
                          orderBy: 'title',
                          limit: 50,
                          relatedTo: nutrition
                      }) %}
  
  ```
  
  The nutriton channel has around 300k entries and around 200k entries in restaurants channel. Dedicated server with 16gb ram and 8 vcpus can't really handle it. Load average is above 4 and CPU utilization by mysql is 100%-300%. Page loads forever with 550 ram used according to dev toolbar.
  
  Is there a way to speed the things up?
  
  **Tags:** performance, speed, optimization
  
  > **Comments:**
  > **username_1**: I think we'll need to know a bit more about the results you're expecting, in order to guide you... it looks like you're just trying to find the first 50 restaurants that have _any kind of nutrition information_ associated with them?  
  > **username_0**: Correct. The goal is to display restaurants that have nutrition data associated with it.  
  > **username_2**: How are the restaurant entries being related to the nutrition entries? Depending on how you're relating them, would [``:notempty:``](https://docs.craftcms.com/v3/entries-fields.html#templating) work on the related field instead? That would eliminate having to pull down 300K entry IDs right off the bat...  
  > **username_0**: They are not related. Nutrition entries have "entries" field pointing to the proper restaurant instead.  
  > **username_2**: username_0: But they are related "the other way around" (that is, the nutrition entries point to or relate to the restaurant entries).  Are there nutrition entries that don't point to any restaurant? If so you can help Craft save some CPU by only querying for the IDs that point back to restaurants.  
  
  ---
  
  ## 0 answers
  
  