views:

90

answers:

6

Apologies for what may very well be a stupid question.

I'm just intrigued as to (a) will indexing improve performance (b) how will it improve performance and (c) why will it improve performance?

Also, if this does improve performance, would this be the case across the board for LINQ to SQL, LINQ to Entities, LINQ to Objects, etc, etc.

Again, if this is a really stupid question, I do apologise.

+3  A: 

It will improve the performance if, and only if, your LINQ query causes an SQL statement to be executed which can make use of the index - since, when using Linq-To-Sql, your LINQ query is translated to an SQL statement (hence the name).

For example, the following query would obviously benefit from an index on the LastName column of the Customers table.

var results = from c in db.Customers
    where c.LastName == 'Smith'
    select c;
Winston Smith
So LINQ to Entities...little no improvement on performance?
Ricardo Deano
If your backing store is a database, then yes - indexing can help performance.
Winston Smith
Excellent - thanks Winston.
Ricardo Deano
A: 

It could improve performance if you index correctly. LINQ just generates a SQL statment behind the scenes... if the statement makes use of your index, then it will improve performance... if it doesn't then it won't.

Galwegian
A: 

In order to improve performance, indexing should be used smartly. E.g. you should index the key columns you are querying by or joining by.

http://www.sql-server-performance.com/tips/optimizing_indexes_general_p1.aspx

Indexing would typically improve select request performance, but slightly increase insert and update times, since the index has to be re-calculated.

Yakimych
A: 

In layman terms, In absence of an index database has to check each record of the table and find out if it matches your Where clause but if there is an index on the column (used in where) then it can just browse through those records which have specific value or range of values.

select * from fruits where name = "Apple"

With index it can directly find the records that have name equal to apple otherwise it will iterate over all fruits and check their name.

Hasan Khan
I like layman terms being a simpleton myself! Thanks Hasan.
Ricardo Deano
A: 

Like everything SQL -- it depends.

If you have a small table it doesnt really matter (todays value for "small" is < 3000 rows).

If your query is going to return more than 30% of the rows in a table then it will probably be quicker without an index.

However if you want to select one or two particular rows from a large table then indexing some of the columns you use in the where statement (the search arguments you pass to LINQ) will speed thing up considerably.

Also if you frequntly join tables than the join predicates (the columns used in the "ON" statement for the joined to table) should be indexed. This can reduce the response time on some queries from hours to seconds.

James Anderson
A: 

LINQ to SQL generate a SQL statement, you can see this statement in different utilities like Linq pad. When you query will run in SQL Server the indexes will definitely improve performance.

  • will indexing improve performanc - Yes.
  • how will it improve performance - Read about Indexes.
  • why will it improve performance?- I really don't have answer of this question.

Also, if this does improve performance, would this be the case across the board for LINQ to SQL, LINQ to Entities, LINQ to Objects, etc, etc.

Answer of this is what is the relation of SQL table with an Object?

Muhammad Kashif Nadeem