Example 4: Sort the data using a Covering index

Example 1 - Fetch the id and income of users whose state is CA and whose city of resid…9447 tokens truncated…act phone.

SELECT id, income FROM Users u WHERE u.address.state = "CA" AND
u.address.city= "Santaclara" AND 1000 < income and income < 10000
ORDER BY income

Query execution plan:

{
  "iterator kind" : "RECEIVE",
  "distribution kind" : "ALL_SHARDS",
  "order by fields at positions" : [ 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" : {"address.state":"CA","address.city":"Santaclara"},
          "range conditions" : { "income" : { "start value" : 1000, "start inclusive" : false, "end value" : 10000, "end inclusive" : false } }
        }
      ]
    },
    "FROM variable" : "$$u_idx",
    "SELECT expressions" : [
      {
        "field name" : "id",
        "field expression" :
        {
          "iterator kind" : "FIELD_STEP",
          "field name" : "#id",
          "input iterator" :
          {
            "iterator kind" : "VAR_REF",
            "variable" : "$$u_idx"
          }
        }
      },
      {
        "field name" : "income",
        "field expression" :
        {
          "iterator kind" : "FIELD_STEP",
          "field name" : "income",
          "input iterator" :
          {
            "iterator kind" : "VAR_REF",
            "variable" : "$$u_idx"
          }
        }
      }
    ]
  }
}

Explanation of the query execution plan :

Note: If the order of fields in the SELECT expression is different, then the value above changes. For example, if the query is SELECT income, id FROM Users u WHERE u.address.state = “CA” AND u.address.city= “Santaclara” AND 1000 < income and income < 10000 ORDER BY income, the order by fields would be order by fields at positions : [0 ] as the income field is the first field in the SELECT expression.