Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Pls help Understanding Entity Framework generated Query vs Custom Query Expand / Collapse
Author
Message
Posted Monday, January 5, 2009 10:31 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, January 5, 2009 3:28 PM
Points: 3, 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
Post #629977
Posted Monday, January 5, 2009 10:35 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Tuesday, May 6, 2014 5:51 AM
Points: 6,266, Visits: 2,028
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
Post #629980
Posted Monday, January 5, 2009 11:00 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 4:56 AM
Points: 15,646, Visits: 28,028
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 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #629996
Posted Monday, January 5, 2009 11:27 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, January 5, 2009 3:28 PM
Points: 3, 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
Post #630019
Posted Monday, January 5, 2009 11:33 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 4:56 AM
Points: 15,646, Visits: 28,028
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 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #630023
Posted Monday, January 5, 2009 11:46 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, January 5, 2009 3:28 PM
Points: 3, 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


Post #630031
Posted Monday, January 5, 2009 11:55 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 4:56 AM
Points: 15,646, Visits: 28,028
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 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #630036
Posted Monday, January 5, 2009 3:26 PM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Tuesday, May 6, 2014 5:51 AM
Points: 6,266, Visits: 2,028
... 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
Post #630176
Posted Monday, January 5, 2009 6:25 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 4:56 AM
Points: 15,646, Visits: 28,028
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 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #630246
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse