Assumptions for question:
- Database Agnostic
- Both queries will return just one row due to the values of X and Y
Question:
Which query is faster, and is one a better practice?:
SELECT * FROM my_table WHERE X = 'some value' AND Y = 'other value';
Or
SELECT * FROM my_table WHERE X = 'some value';
Basically, do you benefit or lose (performance-wise) from passing extra unneeded parameters into a query, and if “benefit” is the answer, is it a “good” practice?
Clearly the query with more parameters needs to do more work, but I think you will find in most cases that the difference will be so trivial as to make it a moot point.
The best practice is to write a query in such a way that solves your business problem and let the chips fall where they may. If you obviously know that a parameter is redundant then leave it off, if for no other reason than to simplify your code. However, I wouldn’t spend a great deal of time going to look for situations where a parameter is redundant because of the current state of the data.