yugabyte / yugabyte-db

YugabyteDB - the cloud native distributed SQL database for mission-critical applications.
https://www.yugabyte.com
Other
8.95k stars 1.07k forks source link

[YCQL] Explain plan for index scans omits limit clause #4649

Open m-iancu opened 4 years ago

m-iancu commented 4 years ago

Jira Link: DB-4683

Repro

Set up a table.
create keyspace foo;
use foo;
create table test(h int, r int, v int, primary key(h,r)) with transactions = {'enabled' : true};
create index on test(v);
Insert some rows.
insert into test (h,r,v) values (1,1,1);
insert into test (h,r,v) values (2,2,1);
insert into test (h,r,v) values (2,3,1);
insert into test (h,r,v) values (2,4,2);
insert into test (h,r,v) values (2,5,1);
select * from test where v = 1;
Test explain plan for sequential scan.
explain select * from test where h = 1 limit 1;
 QUERY PLAN
---------------------------------
 Limit
   ->  Range Scan on foo.test
         Key Conditions: (h = 1)
Test explain plan for index scan.
explain select * from test where v = 1 limit 1;
 QUERY PLAN
--------------------------------------------------
 Index Only Scan using foo.test_v_idx on foo.test
   Key Conditions: (v = 1)

^ Limit component missing.

Actual select works as expected.

This is an EXPLAIN issue only.

select * from test where v = 1 limit 1;
 h | r | v
---+---+---
 1 | 1 | 1

(1 rows)

Possible cause.

The function AnalysisResultToPB in pt_select.cc should inspect the child_select instead of (or in addition to) the top level select when checking for a limit_clause (and possibly aggregate).

m-iancu commented 4 years ago

Also confirmed that the limit is actually pushed down to docdb not filtered in CQL layer. Here is the request proto

request_id: 8448924902569571506
schema_version: 0
hash_code: 4624
hashed_column_values {
  value {
    int32_value: 1
  }
}
selected_exprs {
  column_id: 1
}
selected_exprs {
  column_id: 2
}
selected_exprs {
  column_id: 0
}
limit: 1
return_paging_state: false
distinct: false
remote_endpoint {
  host: "0.0.0.0"
  port: 0
}
max_hash_code: 4624
column_refs {
  ids: 0
  ids: 1
  ids: 2
}
query_id: 4453507968
rsrow_desc {
  rscol_descs {
    name: "h"
    ql_type {
      main: INT32
    }
  }
  rscol_descs {
    name: "r"
    ql_type {
      main: INT32
    }
  }
  rscol_descs {
    name: "v"
    ql_type {
      main: INT32
    }
  }
}
is_forward_scan: true
is_aggregate: false

Note limit 1 row above. Also checked that the iterator stops after 1 row as expected.