blandry (11/18/2009)
Throughout the 30 plus years of my career I have listened to techies arguing the speed issue. I understand that for some businesses, this is crucial, but we should not lose sight that speed is not the main concern for every installed copy SQL Server in the world. Speed is important to us here, but data accuracy is far more the big concern. I don't care if a query takes an extra minute or two, just so long as I can depend on the data returned.None the less, if you are going to have this competition and its purpose is educating the masses, I sure would like to see someone come out with a definitive list of "do's and don'ts" for effectively managing the speed issue. Seems like every time one SQL expert says "this" about speed and efficiency, another comes along and says "No, don't do that, do this..." about speed, and this "round and round" has gone on for years. Can someone step up and just list the 10 (or 20, 30?) "Commandments" for efficient querying? ...[/i]
Nope.
Different situations have different answers. We don't live in a world of absolutes.
Here are a few that are generally the difference between slow and fast:
Avoid cursors
Don't use cursors
Use something other than cursors (Could just repeat variations on that for the next ten lines, but I think I've made my point)
Index appropriately, including the clustered index
Assign the primary key appropriately
Normalize for OLTP
Denormalize appropriately for OLAP
Don't mix OLTP and OLAP in the same tables
Use appropriate data types and avoid implicit conversions
Use SARGable arguments in your Where and Join clauses
Avoid UDFs
Keep Views simple and to the point, no excess joins nor columns
Know the differences between CTEs/Derived Tables, Temp Tables, and Table Variables and use them appropriately
Constraints can help the execution plan engine, so use them where appropriate
Understand parameter sniffing and the steps to handle it
Understand the benefits and drawbacks to both horizontal and vertical table partitioning
Avoid complex triggers
Those are the top ones that come to my mind as general rules for a performant database. Every one of them, even the cursors curse, has exceptions. This leads to the
ONE TRUE RULE FOR A PERFORMANT DATABASE:
Understand what you're doing when you design, build and code!
- 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