Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the way you write sql queries affect performance?

Tags:

sql

linq

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?

like image 349
chat Avatar asked Dec 17 '22 08:12

chat


1 Answers

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

like image 165
Jon Limjap Avatar answered Jan 22 '23 21:01

Jon Limjap