I was wondering if there’s a drawback (other than bad practice) to using something like this
SELECT * FROM my_table WHERE id LIKE '1';
where id is an integer. I know you’re supposed to use id=1 but I am writing a java program and if everything can use LIKE it’ll be a lot easier for me. Also, so far, everything works fine; I get the correct query results, so if there is no drawback I will continue doing it like this.
edit: I am using MySQL.
You’d need to look at the Query Execution Plan on your RDBMS to verify that
LIKEwith no wildcards is treated as efficiently as an=would be. A quick test in SQL Server shows that it would give you an index scan rather than a seek so I guess it doesn’t look at that when generating the plan and for SQL Server using=would be much more efficient. I don’t have a MySQL install to test against.Edit: Just to update this SQL Server seems to handle it fine and do a seek when the data type is varchar. When it is run against an int column though you get the scan. This is because it does an implicit conversion to varchar on the int column so can’t use the index.