3

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 |
+----+-------------+-------+------+------------------+------------------+---------+-------+------+-------------+
Nate
  • 26,164
  • 34
  • 130
  • 214

1 Answers1

2

In terms of performance they do not matter for mysql. For when to use them you can see this question When to use single quotes, double quotes, and backticks in MySQL

You are most likely experiencing a warmed up buffer pool or query caching. Also, when I locally query a remote a sql machine in amazon I can have variability in response time just from my own network performance and remote machine load. If you want to really test it out try profiling the query.

http://dev.mysql.com/doc/refman/5.5/en/show-profile.html

For this specific case it is an eccentricity of mysql that you cannot use a string type index when querying for a numeric value. The reverse however works.

Community
  • 1
  • 1
Daniel Williams
  • 8,673
  • 4
  • 36
  • 47