Example 6: Group the data using a Covering index

Example 1 - Fetch the state, city, and sum of income of all users grouped by the state and city

SELECT u.address.state, u.address.city, sum(income)
AS income FROM Users u GROUP BY u.address.state, u.address.city

Query execution plan:

{
   "iterator kind" : "SELECT",
   "FROM" :
   {
      "iterator kind" : "RECEIVE",
      "distribution kind" : "ALL_SHARDS",
      "order by fields at positions" : [ 0, 1 ],
      "input iterator" :
      {
         "iterator kind" : "SELECT",
         "FROM" :
         {
             "iterator kind" : "TABLE",
             "target table" : "Users",
             "row variable" : "$$u",
             "index used" : "idx_state_city_income",
             "covering index" : true,
             "index row variable" : "$$u_idx",
             "index scans" : [
                {
                    "equality conditions" : {},
                    "range conditions" : {}
                }
            ]
          },
          "FROM variable" : "$$u_idx",
          "GROUP BY" : "Grouping by the first 2 expressions in the SELECT list",
          "SELECT expressions" : [
             {
                "field name" : "state",
                "field expression" :
                {
                   "iterator kind" : "FIELD_STEP",
                   "field name" : "address.state",
                   "input iterator" :
                   {
                      "iterator kind" : "VAR_REF",
                      "variable" : "$$u_idx"
                   }
               }
             },
             {
                "field name" : "city",
                "field expression" :
                {
                   "iterator kind" : "FIELD_STEP",
                   "field name" : "address.city",
                   "input iterator" :
                   {
                      "iterator kind" : "VAR_REF",
                      "variable" : "$$u_idx"
                   }
                }
             },
             {
                "field name" : "income",
                "field expression" :
                {
                   "iterator kind" : "FUNC_SUM",
                   "input iterator" :
                   {
                      "iterator kind" : "FIELD_STEP",
                      "field name" : "income",
                      "input iterator" :
                      {
                         "iterator kind" : "VAR_REF",
                         "variable" : "$$u_idx"
                      }
                   }
                }
            }
       ]
    }
 },
 "FROM variable" : "$from-1",
 "GROUP BY" : "Grouping by the first 2 expressions in the SELECT list",
 "SELECT expressions" : [
    {
       "field name" : "state",
       "field expression" :
       {
          "iterator kind" : "FIELD_STEP",
          "field name" : "state",
          "input iterator" :
          {
             "iterator kind" : "VAR_REF",
             "variable" : "$from-1"
          }
       }
    },
    {
       "field name" : "city",
       "field expression" :
       {
          "iterator kind" : "FIELD_STEP",
          "field name" : "city",
          "input iterator" :
          {
             "iterator kind" : "VAR_REF",
             "variable" : "$from-1"
          }
       }
    },
    {
       "field name" : "income",
       "field expression" :
       {
          "iterator kind" : "FUNC_SUM",
          "input iterator" :
          {
             "iterator kind" : "FIELD_STEP",
             "field name" : "income",
             "input iterator" :
             {
                "iterator kind" : "VAR_REF",
                "variable" : "$from-1"
             }
           }
        }
     }
  ]
}

Explanation of the query execution plan :