Skip to content

Commit 851c7a3

Browse files
authored
Merge pull request #376 from daKeshra/resolve-conflict
Understanding sql sargability
2 parents 7e2bc11 + 81b1327 commit 851c7a3

File tree

6 files changed

+52
-0
lines changed

6 files changed

+52
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Non-Sargable (if id is INT)
2+
EXPLAIN
3+
SELECT * FROM customers
4+
WHERE id = '1001';
5+
6+
-- Sargable
7+
EXPLAIN
8+
SELECT * FROM customers
9+
WHERE id = 1001;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Non-Sargable
2+
EXPLAIN
3+
SELECT * FROM Student
4+
WHERE YEAR(birth_date) = 2001;
5+
6+
-- Sargable
7+
EXPLAIN
8+
SELECT * FROM Student
9+
WHERE birth_date >= '2001-01-01' AND birth_date < '2002-01-01';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Non-sargable
2+
EXPLAIN
3+
SELECT * FROM Student
4+
WHERE name LIKE '%Po';
5+
6+
-- Sargable
7+
EXPLAIN
8+
SELECT * FROM Student
9+
WHERE name LIKE 'Po%';

sql-queries-10/understanding-sql-sargability/main

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- non-sargable
2+
EXPLAIN
3+
SELECT * FROM Student
4+
WHERE national_id = 123345566 OR name = 'John Liu';
5+
6+
-- Sargable
7+
EXPLAIN
8+
SELECT * FROM Student WHERE national_id = 123345566
9+
UNION
10+
SELECT * FROM Student WHERE name='John Liu';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- non-sargable JOINs
2+
EXPLAIN
3+
SELECT c.name, e.semester, e.grade
4+
FROM Course c
5+
JOIN Exam e
6+
ON LOWER(c.id) = LOWER(e.course_id)
7+
WHERE c.credits > 4;
8+
9+
10+
-- Sargable
11+
EXPLAIN
12+
SELECT c.name, e.semester, e.grade
13+
FROM Course c
14+
JOIN Exam e
15+
ON c.id = e.course_id WHERE c.credits > 4;

0 commit comments

Comments
 (0)