Database Design Guidelines For Developers

One of the developers that I work with asked me to write a “brief” (really brief) guide on database design. What I have written below is a summary in bullet point form of best practices when creating databases, tables, indexes, and then querying the data within. It is designed to be short and sweet so the reader can check each section quickly when creating a new database. References are provided so that the reader can use if they wish to explore a certain topic in more detail.

Let me know what you think! Is there anything glaringly obvious that I’ve missed? My email is dbafromthecold@gmail.com

Database Design Guidelines

Database Design

  • PRIMARY filegroup should be reserved for system objects
  • Secondary DATA filegroup should be set to default.
  • Minimum of two physical files mapped to each secondary filegroups
  • Data and Log files presized for a minimum of one year’s data inserts
  • Data and Log files should reside on separate drives
  • Denormalisation should only be considered in special circumstances. A properly normalised OLTP database will outperform a denormalised one
  • Recommended collation is Latin1_General_CI_AS

Table Design

  • Each table created within the database should have a Primary Key and a Clustered Index. These need not be the same column
  • GUID fields should not be used for clustered indexes even if used as table’s Primary Key
  • Composite keys should be avoided. Surrogate INT IDENTITY keys can be used as a replacement
  • Columns set to the smallest size possible – avoiding NVARCHAR(MAX), TEXT etc datatypes
  • Avoid Nullable columns. Nullable columns require extra space within the database to designate the fact that they are nullable
  • Use referential integrity – Foreign keys and unique constraints should be applied

MSDN CREATE TABLE Documentation – http://technet.microsoft.com/en-us/library/ms174979.aspx

Querying Data

  • Stored procedures should be used as much as possible, to allow query execution plans to be re-used
  • Do not use SELECT * – SELECT only the columns needed to keep the query’s memory usage as low as possible
  • As a standard, cursors should be avoided. Removal of RBAR (row-by-agonising-row) processing
  • SET NOCOUNT ON should be at the start of each SQL batch to reduce network traffic
  • Dynamic SQL should be executed using sp_executesql.
  • Do not repeatedly call functions within stored procedures, functions, batches and triggers.
  • Sargability (Search ARGument Able) – Confirm queries executed are able to seek on indexes in database
    • Avoid wildcard characters at the beginning of a word while searching using the LIKE keyword
    • Avoid searching using not equals operators (<> and NOT)
    • Avoid functions in WHERE clause on table columns
    • Avoid implicit conversions in WHERE clause (use CAST/CONVERT if necessary)
BAD:   SELECT ... WHERE Year(StartDate) = 2014
FIXED: SELECT ... WHERE StartDate &gt;= '01-01-2014' AND StartDate &lt; '01-01-2015'

BAD:   SELECT ... WHERE isNull(Surname,'Williams') = 'Williams'
FIXED: SELECT ... WHERE ((Surname = 'Williams') OR (Surname IS NULL))

BAD:   SELECT ... WHERE LEFT(FirstName,1) = 'P'
FIXED: SELECT ... WHERE FirstName LIKE 'P%'

BAD:   SELECT ... WHERE DateDiff(mm,OrderDate,GetDate()) &gt;= 30
FIXED: SELECT ... WHERE OrderDate &lt; DateAdd(mm,-30,GetDate())

Sargability References

Stack Overflow – What makes a SQL Statement sargable? http://stackoverflow.com/questions/799584/what-makes-a-sql-statement-sargable
Rob Farley – Sargable Functions http://blogs.msmvps.com/robfarley/2010/01/21/sargable-functions-in-sql-server/
Brent Ozar – Why %string% is slow http://www.brentozar.com/archive/2010/06/sargable-why-string-is-slow/

Indexes

  •  Naming convention – Prefix_TableName_ColumnName
  •  Prefix – PK for Primary Keys, IX for non-clustered indexes, UC for Unique Indexes
  • Make sure the option ONLINE = ON is in place
  • Correct nonclustered indexes created for incoming queries – avoid RID and KEY lookups on tables
  • No Indexes on columns like “Gender” as this would only have a maximum of three possible entries (Male, Female & NULL) making an index ineffective. Nonclustered indexes must be as selective as possible

 

SQL Server 2014 RTM

The new version of SQL Server will be here on Tuesday.

(Someone at Microsoft has a sense of humour in releasing the newest version of SQL Server on April Fool’s Day.)

At some point the usual argument about when a company should upgrade will be had. I’ve worked companies that have waited for the first service pack of a new version of SQL to come out and I’ve worked for companies that start implementing the new version as soon as it comes out.

I can see a lot of companies moving to SQL Server 2014 pretty quickly for one reason, the increase in the memory limit for Standard Edition from 64GB to 128GB. There are companies out there that have large databases running in Standard Edition, simply because they do not want to fork out for an Enterprise Edition licence.

This month will be quite interesting as there are always problems with a new version of software (see the KB article for SQL Server 2012 SP1 ). I’ll be watching the Connect site with interest.

Happy New Year!

Happy New Year!

Hope you all had a good Xmas, ate too much, drank too much and are still telling yourselves that this year you will stick to your New Year’s resolutions (ha).

I didn’t blog in December because…I moved to Ireland to start a new job. I’ve always wanted to work abroad and for a Brit, working in Ireland is like abroad lite. Don’t get me wrong there are a lot of differences between Britain and Ireland but there are similarities which I find comforting (being able to get the same TV channels is a big one).

I’ve been in a hotel for the last several weeks whilst I removed all my furniture from my old flat in England (which I’ve geekily referred to as decommissioning) and whilst I searched for a new one in Ireland. But I’ve got a flat now so can start to relax a little bit on the personal front and start to concentrate on my new position.

I don’t want to blog about my new position or the company that I’m working for but I do want to talk about how I approach a new position in general. DBAs have a tendency to want to change things immediately when starting a new job, for example implementing their own backup and maintenance procedures.

In the past I have spent most of the first couple of weeks reviewing the environments in a new position, mainly to familiarise myself with them but also to make any notes for any changes I am going to recommend. After that I’ll speak to the incumbent DBA about any recommendations I have and go from there. For example, I like to disable the SA login but there may be apps that have been setup to use that account so simply disabling it may not be an option.

I guess the point I’m trying to make is that it’s not a good idea to go into a new job “all guns blazing”, wanting to make radical changes immediately. This can be difficult for me and I have had to restrain myself in the past but all it will do (at best) is make people think, “Who does this guy think he is?”

Introducing changes slowly with sound evidence for the reasons behind them will not put anyone’s nose out of joint and will also get your new colleagues to respect the knowledge that you are bringing to the table.

My favourite online references

There is a wealth of information about SQL Server online. Absolutely hundreds of blogs, white papers, editorials, the list goes on and on. Trying to filter out the really useful, accurate (!) information from the not so useful (or just plain wrong) can be tricky. There are conflicting opinions out there which can be really confusing when you are researching a problem you are experiencing.

Below are just a few of the sites that have helped me in my day to day work immensely.

SQL Server Central
Contains an absolute wealth of knowledge on SQL Server. Articles, editorials, training and a question of the day. I use the forum on this site more than any others.

Microsoft SQL Server Forums
The official Microsoft SQL Server forum. Loads of people willing to help you out with any problems/queries you may have.

Brent Ozar
These are the guys who wrote sp_Blitz. I’d definitely recommend downloading this proc, it’s a great way of getting a quick overview of a new SQL instance. The site also has a good blog.

Adam Mechanic
Download sp_WhoIsActive. A really useful proc that shows information on currently executing queries.

Twitter
#SQLHelp is a great way of asking quick questions regarding SQL Server

PluralSight
This site provides online training for a monthly fee. It’s not expensive and the information you can get out of the courses is invaluable. However, try not to end up (like me) buying a month subscription and only doing half a course!

SQL DBA With A Beard
An old colleague of mine who is an absolute powershell whizz. If you don’t know how to use powershell, you should start learning!

If you are going to learn powershell you could probably start here powershell.com but Rob’s blog has a lot of good information specifically aimed at DBAs.

SQL Server Central and Brent Ozar’s site send out weekly newsletters which I would strongly recommend that you sign up to. I have learnt so much from both these sites.

Obviously I’ve not even scratched the surface of what is available on the internet regarding SQL Server but the sites listed are an absolute must for your bookmarks.

My 5 pieces of advice for new DBAs

As my first post, I thought I’d list a few points that I would say to any new DBA. I’ll probably turn each one of these into a post on its own but for now here they are:-

Backups
Of all the tasks that you perform, making sure that there are recent, valid backups being taken is the most important. If something goes wrong, a restore is required and there is no valid backup, guess who will get the blame?

Production
Be paranoid, as the saying goes ‘Hope for the best, plan for the worst’. You are responsible for the security, resilience and performance of the live environment(s). Carefully review anything that is going to be deployed and make sure you have a rollback plan.

Research
There is a wealth of information on the internet regarding SQL Server from Books Online to blogs this like. When asking questions about SQL Server, the most common answer you are going to come across is ‘it depends’. For any questions that you have, collate information from multiple sources and then test, test, test, test, test (you get the idea)…

Mistakes
At some point along the line, you will make a mistake. Everyone does. I’m not saying this means you can start working rashly without care but even if you are the most diligent DBA in the world one day you will make a mistake. It’s how you react to it that matters, suck it up and learn from it (I know everyone says this, but what else can you do?).

Training
We are in a profession which relies on technology that is ever evolving, meaning that even if you’ve been working with SQL Server for 20+ years, there will always be something new to learn. I’d have a look at the Microsoft approved entry level course:- http://www.microsoft.com/learning/en-us/Course.aspx?ID=40364A

As your knowledge grows you can attend more advanced courses.