Wednesday, November 9, 2011

FILE GROUP


The database files are stored on your hard drive with an extension of .MDF(Master Data File) and is named as Primary Data File. Every database must have at least one primary data file. This file may hold not only the data for the database, but also the location of all the other files that make up the database, as well as start-up information for the database catalog.
It is also possible to have secondary data files. These would have the suffix .NDF. Secondary data files allow you to spread your tables and indexes over two or more disks. The upside is that by spreading the data over several disks, you will get better performance.
By dividing a single MDF file into multiple filegroups we get better performance in context of Insertion,Updation and Selection.

The reason behind to split .MDF file and place them to different hard disks is to Increase Data Access and Fetching speed. If all the data is on single hard disk then at the time of accessing or fetching the head of the hard disk moves to different sectors on the same hard disk, this consumes time and affect performance. So if the .mdf file is on multiple hard disk then heads of these hard disks moves separately and that increase the data accessing and fetching.

For Better Performance Create different type of objects of database on different file groups.


How To Divide .Mdf File into Multiple Files?
1.Create Database  and Name it as TryFS


2.Select the File Group Option on the left side of the window.


3.Click on the Add Button and Add different FileGroups.


4.Come Back to General Tab and Click the Add Button to add file group, name it and Select File group From filegroup dropdownlist.







5.Default path to Different Hard Disks. Here I have use only drive D and E on the same system(on a single hardisk).But we can add path of different Hard Disks or Systems Attached to the Network.


6.Shows the part of .mdf file is saved in D Drive.



7. Shows the Remaining two Files are saved in E Drive with .ndf extention.


 
CREATE DATABASE OBJECTS ON DIFFERENT FILEGROUPS.
1.Department Table Created on Primary File Group.

USE [TryFG]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Department](
      [DeptId] [int] NOT NULL,
      [DeptName] [nvarchar](50) NULL,
 CONSTRAINT [PK_Department] PRIMARY KEY CLUSTERED
(
      [DeptId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF,
 IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO


2. Employee Table Created on Secondary File Group.
USE [TryFG]
GO
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Employee](
      [EmployeeId] [int] NOT NULL,
      [EmployeeName] [nvarchar](50) NOT NULL,
      [EmployeeAddress] [nvarchar](50) NOT NULL,
      [DeptId] [int] NULL,
 CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
(
      [EmployeeId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON,
 ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 90) ON [Secondary]
) ON [Secondary]
GO
ALTER TABLE [dbo].[Employee]  WITH CHECK ADD  CONSTRAINT [FK_Employee_Department] FOREIGN KEY([DeptId])
REFERENCES [dbo].[Department] ([DeptId])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Employee] CHECK CONSTRAINT [FK_Employee_Department]
GO


In this way we can Create different Database objects on different FileGroups.









Friday, November 4, 2011

Basic Introduction related to Performance


Use Normalization  - Speed updates, Slow in insertions , Fast Search , reports take time because of joining.
Denormalization –fast report (less joining).
Good database design use file groups (primary , secondary , log) .
FGs should be created on different physical disk for performance.
Those objects which compete for resources  should be on diff.  filegroups on diff. disks.
Place diff. tables (to be join) on diff. filegroup (table contain large amount of data).
Keep rows compact  i.e size of row should be small.
Avoid null compact  , use not null + default values.
Keep PK compact. They are Freq. referred in another table as FK.
Try to keep PK on integer value avoid character by character comparison.
Index on column that are used in where clause too much.
Make index on such column that eliminated max rows (not on sex but on name).
Avoid update on index keys instead use delete/insert .
Use fill factor judiciously (amt of space filled in a sql page).
Ordering of columns in index (leftmost should be one eliminates max rows).
Create / drop indexes as required.
Use execution plans to understand query performance.
Use SPs  almost  always.
Use parametrized queries.
Disable the sql messages . like n rows affected ( set no count on).
Avoid using cursors C or if as all required use forward only / read only.
Use transaction control judiciously. Start as late as possible, exit (commit / rollback) as early as possible.
Select only required columns . avoid using queries like “ “
Use table variables instead of temp tables.
Set auto compute statistics is true.



Performance
1.       DB Design
2.       Correct use of indexes
3.       File groups
4.       I/O from tables
5.       Calculation like summarizing (query advancement, direct use sum function in query using group by) )
6.       joins  (file on diff.  file groups)
7.       SPs (precompiled)
8.       Network traffic
Temp Database
1.       Set size of temp DB to a reasonable good value.
2.       Set its increment value not too small.
3.       Place temp DB  on fast I/O multiple disks (use file groups to place temp DB on disk other then user DB)

Security
1.       Create user with limited permissions  (use NTFS  FS)
2.       Keep DB on separate m/c rather  than on the one running Web Application
3.       Enable login auditing at OS+SQL level
4.       Drop guest user from SQLDB using SP_dropuser ‘guest’
5.       Use SPs (insert, update, delete via SPs)
6.       No direct query to base tables, use standard query views.
7.        Use role base security
8.       Secure erroelog by assigning by ntfs permission.