Chapter26 Examples
Chapter26 Examples
1
USE master;
CREATE DATABASE test_partitioned
ON PRIMARY
( NAME='MyDB_Primary',
FILENAME=
'd:\mssql\PT_Test_Partitioned_Range_df.mdf',
SIZE=2000,
MAXSIZE=5000,
FILEGROWTH=1 ),
FILEGROUP MyDB_FG1
( NAME = 'FirstFileGroup',
FILENAME ='d:\mssql\MyDB_FG1.ndf', SIZE = 1000MB,
MAXSIZE=2500, FILEGROWTH=1 ),
FILEGROUP MyDB_FG2
( NAME = 'SecondFileGroup', FILENAME ='f:\mssql\MyDB_FG2.ndf',
SIZE = 1000MB, MAXSIZE=2500, FILEGROWTH=1);
Example 26.2
USE master;
ALTER DATABASE test_partitioned
ADD FILEGROUP MyDB_FG3
GO
ALTER DATABASE test_partitioned
ADD FILE ( NAME = 'ThirdFileGroup',
FILENAME = 'G:\mssql\MyDB_FG3.ndf', SIZE = 1000MB,
MAXSIZE=2500, FILEGROWTH=1)
TO FILEGROUP MyDB_FG3;
Example 26.3
USE test_partitioned;
CREATE PARTITION FUNCTION myRangePF1 (int)
AS RANGE LEFT FOR VALUES (500000);
Example 26.4
USE test_partitioned;
CREATE PARTITION SCHEME myRangePS1
AS PARTITION myRangePF1
TO (MyDB_FG1, MyDB_FG2);
Example 26.5
USE test_partitioned;
CREATE TABLE orders
(orderid INTEGER NOT NULL, orderdate DATETIME,
shippeddate DATETIME, freight money)
ON myRangePS1 (orderid);
Example 26.6
USE test_partitioned;
declare @i int , @order_id integer
declare @orderdate datetime
declare @shipped_date datetime
declare @freight money
set @i = 1
set @orderdate = getdate()
set @shipped_date = getdate()
set @freight = 100.00
while @i < 1000001
begin
insert into orders (orderid, orderdate, shippeddate, freight)
values( @i, @orderdate, @shipped_date, @freight)
set @i = @i+1
end
Example 26.7
USE test_partitioned;
CREATE UNIQUE CLUSTERED INDEX CI_orders
ON orders(orderid)
ON myRangePS1(orderid);
Example 26.8
USE test_partitioned;
SELECT DISTINCT t.name
FROM sys.partitions p INNER JOIN sys.tables t
ON p.object_id = t.object_id
where p.partition_number <> 1;
Example 26.9
SELECT ps.name PartScheme,pf.name PartFunc,fg.name FileGroupName
FROM sys.indexes i
JOIN sys.partitions p ON i.object_id=p.object_id
AND i.index_id=p.index_id
JOIN sys.partition_schemes ps on ps.data_space_id=i.data_space_id
JOIN sys.partition_functions pf on pf.function_id=ps.function_id
JOIN sys.allocation_units au ON au.container_id=p.hobt_id
JOIN sys.filegroups fg ON fg.data_space_id=au.data_space_id
WHERE i.object_id = object_id('orders');
Example 26.10
USE AdventureWorksDW;
SELECT ProductAlternateKey
FROM FactInternetSales f JOIN DimDate t ON f.OrderDateKey = t.DateKey
JOIN DimProduct d ON d.ProductKey = f.ProductKey
WHERE CalendarYear BETWEEN 2003 AND 2004
AND ProductAlternateKey LIKE 'BK%'
GROUP BY ProductAlternateKey, CalendarYear;
Example 26.11
USE AdventureWorksDWMod;
GO
SELECT F.ProductKey, F.CurrencyKey, D1.CurrencyName, D2.EndDate
FROM dbo.FactInternetSales AS F
JOIN dbo.DimCurrency AS D1 ON F.CurrencyKey = D1.CurrencyKey
JOIN dbo.DimProduct D2 ON F.ProductKey = D2.ProductKey
WHERE D1.CurrencyKey <= 12 AND D2.ListPrice > 50
OPTION (MAXDOP 32);
Example 26.12
USE sample;
GO
CREATE VIEW v_enter_month
WITH SCHEMABINDING
AS SELECT emp_no, DATEPART(MONTH, enter_date) AS enter_month
FROM dbo.works_on;
Example 26.13
USE sample;
GO
CREATE UNIQUE CLUSTERED INDEX
c_workson_deptno ON v_enter_month (enter_month, emp_no);
Example 26.14
USE sample;
SELECT objectproperty(object_id('v_enter_month'), 'IsIndexable');
Example 26.15
SELECT sessionproperty ('QUOTED_IDENTIFIER');
Example 26.16
USE sample;
SELECT quoted_identifier, concat_null_yields_null, ansi_nulls, ansi_padding
FROM sys.dm_exec_sessions
WHERE session_id = @@spid;
Example 26.17
USE sample;
EXEC sp_spaceused 'v_enter_month';