SQL Query Anti-Patterns & Best Practices
SQL Query Anti-Patterns & Best Practices
Even with optimal indexes in place, poorly constructed SQL syntax can prevent the query optimizer from leveraging them. In database engineering, these bad syntax habits are known as SQL Anti-Patterns.
Mastering SARGable (Search Argument Able) query design guarantees maximum index utilization.
Anti-Pattern 1: Wrapping Indexed Columns in Functions (Non-SARGable)
When you wrap an indexed column in a function, MySQL cannot use the index because the index contains raw column values, not function results!
Anti-Pattern 2: Implicit Type Conversion
If you compare a numeric column to a string, or a string column to an integer, MySQL converts the column value for every row, disabling the index:
Anti-Pattern 3: Wildcard Prefixes (LIKE '%term')
Anti-Pattern 4: SELECT * in Production Applications
Using SELECT *:
- 1Destroys Covering Index optimizations by demanding all unindexed columns.
- 2Increases network bandwidth consumption between DB and backend API servers.
- 3Wastes application memory parsing unused columns (like large
BLOBorTEXTfields).
Anti-Pattern 5: The OR Operator Across Unrelated Columns
A single query with WHERE col_a = 1 OR col_b = 2 frequently prevents index usage, resulting in an expensive index_merge or full table scan.
Summary Checklist for Production Queries
- Are date and time queries structured with range comparisons instead of
YEAR(),MONTH(), orDATE()? - Are string literals properly quoted to prevent implicit casting?
- Are only necessary columns projected instead of
SELECT *? - Are multiple conditions joined with
ANDrather than disjointORwhere possible?
Multiple Choice Questions
1. What does the term "SARGable" stand for in SQL query optimization?
A. Storage Array Redundant Gateway B. Search Argument Able (queries structured so indexes can be utilized) C. Serialized Asynchronous Read Group D. System Administrator Resource Governance Answer: B Explanation: SARGable describes predicates formatted so the query engine can directly utilize index seeks rather than scanning tables.
2. Why does WHERE DATE(created_at) = '2026-08-01' cause a full table scan on an indexed created_at column?
A. The DATE function is deprecated B. Applying a function to an indexed column prevents the B+Tree from searching raw stored timestamps C. Dates cannot be indexed D. The format is invalid Answer: B Explanation: Wrapping an indexed column in a function requires MySQL to compute the function on every row, disabling index range lookup.
3. If phone_number is VARCHAR, why does WHERE phone_number = 12345 perform poorly?
A. MySQL throws a fatal syntax error B. MySQL implicitly converts the indexed column to a number for every row, disabling index seeking C. Numbers cannot be stored in VARCHAR D. The buffer pool flushes Answer: B Explanation: Type mismatch between a VARCHAR column and an integer literal triggers implicit casting on the column, preventing index lookup.
4. Why is SELECT * considered an anti-pattern in production backend code?
A. It locks tables from writing B. It breaks covering index optimizations and wastes network/memory bandwidth fetching unneeded columns C. MySQL automatically converts it to a temporary table D. Primary keys cannot be selected with Answer: B Explanation: SELECT fetches all columns, preventing covering index optimizations and wasting I/O and network transfer bandwidth.
5. How can a slow WHERE col_a = 10 OR col_b = 20 query often be refactored to utilize two distinct single-column indexes?
A. By replacing OR with AND B. By splitting into two indexed queries connected via UNION C. By adding a GROUP BY D. By converting to a stored procedure Answer: B Explanation: Splitting an OR into two separate queries combined with UNION allows each branch to execute an efficient single-index seek.
Defining Stored Procedures (DELIMITER //, CREATE PROCEDURE)
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Identifying & Fixing Slow Queries & Filesort | Defining Stored Procedures (DELIMITER //, CREATE PROCEDURE) |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.