Sunday, 8 November 2015

Tips for SQL Server Database Design

Database designing is a very vast and very crucial phase. If you belong to group whom goes with Database First Approach, then any decision you make in phase may have huge impacts. I would like to highlight few important points which can be worthy to consider for better database designing and performance.  
  • Always try to device a naming convention and few standards like field size, datatypes, and general assumptions before you start designing database. These can save lot of time and efforts. These can even play vital role in performance.
  • Consider Demoralization, particularly for very large data. But as rule of thumb, first Normalize your Database. It is a good idea to use Indexed view for Demoralization.
  • It will be better to define Primary Key and Foreign Key Relationships.
  • Clearly finalize Database Isolation Level. 
  • Clearly define sequence of involvement to tables in transaction to avoid deadlocks.
  • It is good practice to have one Identity Column in a table, either it is primary key or not.
  • Precisely plan Indexing, and keep in mind, it is ongoing process. Be specific to choose column for clustered and non clustered indexes. In general usage of columns in join, where, order by and group by directly affect your index requirements.
  • You need a good plan for index defragmentation, particularly when there are lots of insert, delete and update operations.
  • Consider new technologies and out of box solution. In general, I may prefer out of box features instead using old custom methodologies until unless there is some specific reason to do so like backward compatibility.
    • Consider built in Table Partitioning feature instead of manual table partitioning for large data tables.
    • Consider File Stream introduced in SQL Server 2008 for File Storage.
    • Consider new DataTypes over old wherever possible, like Varchar(max) over Text, Date over DateTime and etc.
    • Consider In Memory Optimized Tables. Now RAM is very cheap, and such data cache techniques can make a great difference in performance.
  • Consider splitting database into multiple files based on your database size.
  • Consider separation of database files and log files on different hard disk.
  • Always use appropriate datatype, for example if you have to store English data only then you don't need to use nvarchar, or nchar, if you need to store small value set, then there is no need to use bigint.
  • Consider Summary Table or Pre Calculated Data for very large data.
  • Plan a clear strategy for performance and maintenance. In many cases you have to address Performance vs Maintenance. Ideally you must go with some acceptable combination of both, but this decision is directly dependent on your needs and budget.
  • It will be better to same standard for Database objects (Table and Column) and Application Data Model. Although it may not give any performance gain, but it can facilitate developers.
  • If you are planning to use Triggers or Cursors then reevaluate alternate approaches.

4 comments:

  1. Replies
    1. I have an article particularly for SQL Programming, it is not specific to stored procedures, but in general you will find it helping:
      http://imranjavedzia.blogspot.com/2015/11/tips-for-sql-programing.html


      if you like then you can use this utility to create stored procedure stubs:
      http://imranjavedzia.blogspot.com/2015/10/sql-server-stored-procedure-helper.html

      Delete
  2. Plus:

    - Don't feel like the database schema can't change.

    - If you document anything then document your database design.

    ReplyDelete
    Replies
    1. Thanks Brian for highlighting these important points, I totally agree with you.
      I would like to add, if our Analysis is strong, then ideally there may not be much change in schema. And of course, documentations (Logical Design, Physical Design, Data Dictionary) are very important particularly for large projects.

      Delete