Skip to content

Commit 733fcc5

Browse files
committed
understanding sql sargability
1 parent 784e0a9 commit 733fcc5

File tree

5 files changed

+52
-0
lines changed

5 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%';
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)