Full range scan performed in sort when not required

Hi there,

I noticed that the Record Layer planner performs a full range scan where it isn’t required.
A record of type R with primary key k was queried using the query below.

  RecordQuery.newBuilder()
      .setRecordType("R")
      .setSort(Key.Expressions.field("k"),true)
      .build();

The query plan was then found to be:

  Query Plan: Scan(<,>) | [R]

However, a more optimal plan would be:

  Query Plan: [IS R]

Note that k is a primary key exclusively for record type R and no other record type, yet the planner scans all records instead of scanning only those record types that have k as their primary key.

Also, this issue is similar to this one yet I’m adding this here to highlight the fact that the issue persists even when recordType() is not concatenated in the primary key.

1 Like

Got it. This does look like it’s highly related to that other issue. I’ll add this to that one.

Interestingly, that issue suggests that the plan that should be produced for that scan is actually an error:

Note that if one includes the record type key in the sort predicate, then it will do a full scan. If one omits the record type key (which it should be able to), then it throws an error.

Not sure what the discrepancy is from.

Thanks for your response. I think this discrepancy stems from the fact that in one case the primary key has recordType() concatenated in it and in the other it doesn’t.

When the primary key has recordType() concatenated, it throws an error when recordType() is omitted in the sort expression as it isn’t able to map the sort expression to any index/primary key.