Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to SQL: select optimization

On large tables in MSSQL; selecting specific columns results in greater speed of the query. Does the same apply to Linq to SQL?

Would this:

var person = from p in [DataContextObject].Persons
             where p.PersonsID == 1
             select new { p.PersonsID, p.PersonsAdress, p.PersonsZipcode };

be faster than this:

var person = from p in [DataContextObject].Persons
             where p.PersonsID == 1
             select p;

... ?

like image 448
cllpse Avatar asked Oct 14 '08 15:10

cllpse


People also ask

Is LINQ good for performance?

LINQ syntax is typically less efficient than a foreach loop. It's good to be aware of any performance tradeoff that might occur when you use LINQ to improve the readability of your code. And if you'd like to measure the performance difference, you can use a tool like BenchmarkDotNet to do so.

Should I use LINQ in unity?

LINQ (Language Integrated Query) is a great feature available to C# Unity developers. Many developers don't know it exists or how to use it though, and lose out on the great time & code savings it can provide. LINQ in Unity has a variety of great uses, and a couple pitfalls you'll want to avoid.

How do I select a query in LINQ?

LINQ query syntax always ends with a Select or Group clause. The Select clause is used to shape the data. You can select the whole object as it is or only some properties of it. In the above example, we selected the each resulted string elements.


1 Answers

I highly recommend LinqPad. It is free and lets you run LINQ queries dynamically. When you can also look at the SQL that is generated.

What you will see is that the LINQ query will translate the first query into selecting only those columns. So it is faster.

like image 69
Peter Avatar answered Sep 17 '22 22:09

Peter