Why is it that in Oracle world, it's much more custom to have a database administrator (DBA) on your project than when you're working with SQL-Server? I believe it brings great value to a project to have a dedicated experienced DBA working in the team. And I think part of the problem is the fact that Oracle is much more complex to manage, whereas SQL-Server figures out a lot by itself. That's of course a lot of power coming from SQL-Server, but it has its negatives that most developers don't think they require a DBA that often.
I'm writing this because since LINQ and specifically LINQ to SQL has been released, I've had some discussions on wether the query engine of LINQ to SQL is smart enough to create solid and performing queries. There are a lot of things to say here, because when is a query fast enough? This also depends on the requirements of your architecture. Otherwise it's just a gut feeling of when it's not fast enough anymore.
But someone stated recently that
a LINQ to SQL query can never be as fast as a stored procedure written by a DBA
LINQ to SQL could never optimize your query because it doesn't know enough about the database, whereas a DBA does. The conclusion was that you should stick to regular ADO.NET. Of course this is nonsense. For multiple reasons
var query = db.CustomersByCity("London");
var query = from c in db.CustomersByCity("London")
select new { c.ContactName, c.City };
where c.ContactName.Contains('A')
I hope I've explained why it's not logical to easily ditch LINQ to SQL in favor of the regular ADO.NET and Stored Procedures, simply because of performance or other issues. I'm not saying you should use LINQ to SQL everywhere and I'm not saying it's the silver bullet. I'm also not saying you can never write more performing queries in Stored Procedures than LINQ to SQL can. When you're working with and querying large sets of data, you're probably better of using Stored Procedures instead of retrieving thousands of rows into your application. But not using it at all because of the wrong reasons and not giving it a try without some investigation or proof of concept, might be even worse. It can save you a serious amount of time in your development.
WARNING & DISCLAIMER: This is a long post, split over a series, as it discusses some old, well-entrenched
Considering I've put a few posts up about LINQ To SQL, I realised I've never shared some of the
I don't buy the whole ad-hoc queries are just as fast as stored procs, especially complex ones. From my understanding is is all about caching/compiling execution plans, which you again from my understanding you can't do with ad hoc queries. As far as being against logic in your data tier, that is you choice. I think in heavy data centric environments you almost have to put some logic into stored procs for various reasons. One being the database might be more available for changes then the code base. Two with really complex normalized data models good luck trying to put all that logic in code rather then in a stored proc. Often times you will end up making six or seven trips to a database so you can accomplish what could be done with one trip in a stored proc.
My two cents. I don't claim to be either a code nor SQL guru. I think as far as a development choice...more LINQ and less Stored Procs, or vice versa, is really a matter of choice for most developers. Let's face it, most of us aren't pushing the limits of acceptable performance for most projects anyway. Its always a best practice to code like we are, but sometimes cost/time/comfort level/knowledge plays into the equation as well.
@infocyde : about compiling and caching execution plans, read SQL Books Online and you'll know. It's there somewhere, I don't feel like looking it up :)
Your DBMS is better in working with large sets of data. If that's what you need to do, keep it in the database. Don't get many MB's of data into code to enumerate over the results.
I put 99.9% of all logic in my code, simply because I can. Only for large dataset optimizations I turn to the database.
Of course its still a choice. But since O/RM and/or Linq-to-SQL, I don't see any business logic anymore in my database. Now even if inline queries were slower (which they aren't) I'd sacrafice that to having all business logic in code.
One other element you aren't factoring into your test is the client (meaning web server) processing hit to handle business logic that might be done faster in a stored proc. I see that missed a lot in test. I also see a lot of speed test run with really simple data manipulations, I wish I could see more complex test posted. Plus for some operations if you are doing things in code you will have to make multiple trips to the db, which could degrade performance signifigantly.
I know some execution plans might be faster if generated on the fly rather then cached depending upon various parameters. For complex operations I would bet cached execution plans would almost always be faster, but I guess I could be wrong.
Anyway, if your apps do what they should do and the users of your apps are happy with them (and it sounds like they are), you are doing a great job and no one has the real right to question your design choices, so I will shut up. Success is my measurement of what works best and what doesn't.