Presentation is loading. Please wait.

Presentation is loading. Please wait.

Why should I care about SQL, if I have ORM?

Similar presentations


Presentation on theme: "Why should I care about SQL, if I have ORM?"— Presentation transcript:

1 Why should I care about SQL, if I have ORM?
Denis Reznik Why should I care about SQL, if I have ORM?

2 Thank you to our AWESOME sponsors!

3 About Me Denis Reznik Kyiv, Ukraine Data Architect at Intapp, Inc.
Microsoft Data Platform MVP Co-Founder of Ukrainian Data Community Kyiv (PASS Chapter) PASS Regional Mentor, Central and Eastern Europe Co-author of “SQL Server MVP Deep Dives vol. 2”

4 Agenda .Net ORM History (how I remember it) Simple Queries Column List Lazy Load Data Types Data Modification Complex Query Parameter Sniffing The Main Thing to Keep in Mind

5 ORM History which I remember
ADO.NET Data Sets (not too bad) LLBL Gen, NHibernate (better than data sets) LINQ to SQL (Awesome!) Entity Framework. Should be even more awesome! Entity Framework is more powerful than L2S Entity Framework Code First Entity Framework is not so good? So what?

6 Simple Query SELECT * FROM Users WHERE Id = 1

7 Simple Query Demo

8 Column List

9 Column List Demo

10 Lazy Load

11 … … Index Seek SELECT * FROM Users WHERE Id = 523 1 .. 1M 2K+1 .. 4K
1M-2K .. 1M ,5K 1,5K K

12 … … Index Range Scan SELECT * FROM Users WHERE Id BETWEEN 700 AND 1700
1 .. 2K 2K K 1M-2K .. 1M ,5K 1,5K K

13 … … Index Scan SELECT * FROM Users 1 .. 1M 2K+1 .. 4K 1M-2K .. 1M
,5K 1,5K K

14 Can Scan Outperform Seek?
SELECT user_id FROM Statuses WHERE user_id < 2000 Leaf-Level Rows: Leaf-Level Pages: 2970 Rows on Page (Avg): 356 Index Depth: 3 One Page Seek: 356*3= 1068 1068*3=3204 > 2870 => Row by Row Seek of 3 pages is heavier than full index scan (For this concrete case)

15 Lazy Load Demo

16 Data Types

17 Data Types Demo

18 Data Modification

19 Data Modification Demo

20 Complex Query

21 What does it mean “Complex Query” ?

22 What does it mean “Complex Query” ?

23 Complex Query Demo

24 Parameter Sniffing

25 Parameter Sniffing - Stored Procedure
Query Processor EXEC = 1 EXEC = 22 SQL Server Cache Procedure cache Procedure cache Query executes using the query plan created for @UserId = 1 Plan created and cached for the @UserId = 1

26 Parameter Sniffing - Parametrized Query
Query Processor sp_executesql N'SELECT * FROM Users WHERE Id int', 1 SELECT * FROM Users WHERE Id SELECT * FROM Users WHERE Id sp_executesql N'SELECT * FROM Users WHERE Id int', 22 SQL Server Cache Procedure cache Procedure cache Plan created and cached for the @Id = 1 Query executes using the query plan created for @Id = 1

27 Dynamic SQL – Multiple Plans
Query Processor SELECT * FROM Users WHERE Id = 1 SELECT * FROM Users WHERE Id = 22 SQL Server Cache SELECT * FROM Users WHERE Id = 1 Procedure cache Procedure cache Procedure cache Query executed using the query plan, created for the first query. New query plan created and cached. Query executed using newly created plan. New query plan again created and cached. Query executed using newly created plan.

28 Parameter Sniffing Demo

29 The Main Thing to Keep in Mind

30 Life without ORM (Entity Framework)
Some Magic Query Result Set 32 |

31 Life with ORM (Entity Framework)
Some Magic Application Code SQL Query Some Magic Result Set 33 |

32 Life with micro-ORM (Dapper)
Some Magic Query Result Set 34 |

33 Summary ORM is really nice tool
But it still requires strong SQL Server background Databases and ORMs are changing very fast Few Advices Sometimes check the generated SQL and its query plan For complex queries consider raw SQL anyway Consider using Dapper Invest your time to learn database internals ORM should not save you from learning SQL and SQL Server

34 Thank You! Denis Reznik Twitter: @denisreznik
Blog: Facebook: LinkedIn:


Download ppt "Why should I care about SQL, if I have ORM?"

Similar presentations


Ads by Google