Solved

linq to sql - performance improvement

Posted on 2010-09-06
4
592 Views
Last Modified: 2013-11-11
I have 2 tables:

 

table name: table1

columns:

id

 

table name: table2

columns:

id

c1

c2

date1

 

To query table2, I use the following code:

 

var result = (from t1 in table1

join t2 in table2 on t1.id eqauls t2.c1

where t2.id == (from t22 in table2          

                                where t22.c1 == t1.id

                                order by t22.date1 descending

                               

                                select t22.id).first()

                                                                                                               

select t2)

 

return result.ToList()

 

To improve performance, I am thinking about using the following query instead:

 

var result = (from t1 in table1

join t2 on table2 on t1.id == t2.c1

where (!t2.c2.HasValue) || (t2.id == (from t22 in table2

                                                                                where t22.c1 == t1.id

                                                                                order by t22.date1 descending

                                                                                                               

                                                                                select t22.id).first()))

                                                                                                               

select t2)

 

return result.ToList()

 

 

My thought is, if condition !t2.c2.HasValue is true, then the second component of "or" statement will not be looked at. As a result, the subquery will not be run, improving the performance.

 

1. Is this correct? Or will the subquery be run anyway.

2. in the subquery, is the query submitted when .First() is called? Or when result.ToList() is executed?

 

 
0
Comment
Question by:fippif
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 19

Accepted Solution

by:
Bardobrave earned 167 total points
ID: 33615768
Mmmm... as far as I know, linq to sql makes a direct translation from it's codification to SQL, if you pass mouse pointer over a linq to sql query on debug time you'll be the translated SQL query.

Although C derived languages are clever enough to stop evaluating conditions when it founds the first one that makes true or false a statement I don't think that SQL would be as clever (although maybe it depends on the engine executing the SQL...).

Try to see in debug time if the SQL generated by your linq statement is always the same or if it's truncated when first part of OR condition is true, this way you'll know if really the performance is going to improve.

Regarding your second question... linq to sql is made of classes and methods that execute on a straight way, so I supose that every query is submitted when it's line of code is executed. On your example you'll have your query executed and it's results stored on "result" variable (note that this variable is not able to be of any type, it has to be of a type that matches the result of your sql statement execution, this is because when this line of code runs, your variable is going to receive the result of the query execution). Afterwards, result.ToList() will only perform a type conversion, it doesn't fire any stored query.
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 167 total points
ID: 33617910
Does there exist a relationship between Table1 and Table2 in the database? If so what is it?
Can you explain in words what you want to achieve with the queries?
0
 
LVL 1

Assisted Solution

by:yamir2k
yamir2k earned 166 total points
ID: 33636688
Your queries look a bit weird. Table1 and table2 are of Table<T> type? I think you should be more specific about your tables and relations between them. Generally, you should not use join when writing a LINQ to SQL query. Instead, you should use associations between your LINQ to SQL classes.
0
 

Author Closing Comment

by:fippif
ID: 34141869
I did not really get my answer on this question. But thanks for the input.
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question