ling to sql and entity framework performance analysis

20
Linq to SQL and Entity Framework performance comparison and optimization Alexander Konduforov Email: [email protected] Weblog: www.merle- amber.blogspot.com

Upload: alexander-konduforov

Post on 26-May-2015

17.313 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Ling to SQL and Entity Framework performance analysis

Linq to SQL and Entity Framework performance comparison and optimization

Linq to SQL and Entity Framework performance comparison and optimizationAlexander KonduforovEmail: [email protected]: www.merle-amber.blogspot.com

Alexander KonduforovEmail: [email protected]: www.merle-amber.blogspot.com

Page 2: Ling to SQL and Entity Framework performance analysis

GoalsGoals

explain the L2S and EF architectures give an understanding of performance losses

and bottlenecks describe the possible ways of performance

improvements check the real benefits of the performance

improvements analyze the results, give advices

explain the L2S and EF architectures give an understanding of performance losses

and bottlenecks describe the possible ways of performance

improvements check the real benefits of the performance

improvements analyze the results, give advices

Page 3: Ling to SQL and Entity Framework performance analysis

General ORM architectureGeneral ORM architecture

Objects providerObjects provider

Query

Records

Data

Objects

SQL

Database providerDatabase provider

DatabasesDatabases

Databases

Query

MappingMapping

Page 4: Ling to SQL and Entity Framework performance analysis

Linq to SQL architectureLinq to SQL architecture

Linq to SQL Data ContextLinq to SQL Data Context

LINQ

Records

Data

IEnumerable<T>SQL

SQL

LINQ

Linq to SQL Data ProviderLinq to SQL Data Provider

MS SQL Server

SQL

Page 5: Ling to SQL and Entity Framework performance analysis

Entity Framework architecture

Entity Framework architecture

ADO.NET Data ProvidertADO.NET Data ProvidertADO.NET Data ProviderADO.NET Data ProviderADO.NET Data ProviderADO.NET Data Provider

Object ServicesObject Services

LINQ to EntitiesLINQ to Entities

CCT

xDataReader

DBDataReader

IEnumerable<T>ESQL

CCT

ESQL

LINQ

EntityClient Data ProviderEntityClient Data Provider

Page 6: Ling to SQL and Entity Framework performance analysis

Internal caching mechanisms

Internal caching mechanisms

Context metadata, etc. (app-domain level) Materialized objects (context level, first level

cache) EntitySQL queries (app-domain level) Linq queries (when compiled only) ADO.NET providers internal caching No second level caching like in NHibernate

Context metadata, etc. (app-domain level) Materialized objects (context level, first level

cache) EntitySQL queries (app-domain level) Linq queries (when compiled only) ADO.NET providers internal caching No second level caching like in NHibernate

Page 7: Ling to SQL and Entity Framework performance analysis

Main performance lossesMain performance losses

Infrastructure initialization Mapping overhead (inheritance, complex

mapping) Query analysis SQL generation Materialization

Infrastructure initialization Mapping overhead (inheritance, complex

mapping) Query analysis SQL generation Materialization

Page 8: Ling to SQL and Entity Framework performance analysis

Performance considerationsPerformance

considerations

Views pre-generation (EF only) Tracking vs. no tracking Objects materialization and context-level

caching Linq queries compilation Linq to Entities vs. EntitySQL (EF only) Lazy vs. eager loading Data modification SQL generation

Views pre-generation (EF only) Tracking vs. no tracking Objects materialization and context-level

caching Linq queries compilation Linq to Entities vs. EntitySQL (EF only) Lazy vs. eager loading Data modification SQL generation

Page 9: Ling to SQL and Entity Framework performance analysis

Tests suite and toolsTests suite and tools

Northwind database (yeah!)

Data retrieval tests: simple data retrieval advanced data retrieval (lazy loading) advanced data retrieval (optimal)

Data modification tests simple modification (10) advanced modification (with relations, 10) heavy modification (1000)

Northwind database (yeah!)

Data retrieval tests: simple data retrieval advanced data retrieval (lazy loading) advanced data retrieval (optimal)

Data modification tests simple modification (10) advanced modification (with relations, 10) heavy modification (1000)

Page 10: Ling to SQL and Entity Framework performance analysis

Initialization and views pre-generation

Initialization and views pre-generation

Context data (metadata, etc.) are cached in the app-domain

Connections are opened when needed Views can be pre-generated for EF: EdmGen.exeUse it to avoid cold start

Context data (metadata, etc.) are cached in the app-domain

Connections are opened when needed Views can be pre-generated for EF: EdmGen.exeUse it to avoid cold start

Data Access

Readers L2S EF

Time, ms. 12 38 207

Page 11: Ling to SQL and Entity Framework performance analysis

Tracking vs. no trackingTracking vs. no tracking

Tracking is used for: Identity Map for the materialized objects tracking changes of the materialized objects

No Tracking can be used: when you don’t need to modify objects

Tracking is used for: Identity Map for the materialized objects tracking changes of the materialized objects

No Tracking can be used: when you don’t need to modify objects

Data Access

Readers L2S L2S (no tracking)

EF EF (no tracking)

Simple 20 23 20 50 25

Advanced (lazy)

33 98 30 140 138

Advanced (optimal)

23 83 79 88 61

Page 12: Ling to SQL and Entity Framework performance analysis

Objects materialization and context-level caching

Objects materialization and context-level caching

Materialization is: creation of the objects in memory cost-expensive (40-70% of the query)

Created objects are saved in the context (when Tracking)

To create context for every query or to use the same context for few queries? What is better?

Materialization is: creation of the objects in memory cost-expensive (40-70% of the query)

Created objects are saved in the context (when Tracking)

To create context for every query or to use the same context for few queries? What is better?

Data Access

Readers

L2S (many)

L2S (one) EF (many)

EF (one)

Simple 23.3 23.8 13.8 54.1 36.1

Advanced (lazy)

35.2 107.4 36.6 148 54.5

Advanced (optimal)

24.2 79.2 77.9 79.8 76.9

Page 13: Ling to SQL and Entity Framework performance analysis

Linq queries compilationLinq queries compilation

EntitySQL queries are cachedLinq to Entities queries are not cached

Every Linq query can be compiled and thus “cached”

EntitySQL queries are cachedLinq to Entities queries are not cached

Every Linq query can be compiled and thus “cached”

Data Access

L2S L2S (compiled)

EF EF (compiled)

Simple 23 21 48 42

Advanced (lazy)

102 97 137 109

Advanced (optimal)

85 63 82 42

Page 14: Ling to SQL and Entity Framework performance analysis

Linq to Entities vs. EntitySQLLinq to Entities vs. EntitySQL

What is better to use and when?

Linq to Entities: type safety easy-to-use, code completion

What is better to use and when?

Linq to Entities: type safety easy-to-use, code completion

EntitySQL: similar to SQL, flexible low level => faster

EntitySQL: similar to SQL, flexible low level => faster

Data Access Linq to Entities

Linq to Entities (compiled)

EntitySQL

Simple 48 42 44

Advanced (lazy)

137 109 98

Advanced (optimal)

82 42 32

Page 15: Ling to SQL and Entity Framework performance analysis

Lazy vs. eager loadingLazy vs. eager loading

Eager loading is resource-consuming Lazy loading is time-consuming

L2S has implicit lazy loading built-inEF has explicit lazy loading built-in

It is always a dilemma… Get what you really need!

Eager loading is resource-consuming Lazy loading is time-consuming

L2S has implicit lazy loading built-inEF has explicit lazy loading built-in

It is always a dilemma… Get what you really need!

Data Access

L2S L2S (compiled)

Linq to Entities

Linq to Entities (compiled)

EntitySQL

Advanced (lazy)

102 97 137 109 98

Advanced (eager)

85 63 82 42 32

Page 16: Ling to SQL and Entity Framework performance analysis

Data modificationData modification

L2S and EF contexts are Units of Work:get => change => save all

Only when tracked

L2S and EF contexts are Units of Work:get => change => save all

Only when tracked

Data Modification

Commands L2S EF

Simple 42 153 52

Advanced 75 268 156

Heavy 2,285 12,075 2,952

Page 17: Ling to SQL and Entity Framework performance analysis

SQL generationSQL generation

Get your SQL profilers ready!

Get your SQL profilers ready!

Queries Readers/Commands

L2S EF

Simple retrieval 33% 33% 33%

Advanced retrieval (lazy) N/A N/A N/A

Advanced retrieval (optimal)

21% 55% 24%

Simple modification 33% 33% 33%

Advanced modification N/A N/A N/A

Heavy modification N/A N/A N/A

Page 18: Ling to SQL and Entity Framework performance analysis

Performance advicesPerformance advices

choose the appropriate data access technology make views pre-generation when possible use the context as long as possible but be aware

of memory overload and concurrency issues turn off tracking when it can be done use Linq queries compilation for complex queries use eager loading when you know what you need

to get use EntitySQL for most time-cost operations be careful with data modification do not afraid to check the generated SQL,

consider using indexes when possible

choose the appropriate data access technology make views pre-generation when possible use the context as long as possible but be aware

of memory overload and concurrency issues turn off tracking when it can be done use Linq queries compilation for complex queries use eager loading when you know what you need

to get use EntitySQL for most time-cost operations be careful with data modification do not afraid to check the generated SQL,

consider using indexes when possible

Page 19: Ling to SQL and Entity Framework performance analysis

Q&AQ&A

Go ahead!Go ahead!

Page 20: Ling to SQL and Entity Framework performance analysis

Useful linksUseful links

http://msdn.microsoft.com/en-us/library/cc853327.aspx http://blogs.msdn.com/adonet/archive/2008/03/27/ado-net-

entity-framework-performance-comparison.aspx http://blogs.msdn.com/adonet/archive/

2006/08/21/710862.aspx http://blogs.msdn.com/adonet/archive/2007/02/14/entity-

client.aspx http://blogs.msdn.com/adonet/archive/2007/05/30/

entitysql.aspx http://blogs.msdn.com/adonet/ http://mtaulty.com/CommunityServer/blogs/

mike_taultys_blog/archive/2007/08/03/9558.aspx http://oakleafblog.blogspot.com/2007/08/mike-taulty-

compares-linq-to-sql-and.html http://blogs.msdn.com/ricom/archive/2007/06/22/dlinq-linq-to-

sql-performance-part-1.aspx http://www.code-magazine.com/article.aspx?

quickid=0712042&page=1 http://msdn.microsoft.com/ru-ru/magazine/cc163399.aspx

http://msdn.microsoft.com/en-us/library/cc853327.aspx http://blogs.msdn.com/adonet/archive/2008/03/27/ado-net-

entity-framework-performance-comparison.aspx http://blogs.msdn.com/adonet/archive/

2006/08/21/710862.aspx http://blogs.msdn.com/adonet/archive/2007/02/14/entity-

client.aspx http://blogs.msdn.com/adonet/archive/2007/05/30/

entitysql.aspx http://blogs.msdn.com/adonet/ http://mtaulty.com/CommunityServer/blogs/

mike_taultys_blog/archive/2007/08/03/9558.aspx http://oakleafblog.blogspot.com/2007/08/mike-taulty-

compares-linq-to-sql-and.html http://blogs.msdn.com/ricom/archive/2007/06/22/dlinq-linq-to-

sql-performance-part-1.aspx http://www.code-magazine.com/article.aspx?

quickid=0712042&page=1 http://msdn.microsoft.com/ru-ru/magazine/cc163399.aspx