In a table ‘test’ I have an integer field TestID, with an index.
SELECT * FROM test WHERE TestID=1234
SELECT * FROM test WHERE TestID='1234'
SELECT * FROM test WHERE TestID=COALESCE(1234, 0)
SELECT * FROM test WHERE TestID=COALESCE('1234', '')
I use the second option now, because in my application the 1234 part is inserted dynamically, and could also be NULL or and empty string, and I don’t want the statement to fail because of that.
How much would the speed difference between those statements be or is there no difference at all?
If your TestID column is unindexed then varchar will be slower than integer. But if this field is indexed there will be practically no difference in speed.
P.S. if your strings are really long – index will be more bulkier so in that case using integer will be more preferably