Reputation: 626
I am working on C# WPF application that will interact with a database very often and I am concerned about the performance.
I have a question, when we add a WHERE clause to a SELECT statement, does this increase the performance because less records will be retrieved or it makes the performance worse because it will have to go through all the records anyways then additionally execute a filter?
Upvotes: 1
Views: 9316
Reputation: 1195
It's depends on your requirement. The best solution is using JOINS. But some times you have to search by specific field like name='Dilshan'
, In that case you have to use WHERE clause.
However don't retrieve every column from select statement. Always SELECT what you requires. Try to avoid using select *
statements. It will lower the performance.
Upvotes: 0
Reputation: 1297
when we add a WHERE clause to a SELECT statement, does this increase the performance because less records will be retrieved
As per Logical Processing Order that is correct, improvement could be always possible in rendering time, but not always in query processing time.
You can test that by enabling query STATISTICS, execute following command just before executing the query in SSMS, it would give you great insights along with query result (message tab):
SET STATISTICS IO, TIME ON;
P.S: There would be lot of areas to be considered when it comes to query-optimization, STATISTICS are essential among them.
Upvotes: 1
Reputation: 1271003
A where
clause will generally increase the performance of the database.
There may be some edge cases where using a where
clause might not improve performance. But in general, you should push processing to the database for tasks well-suited to the database.
Upvotes: 2