Skip to content

Understanding sql sargability #376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Non-Sargable (if id is INT)
EXPLAIN
SELECT * FROM customers
WHERE id = '1001';

-- Sargable
EXPLAIN
SELECT * FROM customers
WHERE id = 1001;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Non-Sargable
EXPLAIN
SELECT * FROM Student
WHERE YEAR(birth_date) = 2001;

-- Sargable
EXPLAIN
SELECT * FROM Student
WHERE birth_date >= '2001-01-01' AND birth_date < '2002-01-01';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Non-sargable
EXPLAIN
SELECT * FROM Student
WHERE name LIKE '%Po';

-- Sargable
EXPLAIN
SELECT * FROM Student
WHERE name LIKE 'Po%';
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- non-sargable
EXPLAIN
SELECT * FROM Student
WHERE national_id = 123345566 OR name = 'John Liu';

-- Sargable
EXPLAIN
SELECT * FROM Student WHERE national_id = 123345566
UNION
SELECT * FROM Student WHERE name='John Liu';
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- non-sargable JOINs
EXPLAIN
SELECT c.name, e.semester, e.grade
FROM Course c
JOIN Exam e
ON LOWER(c.id) = LOWER(e.course_id)
WHERE c.credits > 4;


-- Sargable
EXPLAIN
SELECT c.name, e.semester, e.grade
FROM Course c
JOIN Exam e
ON c.id = e.course_id WHERE c.credits > 4;