Entity Framework for DBAs

Published 04 September 07 05:04 PM | dpblogs 

 

There are two new data access technologies coming out of Microsoft in the coming year that have particular interest for DBAs: the Entity Framework and LINQ to SQL. Both of these technologies are going to radically change the access patterns that you see as a DBA, and the way that you work. What is the impact of these technologies on a DBA? Are they a step in the positive direction?

 

For this post, I am going to focus on the Entity Framework, and look at some of its features and capabilities as they relate to being a DBA. If you would like an introduction to the Entity Framework as a whole, this post is a great place to start.

 

Fear and Loathing

When most DBAs first hear about LINQ and the Entity Framework, the first reaction is revulsion. In a previous life, I wore a DBA hat, so my initial reaction was not too different. Why?

 

Take a look at a LINQ to Entities statement like the following.

 

var query = from Cust in Customers

            where Cust.LastName == “Smith”

            select Cust.ID;

 

Ok, that is not so bad, and not too different from something you might see in a stored procedure. Now, take a look at this.

 

var query = Customers.GroupBy(c => c.Region)

    .Select(g => new

                 {

                     Region = g.Key, FreightTotal = g

                    .SelectMany(c2 => c2.Orders)

                    .Sum(o => o.Freight)

                 });

 

What exactly is this going to do to your database? How many table scans are going to occur? It might be a great query, or it might bring your server to its knees. What happens when you need to normalize the Customers table for performance? What is it that we gain for this uncertainty?

 

The Journey Forward

The journey from embedded SQL to where we are today has been long and arduous. We have passed a lot of mileposts: T-SQL, views, stored procedures, etc. With each step, DBAs have been able to exercise more control over the database, both in terms of administration and in usage, while allowing the application to develop in its own way. This has allowed productivity to increase, and systems to become less fragile.

 

The introduction of the Entity Framework furthers this tradition, and has the benefit of giving the DBA more power and control over the system.

 

Control

In my mind, the key attribute of a DBA is that the power of control rests with them. The DBA is the guardian of performance, security and reliability. None of these can be achieved without the ability to control what goes on; what SQL is executed, what the database structure looks like, who can run what, etc.

 

The above LINQ query looks a lot like something you would write in T-SQL (odd syntax notwithstanding), but actually, you are writing to the conceptual model. What does this mean? This means that you now have more control over the database!

 

How does this work? Now, you do not need to care how ugly that LINQ query looks, as it all works against the logical model. So, you have more control over the database, because as long as you make sure that the logical model remains consistent, you can do whatever you need to do in order to make the system work!

 

Model

Database models are nothing new. In fact, they are very old. In the beginning, there was EF Codd, who brought to bear the power of relational algebra. There was also Chen, who created the Entity Relational Model (ERM). From these, the basic database model was born. But like most things in computer science, a little seasoning was in order. And nothing seasons like a well chosen abstraction.

 

The evolution of views

First there were joins, which allowed for normalization to occur. However, as databases became larger and more complex, queries started to look like the tax code. To help, another tasty abstract layer arrives: views. Views allowed DBAs to consolidate a set of joins into a logical “table”, and, with later versions of SQL Server, even allowed for some level of updating (if you were willing to write instead-of triggers).

 

By abstracting the object on which you query data from the way in which you store data, views gave DBAs more control and flexibility over the structure of the database. Tables could be changed, normalized, partitioned, and as long the compensating changes were made in the view, life was good, if a little boring with all the CRUD SQL that needed to be written.

 

Stored Procedures – an alternative evolution

Views are not the only way to abstract the structure of a database; another common approach is to use stored procedures. This gives the DBA the ultimate in flexibility. The entire API for the database can be explicitly defined, but it is flat, and you cannot compose them, so all that powerful SQL is no longer usable by the developer.

 

In addition, you still need to maintain all those nasty novel sized SQL statements, but at least the developer cannot see them!

 

The Entity Framework

With the Entity Framework (EF) and the Entity Data Model, the DBA/Architect now has a system that has, and expands on, the benefits of both views and stored procedures. This framework currently resides outside of the database, but it can be thought of as a part of the data layer.

 

Now, instead of having to hand manage views (including those oh-so-fun instead of triggers), the Entity Framework will do the heavy lifting for you. The Entity Framework contains some very intelligent view mapping technology, so if you can declare the mapping, you can update it!

 

Now, instead of having to write thousands of stored procedures to control the API of a database, you can create a logical model that becomes the API. And when you need the benefit of stored procedures for performance or logic, you can still plug them in.

 

Does this mean that the Entity Framework will replace views and stored procedures? NO!

 

Stored procedures and views are still very powerful and useful technologies. Performance and security are two key benefits of stored procedures and views, and there is no reason to throw the baby out with the bathwater! Myself, I would not give up indexed views for the world; those things are very cool.

 

Before the Entity Framework, the API between the database and the application was like a pond. Any time you touched anything, the mud would rise. Likewise, any change to the database might affect the API, and potentially cause things to break.

 

 

 

 

After the Entity Framework, the API is now explicit. It can still use the same technologies and techniques on the database side, but it is not cloudy any more. It gives you the flexibility to change things, plus it allows for some cool benefits for the application developer, like ORM, LINQ, and Entity SQL.

 

 

 

So the Entity Framework lets you continue to use the technologies that you love (stored procedures, views, etc) and even those you have to tolerate (dynamic SQL). Except that now you don’t need to jump through hoops to support an implicit API for accessing the database; you can now worry about more important things.

 

We now live in the land of milk and honey, right? Remember, there is no such thing as a free lunch. So what do you give up? Some measure of query control. But the gain in database control and flexibility far outweighs the loss of query control, in my opinion.

 

The best use of your time

We now have a way to cleanly define a logical API for developers, one that does not require weeks and weeks of CRUD development, and a long involved process with each change. That alone is worth the price of admission!

 

The Entity Framework is going to you as a DBA the ability to make better use of your time, and to spend some on making the system better, instead of changing random SQL queries all day. Like any version 1 of a product, I am sure that the Entity Framework will cause some problems for DBAs, but the upside is huge, and that is why my DBA side has come around to seeing the benefit of the Entity Framework.

 

If you are attending the 2007 PASS Community Summit in Denver this September, I encourage you to come to my session on this very topic. The session is called Entity Framework for DBAs – what’s in it for me? and is running from 3 until 4:15 on Wednesday the 19th.

 

Viva la Entity Framework revolution!

 

Erick Thompson

Program Manager

Microsoft

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# MSDN Blog Postings » Entity Framework for DBAs said on September 4, 2007 11:04 PM:

PingBack from http://msdnrss.thecoderblogs.com/2007/09/04/entity-framework-for-dbas/

# Andres Aguiar said on September 5, 2007 10:42 AM:

Good start, but it will take muuuch more than that ;)

# Erick Thompson said on September 5, 2007 7:42 PM:

Andres,

I assume that you are from the DBA side of the house. What more would you like to see? What are your concerns?

Thanks,

Erick

# From the software development trenches said on September 7, 2007 1:42 AM:

Time for another weekly roundup of news that focuses on .NET and general development related content

# triach said on September 7, 2007 7:10 AM:

I like to concepts and thoughts about how this helps the DBA - but we all know when something like this comes up some will abuse this and ignore SP's and views etc and just use this. This will cause the DBA more heart ache unless as describe above it is used in a controlled manner and maintained within a give guideline of performance vs speed of development.

I will watch this very closely and have not made a decision either way - lets see what comes as it is release and I can actually see what affects it has.

# Matt Neerincx said on September 7, 2007 2:26 PM:

If I was a DBA, I would ask the following questions:

1. How can I control what entities are used against my server?

2. What prevents different development groups from using slightly different entity models?  For example dev group 1 creates customer model that is different from dev group 2.  The different models cause slightly different queries to occur on the back-end, and this increases the number of query plans on the server cache.  Server is thus not used efficiently.  I can control this with enforcing good development practices only?  As a DBA I want to control this using access control on server side.

3. How can I as a DBA understand the entitie model and what sets of queries it will create?  Are there any tools to analyze the entity model and determine what queries will be produced?  As a DBA I don't understand entity language, I understand TSQL language, so if the tool could tell me what TSQL queries will be produced, then I could understand the implications of this an create indexes ahead of time to optimize the performance.

4. Does EDM have a way to dump it's showplan like SQL does?  This way I can change my EDM code to produce a better plan (perhaps).

5. Does EDM have a analyzer that can tell me where I need to add indexes (like SQL's Index Tuning Wizard)?  Or do I just need to write a bunch of EDM code, run my app with SQL Profiler running, then run Index Tuning Wizard on the result?

6. How can I optimize my EDM code to ensure I optimize my queries on the server side?  With normal ADO.NET I can write the TSQL to use parameterized TSQL and ensure I have good cache hits.

So the main questions I can think of are about hitting optimum performance with SQL Server.  DBA will want to know exactly what queries are produced and how to control what queries are produced (if possible).   Random TSQL is worst case behavior, as it causes cache to blow out on server side.  If there are ways to dump showplan for EDM this would help as well.  If there is a way to convert EDM to TSQL this would help DBA understand impact as well.

# BonzBlog Michaela Juřka said on September 17, 2007 3:07 PM:

Poslední reinkarnace LINQu, o které jsem se zmínil v přehledovém článku , je LINQ to ADO.NET Entities,

# BonzBlog Michaela Juřka said on September 17, 2007 3:09 PM:

Poslední reinkarnace LINQu, o které jsem se zmínil v přehledovém článku , je LINQ to ADO.NET Entities,

# Erick Thompson said on September 18, 2007 4:42 PM:

Matt,

Those are some great questions. I'll be addressing these questions, and others, in the future. I'm going to be doing a webcast on this topic on Sept 27th, so please stop by with your questions.

Thanks,

Erick

# Cristian's Blog said on October 3, 2007 4:08 AM:

SQL Server 2008 is at the door and the same stands for Visual Studio 2008. The data access strategy is

# Rowland Gosling said on November 29, 2007 12:04 PM:

I had an artist acquaintance who'd paint pictures of food. This looks an awful like his 'Hotdog in Search of Mustard' to me.

Maybe I've lost a few too many brain cells along the way but I don't see a benefit. I'm both a DBA and Devoper. I've seen a lot of code generators/techniques come and go. This might be a bit more flexible than ORM--or even useful to some extent. Or maybe not.

# Siôn said on June 4, 2008 10:21 AM:

Sounds great, but how do you access your Stored Procedures (Entity Functions) from code?  

For example I have a trued and tested stored procedure that updates 5 tables and I would like to access it using the Entity Framework.  Is this possible without having to write loads of custom code?

Leave a Comment

(required) 
(optional)
(required) 
Page view tracker