Skip to content

Instantly share code, notes, and snippets.

@humorless
Last active September 17, 2016 15:20
SQL performance tips --- Using DATE columns
CREATE INDEX tbl_idx ON tbl (date_column);
SELECT text, date_column
FROM tbl
WHERE YEAR(date_column) = '2012';
# It should changes to the below:
# Wrapping the table column in a function renders the index useless for this query.
# Write queries for continuous periods as explicit range condition:
SELECT text, date_column
FROM tbl
WHERE date_column >= STR_TO_DATE('2012-01-01', '%Y-%m-%d')
AND date_column < STR_TO_DATE('2013-01-01', '%Y-%m-%d');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment