C2-SQL Server Storage Architecture
C2-SQL Server Storage Architecture
Data Storage
Filegroups
Listing 6-1. SQL Server Round-Robin Allocation
USE Master
GO
--Create a database with three files in the primary filegroup.
CREATE DATABASE TEST
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'Test', FILENAME = N'D:\Data\SqlServer\TEST\TEST.mdf'),
( NAME = N'Test_File2',FILENAME = N'D:\Data\SqlServer\TEST\test_file2.ndf'),
( NAME = N'Test_File3',FILENAME = N'D:\Data\SqlServer\TEST\test_File3.ndf')
LOG ON
( NAME = N'Test_log',FILENAME = N'D:\Data\SqlServer\TEST\test_log.ldf');
GO
IF NOT EXISTS (SELECT name FROM sys.filegroups WHERE is_default=1 AND name =
N'PRIMARY')
ALTER DATABASE TEST MODIFY FILEGROUP [PRIMARY] DEFAULT;
GO
USE TEST
GO
--Create a table in the new database. The table contains a wide, fixed-length column
--to increase the number of allocations.
CREATE TABLE dbo.RoundRobinTable
(
ID INT IDENTITY PRIMARY KEY,
DummyTxt NCHAR(1000),
);
GO
--Create a Numbers table that will be used to assist the population of the table.
DECLARE @Numbers TABLE
(
Number INT
)
--Populate the Numbers table.
;WITH CTE(Number)
AS
(
SELECT 1 Number
UNION ALL
SELECT Number +1
FROM CTE
WHERE Number <= 99
)
INSERT INTO @Numbers
SELECT *
FROM CTE;
--Select all the data from the table, plus the details of the row’s physical location.
--Then group the row count.
--by file ID
SELECT b.file_id, COUNT(*) AS [RowCount]
FROM
(
SELECT ID, DummyTxt, a.file_id
FROM dbo.RoundRobinTable
CROSS APPLY sys.fn_PhysLocCracker(%%physloc%%) a
) b
GROUP BY b.file_id;
FILESTREAM Filegroups
Listing 6-2. Adding a FILESTREAM Filegroup
ALTER DATABASE TEST ADD FILEGROUP [Test_FS_FG] CONTAINS FILESTREAM;
GO
ALTER DATABASE TEST ADD FILE ( NAME = N'Test_FA_File1',
FILENAME = N'D:\Data\SqlServer\TEST\Test_FA_File1' )
TO FILEGROUP [Test_FS_FG];
GO
AS MyAlias;
Memory-Optimized Filegroups
Listing 6-6. Adding an In-Memory Filegroup and Container
ALTER DATABASE TEST ADD FILEGROUP [Test_InMemory] CONTAINS
MEMORY_OPTIMIZED_DATA;
GO
ALTER DATABASE Test ADD FILE ( NAME = N'InMemory',
FILENAME = N'D:\Data\SqlServer\TEST\InMemory') TO FILEGROUP [Test_InMemory];
GO
Expanding Files
Listing 6-9. Calculating Free Space in Each File
SELECT
file_id
,unallocated_extent_page_count * 1.0 / 128 'Free Space (MB)'
FROM sys.dm_db_file_space_usage;
Shrinking Files
--Listing 6-12. Shrinking a Database via T-SQL
USE TEST
GO
DBCC SHRINKDATABASE(N'TEST' );
Log Fragmentation
USE TestLogFragmentation
GO
--Create Inserts table
CREATE TABLE dbo.Inserts
(ID INT IDENTITY,
DummyText NVARCHAR(50)
);
--Create a Numbers table that will be used to assit the population of the table
DECLARE @Numbers TABLE
(
Number INT
)
--Populate the Numbers table
;WITH CTE(Number)
AS
(
SELECT 1 Number
UNION ALL
SELECT Number +1
FROM CTE
WHERE Number <= 99
)
INSERT INTO @Numbers
SELECT *
FROM CTE;
--Populate the example table with 100 rows of dummy text
INSERT INTO dbo.Inserts
SELECT 'DummyText'
FROM @Numbers a
CROSS JOIN @Numbers b
CROSS JOIN @Numbers c;
--Display the size of the log file, combined with the number of VLFs and a VLFs to GB
ratio
SELECT
name
,[Size in MBs]
,[Number of VLFs]
,[Number of VLFs] / ([Size in MBs] / 1024) 'VLFs per GB'
FROM
(
SELECT
name
,size * 1.0 / 128 'Size in MBs'
,(SELECT COUNT(*)
FROM @DBCCLogInfo) 'Number of VLFs'
FROM sys.database_files
WHERE type = 1
) a;