Example 5: Sort the data using a field not part of the index

Example 1 - Fetch the id, income, and age of users belonging to the state CA and whose city of residence is Santaclara and have income between 1000 and 10000. Sort the results by age

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

Query execution plan:

{
  "iterator kind" : "SORT",
  "order by fields at positions" : [ 2 ],
  "input iterator" :
  {
    "iterator kind" : "RECEIVE",
    "distribution kind" : "ALL_SHARDS",
    "input iterator" :
    {
      "iterator kind" : "SELECT",
      "FROM" :
      {
        "iterator kind" : "TABLE",
        "target table" : "users",
        "row variable" : "$$u",
        "index used" : "idx_state_city_income",
        "covering index" : false,
        "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",
      "SELECT expressions" : [
        {
          "field name" : "id",
          "field expression" :
          {
            "iterator kind" : "FIELD_STEP",
            "field name" : "id",
            "input iterator" :
           {
              "iterator kind" : "VAR_REF",
              "variable" : "$$u"
            }
          }
        },
        {
          "field name" : "income",
          "field expression" :
          {
            "iterator kind" : "FIELD_STEP",
            "field name" : "income",
            "input iterator" :
            {
              "iterator kind" : "VAR_REF",
              "variable" : "$$u"
            }
          }
        },
        {
          "field name" : "age",
          "field expression" :
          {
            "iterator kind" : "FIELD_STEP",
            "field name" : "age",
            "input iterator" :
            {
              "iterator kind" : "VAR_REF",
              "variable" : "$$u"
            }
          }
        }
      ]
    }
  }
}

Explanation of the query execution plan :