I live in Australia, so postcodes are numeric and four digits long.
In a table steup by another person the postcode field has been setup as a VARCHAR(10) – strange i know!!!
There is a difference between the following two query times:
Postcode=’3000′
Postcode=3000
Both queries run, but the one with single quotes around it runs between 50% to 80% faster. Likewise postcode IN(‘3000′,’3001′,’3002’) is much faster than Postcode IN(3000,3001,3002). The postcode field is indexed
The quesiton is HOW do the single quotes make so much speed difference?
Can anyone shed any light on how the engine optimizes the above queries?
There is one important trap in this.
If you use something like
instead of
then if you had other dataset, the first case would return all records like:
etc, while the second would return as expected only ‘1000’. This might be the reason of performance issue. Some mentioned that it converts int to varchar. I believe it converts all varchars to int and that is why it is noticable