I was under the impression it didn't matter whether numbers were quoted or not, but when I noticed that this simple query:
SELECT id FROM table t WHERE t.col = 1234
Took 0.21s
to execute (where id
is a BIGINT
primary key and col is a varchar
with an index), I knew something wasn't right.
After fiddling around a while, I tried putting quotes around the number:
SELECT id FROM table t WHERE t.col = "1234"
The execution time dropped to 0.046s
.
Is this a fluke, or does it matter if numbers are quoted?
EDIT: Also, how does this affect PDO queries where parameters are bound?
EDIT 2: Apparently the query plans are different:
Without quotes:
+----+-------------+-------+------+------------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+------------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | t | ALL | ind_col | NULL | NULL | NULL | 99431 | Using where |
+----+-------------+-------+------+------------------+------+---------+------+-------+-------------+
With quotes:
+----+-------------+-------+------+------------------+------------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+------------------+------------------+---------+-------+------+-------------+
| 1 | SIMPLE | t | ref | ind_col | ind_col | 767 | const | 1 | Using where |
+----+-------------+-------+------+------------------+------------------+---------+-------+------+-------------+