Ling to SQL and Entity Framework performance analysis - Presentation Transcript
Linq to SQL and Entity Framework performance comparison and optimization Alexander Konduforov Email: alexander.konduforov@gmail.com Weblog: www.merle-amber.blogspot.com
Goals
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
General ORM architecture Objects provider Query Records Data Objects SQL Database provider Databases Databases Databases Query Mapping
Linq to SQL architecture Linq to SQL Data Context LINQ Records Data IEnumerable<T> SQL SQL LINQ Linq to SQL Data Provider MS SQL Server SQL
Entity Framework architecture ADO.NET Data Providert ADO.NET Data Provider ADO.NET Data Provider Object Services LINQ to Entities CCT xDataReader DBDataReader IEnumerable<T> ESQL CCT ESQL LINQ EntityClient Data Provider
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
Main performance losses
Infrastructure initialization
Mapping overhead (inheritance, complex mapping)
Query analysis
SQL generation
Materialization
Performance 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
Tests 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)
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.exe
Use it to avoid cold start
207 38 12 Time, ms. EF L2S Readers Data Access
Tracking 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
61 138 25 EF (no tracking) 79 30 20 L2S (no tracking) 140 98 33 Advanced (lazy) 88 50 EF 83 23 Advanced (optimal) 23 20 Simple L2S Readers Data Access
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?
0 comments
Post a comment