Answers.Body: <p>Hum,</p>
  <p>The problem was very simple. I didn't understand what the query was about and it showed.</p>
  <p>I used <code>blockType:img</code> in my query thinking <code>blockType</code> was a keyword instead of using the actual block slug witch in my case is <code>image</code>.</p>
  <p>Once fixed, the number of queries did get under the non eager loaded version </p>
  <p>Here is the relevant part of the fixed code:</p>
  <pre class="lang-html prettyprint-override"><code>{% set articleBlocks = entry.articleBody
      .with([
          ['image:img', {
              withTransforms: ['articleHalf', 'articleHalf2x', 'articleFull', 'articleFull2x']
          }]
      ])
      .all() 
  %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-25T18:37:34.013Z
Answers.LastActivityDate: 2022-08-25T18:37:34.013Z
Answers.UserDisplayName: fdelaneau
Body: <p>From what I understand it is wise to always eager load assets, I used it successfully in other cases, but here it results in a more queries, not fewer.</p>
  <p>I am trying to eager load an asset in a matrix field from the current page entry and it seems I missed something </p>
  <p>Here are the 2 versions of my code:</p>
  <h4>Without eager loading</h4>
  <pre class="lang-html prettyprint-override"><code>{% for block in entry.articleBody %}
      {% switch block.type %}
          {% case 'text' %}
              {{ block.text }}
  
          {% case 'img' %}
              {% set img = block.img.one() %}
              
              {% if block.position == 'full' %}
                  {% set url = img.getUrl('articleFull') %}
                  {% set url2x = img.getUrl('articleFull2x') %}
              {% else %}
                  {% set url = img.getUrl('articleHalf') %}
                  {% set url2x = img.getUrl('articleHalf2x') %}
              {% endif %}
              &lt;img src=&quot;{{  url ?? img.url }}&quot;
                  srcset=&quot;{{ url }} 1x, {{ url2x }} 2x&quot;
              alt=&quot;&quot;&gt;
      {% endswitch %}
  {% endfor %}
  </code></pre>
  <h4>With what I understand should be eager loading</h4>
  <pre class="lang-html prettyprint-override"><code>{% set articleBlocks = entry.articleBody
      .with([
          ['blockType:img', {
              withTransforms: ['articleHalf', 'articleHalf2x', 'articleFull', 'articleFull2x']
          }]
      ])
      .all() %}
  {% for block in articleBlocks %}
      {% switch block.type %}
          {% case 'text' %}
              {{ block.text }}
  
          {% case 'img' %}
              {% set img = block.img[0] ?? null %}
  
              {% if block.position == 'full' %}
                  {% set url = img.getUrl('articleFull') %}
                  {% set url2x = img.getUrl('articleFull2x') %}
              {% else %}
                  {% set url = img.getUrl('articleHalf') %}
                  {% set url2x = img.getUrl('articleHalf2x') %}
              {% endif %}
              &lt;img src=&quot;{{  url ?? img.url }}&quot;
                  srcset=&quot;{{ url }} 1x, {{ url2x }} 2x&quot;
              alt=&quot;&quot;&gt;
      {% endswitch %}
  {% endfor %}
  </code></pre>
  
Tags: <matrix><image-transforms><eager-loading><optimization>
Title: Eager loading results in more queries
ThreadText: # Eager loading results in more queries
  
  (Asked by: username_0 on 2022-08-25)
  
  From what I understand it is wise to always eager load assets, I used it successfully in other cases, but here it results in a more queries, not fewer.
  
  I am trying to eager load an asset in a matrix field from the current page entry and it seems I missed something
  
  Here are the 2 versions of my code:
  
  #### Without eager loading
  
  ```html
  {% for block in entry.articleBody %}
      {% switch block.type %}
          {% case 'text' %}
              {{ block.text }}
  
          {% case 'img' %}
              {% set img = block.img.one() %}
              
              {% if block.position == 'full' %}
                  {% set url = img.getUrl('articleFull') %}
                  {% set url2x = img.getUrl('articleFull2x') %}
              {% else %}
                  {% set url = img.getUrl('articleHalf') %}
                  {% set url2x = img.getUrl('articleHalf2x') %}
              {% endif %}
              <img src="{{  url ?? img.url }}"
                  srcset="{{ url }} 1x, {{ url2x }} 2x"
              alt="">
      {% endswitch %}
  {% endfor %}
  
  ```
  
  #### With what I understand should be eager loading
  
  ```html
  {% set articleBlocks = entry.articleBody
      .with([
          ['blockType:img', {
              withTransforms: ['articleHalf', 'articleHalf2x', 'articleFull', 'articleFull2x']
          }]
      ])
      .all() %}
  {% for block in articleBlocks %}
      {% switch block.type %}
          {% case 'text' %}
              {{ block.text }}
  
          {% case 'img' %}
              {% set img = block.img[0] ?? null %}
  
              {% if block.position == 'full' %}
                  {% set url = img.getUrl('articleFull') %}
                  {% set url2x = img.getUrl('articleFull2x') %}
              {% else %}
                  {% set url = img.getUrl('articleHalf') %}
                  {% set url2x = img.getUrl('articleHalf2x') %}
              {% endif %}
              <img src="{{  url ?? img.url }}"
                  srcset="{{ url }} 1x, {{ url2x }} 2x"
              alt="">
      {% endswitch %}
  {% endfor %}
  
  ```
  
  **Tags:** matrix, image-transforms, eager-loading, optimization
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2022-08-25. Score: 1
  
  Hum,
  
  The problem was very simple. I didn't understand what the query was about and it showed.
  
  I used `blockType:img` in my query thinking `blockType` was a keyword instead of using the actual block slug witch in my case is `image`.
  
  Once fixed, the number of queries did get under the non eager loaded version
  
  Here is the relevant part of the fixed code:
  
  ```html
  {% set articleBlocks = entry.articleBody
      .with([
          ['image:img', {
              withTransforms: ['articleHalf', 'articleHalf2x', 'articleFull', 'articleFull2x']
          }]
      ])
      .all() 
  %}
  
  ```
  
  
  ---
  
  