This repository was archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 502
/
Copy pathindex-select.sql
68 lines (45 loc) · 2.85 KB
/
index-select.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
CREATE TABLE foo (var1 INT, var2 INT, var3 INT);
INSERT INTO foo VALUES (0,0,0), (0,0,1), (0,0,2), (0,1,0), (0,1,1), (0,1,2), (0,2,0), (0,2,1), (0,2,2), (1,0,0), (1,0,1), (1,0,2), (1,1,0), (1,1,1), (1,1,2), (1,2,0), (1,2,1), (1,2,2), (2,0,0), (2,0,1), (2,0,2), (2,1,0), (2,1,1), (2,1,2), (2,2,0), (2,2,1), (2,2,2);
CREATE INDEX ind ON foo (var1, var2, var3);
-- Predicates + Sort exist
-- At least one predicate satisfied and sort not satisfied
-- Index should be chosen due to satisfied predicate, but limit should not be pushed down due to invalid order by
SELECT * FROM foo WHERE var1 >= 1 ORDER BY var3 LIMIT 17;
DROP INDEX ind;
CREATE INDEX ind ON foo (var1, var3);
-- At least one predicate satisfied and sort not satisfied
-- Index should be chosen due to satisfied predicate, but limit should not be pushed down due to invalid order by
SELECT * FROM foo WHERE var1 >= 1 AND var3 >= 1 ORDER BY var2 LIMIT 11;
SELECT * FROM foo WHERE var1 = 1 AND var3 >= 1 ORDER BY var2 LIMIT 5;
SELECT * FROM foo WHERE var1 >= 1 AND var3 >= 1 ORDER BY var3 LIMIT 11;
-- No predicates satisfied
-- Sequential scan chosen since no predicates or sort satisfied by index
SELECT * FROM foo WHERE var2 >= 1 ORDER BY var2 LIMIT 17;
-- At least one predicate satisfied and sort satisfied
-- Index can be chosen due to bounds, but limit cannot be pushed down because of complex predicate check
-- TODO(dpatra): Once predicate can be pushed down to index iterator, limit can also be pushed down
SELECT * FROM foo WHERE var1 >= 1 AND var3 >= 1 ORDER BY var1, var3 LIMIT 11;
SELECT * FROM foo WHERE var1 >= 1 AND var2 >= 1 ORDER BY var1, var3 LIMIT 11;
-- Index can be chosen due to bounds, and limit can be pushed down because of valid sort and enforced predicate check
SELECT * FROM foo WHERE var1 = 1 AND var3 >= 1 ORDER BY var3 LIMIT 5;
-- Sort exists
-- Sequential scan chosen since sort not satisfied by index
SELECT * FROM foo ORDER BY var2 LIMIT 26;
SELECT * FROM foo ORDER BY var3 LIMIT 26;
-- Sort satisfied
-- Index can be chosen due to bounds and limit pushed down due to satisfied order by and lack of predicates
SELECT * FROM foo ORDER BY var1, var3 LIMIT 26;
-- Predicates exist
-- Sequential scan chosen since predicate not satisfied by index
SELECT * FROM foo WHERE var2 >= 1 LIMIT 17;
-- Predicates satisfied
-- Index can be chosen due to bounds, but limit cannot be pushed down because of complex predicate check
-- TODO(dpatra): Once predicate can be pushed down to index iterator, limit can also be pushed down
SELECT * FROM foo WHERE var1 >= 1 AND var3 >= 1 LIMIT 11;
-- Index can be chosen due to bounds, and limit can be pushed down because of enforced predicate check
SELECT * FROM foo WHERE var1 = 1 AND var3 >= 1 LIMIT 5;
-- Neither predicates nor sort exist
-- Sequential scan chosen since no predicates nor order by's exist
SELECT * FROM foo LIMIT 26;
DROP INDEX ind;
DROP TABLE foo;