# Full range scan performed in sort when not required

**URL:** <https://forums.foundationdb.org/t/full-range-scan-performed-in-sort-when-not-required/1995>\
**Category:** Record Layer\
**Created:** [March 2, 2020, 10:49am UTC](https://forums.foundationdb.org/t/full-range-scan-performed-in-sort-when-not-required/1995 "2020-03-02T10:49:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aymalik](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@aymalik](https://forums.foundationdb.org/u/aymalik)\
**Post date:** [March 2, 2020, 10:49am UTC](https://forums.foundationdb.org/t/full-range-scan-performed-in-sort-when-not-required/1995/1 "2020-03-02T10:49:07Z")

</div>

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](https://github.com/FoundationDB/fdb-record-layer/issues/744) yet I’m adding this here to highlight the fact that the issue persists even when recordType() is not concatenated in the primary key.

---

<div class="post-metadata">

**Author:** ![alloc](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/alloc/32/9_2.png) [@alloc](https://forums.foundationdb.org/u/alloc)\
**Post date:** [March 2, 2020, 5:15pm UTC](https://forums.foundationdb.org/t/full-range-scan-performed-in-sort-when-not-required/1995/2 "2020-03-02T17:15:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alloc](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/alloc/32/9_2.png) [@alloc](https://forums.foundationdb.org/u/alloc)\
**Post date:** [March 2, 2020, 5:17pm UTC](https://forums.foundationdb.org/t/full-range-scan-performed-in-sort-when-not-required/1995/3 "2020-03-02T17:17:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aymalik](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@aymalik](https://forums.foundationdb.org/u/aymalik)\
**Post date:** [March 3, 2020, 10:16am UTC](https://forums.foundationdb.org/t/full-range-scan-performed-in-sort-when-not-required/1995/4 "2020-03-03T10:16:53Z")

</div>

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.
