Partition in Sql Server Working
CHECK if any Partition present in our database
SELECT
o.name objectName,
i.name as indexname,
partition_id,
partition_number, [rows]
from sys.partitions p
INNER JOIN sys.objects o on o.object_id = p.object_id
INNER JOIN sys.indexes i on i.object_id=p.object_id and p.index_id=i.index_id
WHERE o.name like '%TransactionHistoryArchieve%'
Check File groups already present in our database
- Only single file group is present
STEP 1: determining the range. What kind of data do we want to specify the range by? Check year wise range
- Year wise records present
SELECT
DISTINCT YEAR(TransactionDate) AS [YEAR], COUNT(*) AS TotalRows
FROM AdventureWorks2012.Production.TransactionHistoryArchive
GROUP BY YEAR(TransactionDate)
ORDER BY 1
STEP 2: CREATE FILE GROUP CONTAINERS
- Now, in a production situation, to get even greater speed when you split a table, If you have them, you should make the file groups on different physical discs.
- For testing purpose lets create for folder with drive names in a Drive
- `
- `
STEP 3: Create file group
- Create file group for each drive. right now its not mapping with folders
- CREATE these file group in Master Database
use master
goalter database AdventureWorks2012
ADD FILEGROUP [Drive W]alter database AdventureWorks2012
ADD FILEGROUP [Drive X]alter database AdventureWorks2012
ADD FILEGROUP [Drive Y]alter database AdventureWorks2012
ADD FILEGROUP [Drive Z]
- Create four file groups for each year, 2011, 2012, 2013, and all rest of the data that is not in the first three partitions.
- Now check properties of database [AdventureWorks2012] > filegroups. We can see 4 additional, file groups or containers that have been created.
STEP 4: Create data file (.ndf) in each file group and maps each with respective Drives/ Drive folder (in our case)
Create 4 data files, .ndf files in the file group for each partition'
ALTER DATABASE AdventureWorks2012
ADD FILE (
NAME = N'TRANS2011',
FILENAME=N'D:\Sql Partition\Drive W\TRANS2011.ndf',
SIZE = 4096KB,
FILEGROWTH = 1024KB
)
TO FILEGROUP [Drive W]
GO
ALTER DATABASE AdventureWorks2012
ADD FILE (
NAME = N'TRANS2012',
FILENAME=N'D:\Sql Partition\Drive X\TRANS2012.ndf',
SIZE = 4096KB,
FILEGROWTH = 1024KB
)
TO FILEGROUP [Drive X]
GO
ALTER DATABASE AdventureWorks2012
ADD FILE (
NAME = N'TRANS2013',
FILENAME=N'D:\Sql Partition\Drive Y\TRANS2013.ndf',
SIZE = 4096KB,
FILEGROWTH = 1024KB
)
TO FILEGROUP [Drive Y]
GO
ALTER DATABASE AdventureWorks2012
ADD FILE (
NAME = N'TRANS2014',
FILENAME=N'D:\Sql Partition\Drive Z\TRANS2014.ndf',
SIZE = 4096KB,
FILEGROWTH = 1024KB
)
TO FILEGROUP [Drive Z]
GO