SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Pls help Understanding Entity Framework generated Query vs Custom Query


Pls help Understanding Entity Framework generated Query vs Custom Query

Author
Message
mnachu
mnachu
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 6
SQL Server : 2005.
Database: AdentureWorks.
.NET version 3.6 with SP1, EF is included in .NET. Using VS 2008 with SP1.

I am trying some examples in Microsoft Entity Framework. I noticed most of the queries generated by EF looks different for even simple selects. For example the following EF query is selecting rows from Employee with Order by and one extra column:

SET STATISTICS IO ON
SET STATISTICS TIME ON

SELECT
[Project1].[C1] AS [C1],
[Project1].[EmployeeID] AS [EmployeeID],
[Project1].[NationalIDNumber] AS [NationalIDNumber],
[Project1].[LoginID] AS [LoginID],
[Project1].[Title] AS [Title],
[Project1].[BirthDate] AS [BirthDate],
[Project1].[MaritalStatus] AS [MaritalStatus],
[Project1].[Gender] AS [Gender],
[Project1].[HireDate] AS [HireDate],
[Project1].[SalariedFlag] AS [SalariedFlag],
[Project1].[VacationHours] AS [VacationHours],
[Project1].[SickLeaveHours] AS [SickLeaveHours],
[Project1].[CurrentFlag] AS [CurrentFlag],
[Project1].[rowguid] AS [rowguid],
[Project1].[ModifiedDate] AS [ModifiedDate],
[Project1].[ContactID] AS [ContactID],
[Project1].[ManagerID] AS [ManagerID]
FROM ( SELECT
[Extent1].[EmployeeID] AS [EmployeeID],
[Extent1].[NationalIDNumber] AS [NationalIDNumber],
[Extent1].[ContactID] AS [ContactID],
[Extent1].[LoginID] AS [LoginID],
[Extent1].[ManagerID] AS [ManagerID],
[Extent1].[Title] AS [Title],
[Extent1].[BirthDate] AS [BirthDate],
[Extent1].[MaritalStatus] AS [MaritalStatus],
[Extent1].[Gender] AS [Gender],
[Extent1].[HireDate] AS [HireDate],
[Extent1].[SalariedFlag] AS [SalariedFlag],
[Extent1].[VacationHours] AS [VacationHours],
[Extent1].[SickLeaveHours] AS [SickLeaveHours],
[Extent1].[CurrentFlag] AS [CurrentFlag],
[Extent1].[rowguid] AS [rowguid],
[Extent1].[ModifiedDate] AS [ModifiedDate],
1 AS [C1]
FROM [HumanResources].[Employee] AS [Extent1]
) AS [Project1]
ORDER BY [Project1].[HireDate] ASC


As you would have noticed EF is adding a subquery. If I had written this query I would have done something like this:

SET STATISTICS IO ON
SET STATISTICS TIME ON

SELECT
1 AS [C1],
[Project1].[EmployeeID] AS [EmployeeID],
[Project1].[NationalIDNumber] AS [NationalIDNumber],
[Project1].[LoginID] AS [LoginID],
[Project1].[Title] AS [Title],
[Project1].[BirthDate] AS [BirthDate],
[Project1].[MaritalStatus] AS [MaritalStatus],
[Project1].[Gender] AS [Gender],
[Project1].[HireDate] AS [HireDate],
[Project1].[SalariedFlag] AS [SalariedFlag],
[Project1].[VacationHours] AS [VacationHours],
[Project1].[SickLeaveHours] AS [SickLeaveHours],
[Project1].[CurrentFlag] AS [CurrentFlag],
[Project1].[rowguid] AS [rowguid],
[Project1].[ModifiedDate] AS [ModifiedDate],
[Project1].[ContactID] AS [ContactID],
[Project1].[ManagerID] AS [ManagerID]
FROM [HumanResources].[Employee] AS [Project1]
ORDER BY [Project1].[HireDate] ASC

Both the queries give same result. I could slightly optimize my query. But I am not going to touch that part for now.

My question is when I look at the IO details both queries does the same and they both run pretty much at same speed.

Question is 1) Even though I see syntactical difference why don't I see any performance different? (or I am missing something here).
2) Is there a clear answer to which one is better here?

If there are other articles please ignore this one and direct me there.

Thanks,
Nachi
noeld
noeld
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23084 Visits: 2048
mnachu (1/5/2009)
SQL Server : 2005.
Database: AdentureWorks.
.NET version 3.6 with SP1, EF is included in .NET. Using VS 2008 with SP1.

I am trying some examples in Microsoft Entity Framework. I noticed most of the queries generated by EF looks different for even simple selects. For example the following EF query is selecting rows from Employee with Order by and one extra column:

SET STATISTICS IO ON
SET STATISTICS TIME ON

SELECT
[Project1].[C1] AS [C1],
[Project1].[EmployeeID] AS [EmployeeID],
[Project1].[NationalIDNumber] AS [NationalIDNumber],
[Project1].[LoginID] AS [LoginID],
[Project1].[Title] AS [Title],
[Project1].[BirthDate] AS [BirthDate],
[Project1].[MaritalStatus] AS [MaritalStatus],
[Project1].[Gender] AS [Gender],
[Project1].[HireDate] AS [HireDate],
[Project1].[SalariedFlag] AS [SalariedFlag],
[Project1].[VacationHours] AS [VacationHours],
[Project1].[SickLeaveHours] AS [SickLeaveHours],
[Project1].[CurrentFlag] AS [CurrentFlag],
[Project1].[rowguid] AS [rowguid],
[Project1].[ModifiedDate] AS [ModifiedDate],
[Project1].[ContactID] AS [ContactID],
[Project1].[ManagerID] AS [ManagerID]
FROM ( SELECT
[Extent1].[EmployeeID] AS [EmployeeID],
[Extent1].[NationalIDNumber] AS [NationalIDNumber],
[Extent1].[ContactID] AS [ContactID],
[Extent1].[LoginID] AS [LoginID],
[Extent1].[ManagerID] AS [ManagerID],
[Extent1].[Title] AS [Title],
[Extent1].[BirthDate] AS [BirthDate],
[Extent1].[MaritalStatus] AS [MaritalStatus],
[Extent1].[Gender] AS [Gender],
[Extent1].[HireDate] AS [HireDate],
[Extent1].[SalariedFlag] AS [SalariedFlag],
[Extent1].[VacationHours] AS [VacationHours],
[Extent1].[SickLeaveHours] AS [SickLeaveHours],
[Extent1].[CurrentFlag] AS [CurrentFlag],
[Extent1].[rowguid] AS [rowguid],
[Extent1].[ModifiedDate] AS [ModifiedDate],
1 AS [C1]
FROM [HumanResources].[Employee] AS [Extent1]
) AS [Project1]
ORDER BY [Project1].[HireDate] ASC


As you would have noticed EF is adding a subquery. If I had written this query I would have done something like this:

SET STATISTICS IO ON
SET STATISTICS TIME ON

SELECT
1 AS [C1],
[Project1].[EmployeeID] AS [EmployeeID],
[Project1].[NationalIDNumber] AS [NationalIDNumber],
[Project1].[LoginID] AS [LoginID],
[Project1].[Title] AS [Title],
[Project1].[BirthDate] AS [BirthDate],
[Project1].[MaritalStatus] AS [MaritalStatus],
[Project1].[Gender] AS [Gender],
[Project1].[HireDate] AS [HireDate],
[Project1].[SalariedFlag] AS [SalariedFlag],
[Project1].[VacationHours] AS [VacationHours],
[Project1].[SickLeaveHours] AS [SickLeaveHours],
[Project1].[CurrentFlag] AS [CurrentFlag],
[Project1].[rowguid] AS [rowguid],
[Project1].[ModifiedDate] AS [ModifiedDate],
[Project1].[ContactID] AS [ContactID],
[Project1].[ManagerID] AS [ManagerID]
FROM [HumanResources].[Employee] AS [Project1]
ORDER BY [Project1].[HireDate] ASC

Both the queries give same result. I could slightly optimize my query. But I am not going to touch that part for now.

My question is when I look at the IO details both queries does the same and they both run pretty much at same speed.

Question is 1) Even though I see syntactical difference why don't I see any performance different? (or I am missing something here).
2) Is there a clear answer to which one is better here?

If there are other articles please ignore this one and direct me there.

Thanks,
Nachi


SQL Optimizer can remove the unnecessary subquery for the execution plan which I bet is identical!


* Noel
Grant Fritchey
Grant Fritchey
SSC Guru
SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)

Group: General Forum Members
Points: 99653 Visits: 33014
I'll bet the execution plan for both is the same.

It's a messy way to write procedures, at least for really simple things like this, but it's not going to add a lot of overhead to the query. Not for this simple one anyway.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
mnachu
mnachu
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 6
Thanks Noel and Grant,

I looked at the Execution plan and it was identical.

I still wonder why would Microsoft's EF engine create queries like this and is there any memory related performance issues for the subquery or any benefit with these approaches.

I had seen really complicated queries generated by EF for simple SUM/Average functions. It is just not clear why they do that. But still they is very negligible performance difference.

Thanks,
Nachi
Grant Fritchey
Grant Fritchey
SSC Guru
SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)

Group: General Forum Members
Points: 99653 Visits: 33014
I'm not sure, I haven't worked with Entity Framework much. What does it do when it has to do a JOIN between two tables?

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
mnachu
mnachu
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 6
Grant,

EF does JOINs exactly like we do in T-SQL.

Example: There are two tables in AdventureWorks database, Employee and Contact. Each Employee can have more than one contact but Contact's cannot have more than one employee!!.

In order to Load it you can request EF to do it in two different approach.

Approach One: "Query Path", You request EF to load all the related data at the time you load your parent. When you do this EF will issue a LEFT JOIN and load all the related tables: See Following query:

SELECT
1 AS [C1],
[Extent1].[EmployeeID] AS [EmployeeID],
[Extent1].[DepartmentID] AS [DepartmentID],
[Extent1].[ShiftID] AS [ShiftID],
[Extent1].[StartDate] AS [StartDate],
[Extent1].[EndDate] AS [EndDate],
[Extent1].[ModifiedDate] AS [ModifiedDate],
1 AS [C2],
[Extent2].[EmployeeID] AS [EmployeeID1],
[Extent2].[NationalIDNumber] AS [NationalIDNumber],
[Extent2].[LoginID] AS [LoginID],
[Extent2].[Title] AS [Title],
[Extent2].[BirthDate] AS [BirthDate],
[Extent2].[MaritalStatus] AS [MaritalStatus],
[Extent2].[Gender] AS [Gender],
[Extent2].[HireDate] AS [HireDate],
[Extent2].[SalariedFlag] AS [SalariedFlag],
[Extent2].[VacationHours] AS [VacationHours],
[Extent2].[SickLeaveHours] AS [SickLeaveHours],
[Extent2].[CurrentFlag] AS [CurrentFlag],
[Extent2].[rowguid] AS [rowguid],
[Extent2].[ModifiedDate] AS [ModifiedDate1],
[Extent3].[ContactID] AS [ContactID],
[Extent3].[NameStyle] AS [NameStyle],
[Extent3].[Title] AS [Title1],
[Extent3].[FirstName] AS [FirstName],
[Extent3].[MiddleName] AS [MiddleName],
[Extent3].[LastName] AS [LastName],
[Extent3].[Suffix] AS [Suffix],
[Extent3].[EmailAddress] AS [EmailAddress],
[Extent3].[EmailPromotion] AS [EmailPromotion],
[Extent3].[Phone] AS [Phone],
[Extent3].[PasswordHash] AS [PasswordHash],
[Extent3].[PasswordSalt] AS [PasswordSalt],
[Extent3].[AdditionalContactInfo] AS [AdditionalContactInfo],
[Extent3].[rowguid] AS [rowguid1],
[Extent3].[ModifiedDate] AS [ModifiedDate2],
[Extent2].[ManagerID] AS [ManagerID]
FROM [HumanResources].[EmployeeDepartmentHistory] AS [Extent1]
LEFT OUTER JOIN [HumanResources].[Employee] AS [Extent2]
ON [Extent1].[EmployeeID] = [Extent2].[EmployeeID]
LEFT OUTER JOIN [Person].[Contact] AS [Extent3]
ON [Extent2].[ContactID] = [Extent3].[ContactID]

Approach 2: "Explicit Load"

You load the Parent first and whenever you want you load the child. In this case EF will issue one D-SQL to DB and get the Parent rows. After that when you want the related child rows you load the child for one parent at a time, which mean you making multiple trips to DB but same D-SQL with different Parent ID. The queries look like this: This works only in SQL 2005.

-- SQL Generated from EF
SELECT
1 AS [C1],
[Extent1].[EmployeeID] AS [EmployeeID],
[Extent1].[NationalIDNumber] AS [NationalIDNumber],
[Extent1].[LoginID] AS [LoginID],
[Extent1].[Title] AS [Title],
[Extent1].[BirthDate] AS [BirthDate],
[Extent1].[MaritalStatus] AS [MaritalStatus],
[Extent1].[Gender] AS [Gender],
[Extent1].[HireDate] AS [HireDate],
[Extent1].[SalariedFlag] AS [SalariedFlag],
[Extent1].[VacationHours] AS [VacationHours],
[Extent1].[SickLeaveHours] AS [SickLeaveHours],
[Extent1].[CurrentFlag] AS [CurrentFlag],
[Extent1].[rowguid] AS [rowguid],
[Extent1].[ModifiedDate] AS [ModifiedDate],
[Extent1].[ContactID] AS [ContactID],
[Extent1].[ManagerID] AS [ManagerID]
FROM [HumanResources].[Employee] AS [Extent1]

-- SQL Generated from EF
exec sp_executesql N'SELECT
1 AS [C1],
[Extent1].[EmployeeID] AS [EmployeeID],
[Extent1].[DepartmentID] AS [DepartmentID],
[Extent1].[ShiftID] AS [ShiftID],
[Extent1].[StartDate] AS [StartDate],
[Extent1].[EndDate] AS [EndDate],
[Extent1].[ModifiedDate] AS [ModifiedDate]
FROM [HumanResources].[EmployeeDepartmentHistory] AS [Extent1]
WHERE [Extent1].[EmployeeID] = @EntityKeyValue1',
N'@EntityKeyValue1 int',@EntityKeyValue1=1

exec sp_executesql N'SELECT
1 AS [C1],
[Extent1].[EmployeeID] AS [EmployeeID],
[Extent1].[DepartmentID] AS [DepartmentID],
[Extent1].[ShiftID] AS [ShiftID],
[Extent1].[StartDate] AS [StartDate],
[Extent1].[EndDate] AS [EndDate],
[Extent1].[ModifiedDate] AS [ModifiedDate]
FROM [HumanResources].[EmployeeDepartmentHistory] AS [Extent1]
WHERE [Extent1].[EmployeeID] = @EntityKeyValue1',
N'@EntityKeyValue1 int',@EntityKeyValue1=285

HTH,
Nachi
Grant Fritchey
Grant Fritchey
SSC Guru
SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)

Group: General Forum Members
Points: 99653 Visits: 33014
I expected it to perform the join inside a sub-select and then select from the result set as a single set of values. That would have helped explain why it did the first query you posted the way it did.

Ah well, I'm stuck. Sorry.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
noeld
noeld
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23084 Visits: 2048
... It is just not clear why they do that. ...


The answer is that they are trading "flexibility" on the language (high speed development) for possible performance implications in the DB.


* Noel
Grant Fritchey
Grant Fritchey
SSC Guru
SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)

Group: General Forum Members
Points: 99653 Visits: 33014
If you think these queries are bad, you should see the stuff coming out of nHibernate. I've only seen a few so far, but they're pretty horrifying. It does implicit data conversions on almost everything. Indexes may be a thing of the past.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search