Skip to content

Commit

Permalink
Issue #25: Make ORDER BY DESC faster
Browse files Browse the repository at this point in the history
Added support for reverse-ordered Column Families. If a Column Family
name starts with "rev:", then the data in it is stored in the reverse
order.
  • Loading branch information
spetrunia authored and jtolmer committed Jan 5, 2016
1 parent 46fb1ea commit beba89d
Show file tree
Hide file tree
Showing 13 changed files with 581 additions and 142 deletions.
120 changes: 120 additions & 0 deletions mysql-test/r/rocksdb_cf_reverse.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t1 (
pk int primary key,
a int not null,
b int not null,
key(a) comment 'rev:foo',
key(b) comment 'bar'
) engine=rocksdb;
insert into t1 select a,a,a from t0;
insert into t1 select a+10,a+10,a+10 from t0;
# Primary key is not in a reverse-ordered CF, so full table scan
# returns rows in ascending order:
select * from t1;
pk a b
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
10 10 10
11 11 11
12 12 12
13 13 13
14 14 14
15 15 15
16 16 16
17 17 17
18 18 18
19 19 19
explain
select a from t1 order by a limit 5;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL a 4 NULL 5 Using index
select a from t1 order by a limit 5;
a
0
1
2
3
4
explain
select b from t1 order by b limit 5;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL b 4 NULL 5 Using index
select a from t1 order by a limit 5;
a
0
1
2
3
4
explain
select a from t1 order by a desc limit 5;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL a 4 NULL 5 Using index
select a from t1 order by a desc limit 5;
a
19
18
17
16
15
explain
select b from t1 order by b desc limit 5;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL b 4 NULL 5 Using index
select b from t1 order by b desc limit 5;
b
19
18
17
16
15
drop table t1;
#
# Try a primary key in a reverse-ordered CF.
#
create table t2 (
pk int,
a int not null,
primary key(pk) comment 'rev:cf1'
) engine=rocksdb;
insert into t2 select a,a from t0;
# Primary key is in a reverse-ordered CF, so full table scan
# returns rows in descending order:
select * from t2;
pk a
9 9
8 8
7 7
6 6
5 5
4 4
3 3
2 2
1 1
0 0
set autocommit=0;
begin;
delete from t2 where a=3 or a=7;
select * from t2;
pk a
9 9
8 8
6 6
5 5
4 4
2 2
1 1
0 0
rollback;
set autocommit=1;
drop table t2;
drop table t0;
65 changes: 65 additions & 0 deletions mysql-test/t/rocksdb_cf_reverse.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#
# RocksDB-SE tests for reverse-ordered Column Families
#

create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

create table t1 (
pk int primary key,
a int not null,
b int not null,
key(a) comment 'rev:foo',
key(b) comment 'bar'
) engine=rocksdb;

insert into t1 select a,a,a from t0;
insert into t1 select a+10,a+10,a+10 from t0;

--echo # Primary key is not in a reverse-ordered CF, so full table scan
--echo # returns rows in ascending order:
select * from t1;

explain
select a from t1 order by a limit 5;
select a from t1 order by a limit 5;

explain
select b from t1 order by b limit 5;
select a from t1 order by a limit 5;

explain
select a from t1 order by a desc limit 5;
select a from t1 order by a desc limit 5;

explain
select b from t1 order by b desc limit 5;
select b from t1 order by b desc limit 5;

drop table t1;

--echo #
--echo # Try a primary key in a reverse-ordered CF.
--echo #

create table t2 (
pk int,
a int not null,
primary key(pk) comment 'rev:cf1'
) engine=rocksdb;

insert into t2 select a,a from t0;
--echo # Primary key is in a reverse-ordered CF, so full table scan
--echo # returns rows in descending order:
select * from t2;

set autocommit=0;
begin;
delete from t2 where a=3 or a=7;
select * from t2;
rollback;
set autocommit=1;

drop table t2;
drop table t0;

0 comments on commit beba89d

Please sign in to comment.