-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathsql_optimizations_applied
87 lines (56 loc) · 1.73 KB
/
sql_optimizations_applied
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
https://blog.jooq.org/2017/09/28/10-cool-sql-optimisations-that-do-not-depend-on-the-cost-model/#top3
1. Transitive Closure - done
SELECT first_name, last_name, film_id
FROM actor a
JOIN film_actor fa ON a.actor_id = fa.actor_id
WHERE a.actor_id = 1;
-->
SELECT first_name, last_name, film_id
FROM actor a
JOIN film_actor fa ON a.actor_id = fa.actor_id
WHERE a.actor_id = 1
AND fa.actor_id = 1;
(x join y [on x.a = y.a]) where x.a = 1
->
(x where x.a = 1) join (y where y.a = 1)
or
x where a=@b and b=3
->
x where a=3 and b=3
2. Impossible Predicates - Done
s where 3 = 5
s where true -> s
s where false -> emptied s
3. Join Elimination - Done
SELECT first_name, last_name
FROM customer c
JOIN address a ON c.address_id = a.address_id
-->
SELECT first_name, last_name
FROM customer c
(x join y){x.attrs only} iff there is a foreign key constraint on the full join condition from x to y
4. Silly Predicates - done
where true -> X
where attr = attr -> X
insert s s where name = @name -> X
5. Projections in Exists Subqueries
Our exists clause is a projection against zero attributes already.
6. Predicate Merging - Done
where X and X -> where X
where X or X -> where X
7. Empty Sets
Use constraints to determine if a predicate is provably false:
constraint x > 100
where x = 10 -> where false
X join false -> x where false
x join true -> x where false
8. CHECK() constraints
not relevant - see 7
9. Unneeded self join - done
x join x -> x
(x where c1) join (x where c2) -> x where c1 and c2
(x where c1) union (x where c2) -> x where c1 or c2
10. Predicate Pushdown - done
(x where c1) where c2 -> x where c1 and c2 - done
x{proj} where c1 -> (x where c1){proj} #project on fewer tuples
(x union y) where c -> (x where c) union (y where c)