tags:

views:

303

answers:

4

say i have a table

Id int
Region int
Name nvarchar

select * from table1 where region = 1 and name = 'test'

select * from table1 where name = 'test' and region = 1

will there be a difference in performance? assume no indexes

is it the same with LINQ?

+3  A: 

Should it? No. SQL is a relational algebra and the DBMS should optimize irrespective of order within the statement.

Does it? Possibly. Some DBMS' may store data in a certain order (e.g., maintain a key of some sort) despite what they've been told. But, and here's the crux: you cannot rely on it.

You may need to switch DBMS' at some point in the future. Even a later version of the same DBMS may change its behavior. The only thing you should be relying on is what's in the SQL standard.

Regarding the query given: with no indexes or primary key on the two fields in question, you should assume that you'll need a full table scan for both cases. Hence they should run at the same speed.

paxdiablo
+7  A: 

Because your qualifiers are, in essence, actually the same (it doesn't matter what order the where clauses are put in), then no, there's no difference between those.

As for LINQ, you will need to know what query LINQ to SQL actually emits (you can use a SQL Profiler to find out). Sometimes the query will be the simplest query you can think of, sometimes it will be a convoluted variety of such without you realizing it, because of things like dependencies on FKs or other such constraints. LINQ also wouldn't use an * for select.

The only real way to know is to find out the SQL Server Query Execution plan of both queries. To read more on the topic, go here:

SQL Server Query Execution Plan Analysis

Jon Limjap
+1  A: 

I don't recommend the *, because the engine should look for the table scheme before executing the query. Instead use the table fields you want to avoid unnecessary overhead.

And yes, the engine optimizes your queries, but help him :) with that.

Best Regards!

MRFerocius
+1  A: 

For simple queries, likely there is little or no difference, but yes indeed the way you write a query can have a huge impact on performance.

In SQL Server (performance issues are very database specific), a correlated subquery will usually have poor performance compared to doing the same thing in a join to a derived table.

Other things in a query that can affect performance include using SARGable1 where clauses instead of non-SARGable ones, selecting only the fields you need and never using select * (especially not when doing a join as at least one field is repeated), using a set-bases query instead of a cursor, avoiding using a wildcard as the first character in a a like clause and on and on. There are very large books that devote chapters to more efficient ways to write queries.

1 "SARGable", for those that don't know, are stage 1 predicates in DB2 parlance (and possibly other DBMS'). Stage 1 predicates are more efficient since they're parts of indexes and DB2 uses those first.

HLGEM