March 20, 2008 at 11:40 am
We are looking at rewriting a lot of our older code currently and looking at implementing new standards, but one point of question between different areas is where is the best place to execute queries? Is it generally better to have sql queries stored within the .Net code itself or to force the developers to do all interfacing with the database through stored procedures?
My current belief is that it is better to do it through stored procedures. This will help with security and allow small tweaks to be made on the back end without forcing recompiles and complete redeployment of the front end application for each one. It also more clearly compartmentalizes the code.
But is there something I am overlooking? Is this truly the best practice? And if so, is there more information to help bolster my case?
---
Timothy A Wiseman
SQL Blog: http://timothyawiseman.wordpress.com/
March 20, 2008 at 12:56 pm
Is this a plant? Are you looking to start a fight?
Well, I'll answer anyway.
My .00001 cents on this issue is, yes, use stored procedures as a standard method of development. First off, you get more granular security through the stored proc than you can through VB code. Second, you get more modular code by putting the code for database access into the database itself, allowing you to make changes to the internals of a query without having to redeploy the app.
You'll hear that performance will change in one direction or the other. That's not entirely true. If a reusable execution plan is generated from code or from TSQL, it doesn't really matter.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
March 21, 2008 at 12:56 pm
I have to agree with Grant. There are possible exceptions, but as a rule sprocs are the best standard for db access.
----------------------------------------------------------------------------
"No question is so difficult to answer as that to which the answer is obvious." - George Bernard Shaw
March 21, 2008 at 12:59 pm
Procs. Definitely.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
March 21, 2008 at 1:10 pm
Procs are the industry best practice.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
March 21, 2008 at 1:43 pm
Thanks for your help. And I was looking for help in an argument I am in, Grant. If I was right (and the consensus here seems to be that I am) I wanted more arguments to bring to bear in convincing my coworkers. If I was wrong I wanted to be able to change positions before I got myself too entrenched.
Any other arguments that might help sway my coworkers?
---
Timothy A Wiseman
SQL Blog: http://timothyawiseman.wordpress.com/
March 21, 2008 at 2:07 pm
Timothy,
The following articles may be of use to you.:w00t:
http://www.sqlservercentral.com/articles/Security/updatedsqlinjection/2065/
http://www.mssqltips.com/tip.asp?tip=1455
http://www.sommarskog.se/dynamic_sql.html
http://msdn2.microsoft.com/en-us/magazine/cc163917.aspx
----------------------------------------------------------------------------
"No question is so difficult to answer as that to which the answer is obvious." - George Bernard Shaw
March 21, 2008 at 6:59 pm
timothyawiseman (3/21/2008)
Thanks for your help. And I was looking for help in an argument I am in, Grant. If I was right (and the consensus here seems to be that I am) I wanted more arguments to bring to bear in convincing my coworkers. If I was wrong I wanted to be able to change positions before I got myself too entrenched.Any other arguments that might help sway my coworkers?
Sprocs offer tight data security (and encapsulation) that can be easily and efficiently managed by a DBA team. (By encapsulation I mean that the client does not need to know which tables/columns were accessed for the returned data - neither should it know).
Also, sprocs serve to better organize database-access code, meaning better code maintenability and reusability.
You will not get these advantages from db code embedded directly in your client app.
__________________________________________________________________________________
SQL Server 2016 Columnstore Index Enhancements - System Views for Disk-Based Tables[/url]
Persisting SQL Server Index-Usage Statistics with MERGE[/url]
Turbocharge Your Database Maintenance With Service Broker: Part 2[/url]
March 24, 2008 at 9:25 am
Thank you, DCarlson and Marios. The list of links and points were very useful.
---
Timothy A Wiseman
SQL Blog: http://timothyawiseman.wordpress.com/
March 24, 2008 at 10:23 am
There's also the data layer vs data ACCESS layer vs Business Layer argument. Embedding SQL calls just screws up the separation in my mind. It's not always a valid argument IMO, but it's kind of a big hammer when you discuss best practices.
Also - assuming you actually have a DBA role - you're undercutting the "specialist" and any optimization they might be able to provide by not putting the code in a place they can access. I mean - your devs don't REALLY want the DBA monkeying with their code? Well - in order to do their part of the job, the DBA's would kind of HAVE to if the SQL code isn't up to snuff. And the code would have to be recompiled and redeployed, just to fix SQL performance. That in itself is a costly scenario.
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
March 24, 2008 at 11:23 am
Matt Miller (3/24/2008)
There's also the data layer vs data ACCESS layer vs Business Layer argument. Embedding SQL calls just screws up the separation in my mind. It's not always a valid argument IMO, but it's kind of a big hammer when you discuss best practices.Also - assuming you actually have a DBA role - you're undercutting the "specialist" and any optimization they might be able to provide by not putting the code in a place they can access. I mean - your devs don't REALLY want the DBA monkeying with their code? Well - in order to do their part of the job, the DBA's would kind of HAVE to if the SQL code isn't up to snuff. And the code would have to be recompiled and redeployed, just to fix SQL performance. That in itself is a costly scenario.
Excellent point. Separation of roles is a MUST in any decent-size organization. DBAs are the best choice for database tuning and optimization.
__________________________________________________________________________________
SQL Server 2016 Columnstore Index Enhancements - System Views for Disk-Based Tables[/url]
Persisting SQL Server Index-Usage Statistics with MERGE[/url]
Turbocharge Your Database Maintenance With Service Broker: Part 2[/url]
March 25, 2008 at 9:00 am
When I was a newbie doing development (mid 90's), it was made very clear to me that you do NOT put inline SQL in your code - PERIOD. What if (God forbid) someone used "select * from MyTable" and they are expecting something in the tenth position and then a column is added in position two? Now what? Or what if a column is renamed (true, this is rare, but what if??), now you have to track down all the areas in the code that referenced that column. And quite frankly, I have few (if any) cases where data is retrieved with a simple select statement and a few joins. 98% of the time, there is much more "meat" to the code. Seems crazy to put that in your .NET code.
Lisa
March 25, 2008 at 9:13 am
I'm worried that the new LINQ technology shipping with SQL 2008 will open the door for more inline SQL code. I think it is a step in the wrong direction...
__________________________________________________________________________________
SQL Server 2016 Columnstore Index Enhancements - System Views for Disk-Based Tables[/url]
Persisting SQL Server Index-Usage Statistics with MERGE[/url]
Turbocharge Your Database Maintenance With Service Broker: Part 2[/url]
March 25, 2008 at 9:23 am
Marios Philippopoulos (3/25/2008)
I'm worried that the new LINQ technology shipping with SQL 2008 will open the door for more inline SQL code. I think it is a step in the wrong direction...
Oh - it's worse than that....MUCH worse in fact. It allows you to write pseudo-SQL inline code, which then gets processed through the LinQ engine, which then auto-generates the inline SQL code for you. That's right, it's essentially "inline Hibernate code", or the SSRS dynamic code generator used in a CRUD setting. Not one level of ugliness - you get 2 for the price of 1....
I'm anticipating having two separate cranium-shaped divots in my desk now. As Rachel Ray would say...."Yummo!"
By the way - did you know that NOLOCK is now a .NET method??? How about CHAOS?
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
March 25, 2008 at 9:31 am
You have excellent points about the LINQ technology. I have not yet had a chance to play with SQL Server 2008, is there a way for the DBA to disable or disallow the use of LINQ against that server?
---
Timothy A Wiseman
SQL Blog: http://timothyawiseman.wordpress.com/
Viewing 15 posts - 1 through 15 (of 20 total)
You must be logged in to reply to this topic. Login to reply