Chapter28 Examples
Chapter28 Examples
1
ALTER DATABASE SCOPED CONFIGURATION SET
BATCH_MODE_MEMORY_GRANT_FEEDBACK = ON;
Example 28.2
USE AdventureWorks;
SELECT product.Name, COUNT(history.ProductID) AS Cnt,
SUM(history.Quantity) AS Sum,
AVG(history.ActualCost) AS Avg
FROM Production.TransactionHistory AS history
JOIN Production.Product AS product
ON product.ProductID = history.ProductID
GROUP BY history.ProductID, product.Name;
Example 28.3
USE AdventureWorks;
CREATE NONCLUSTERED COLUMNSTORE INDEX csi_history
ON Production.TransactionHistory(ProductID,Quantity,ActualCost);
Example 28.4
USE AdventureWorks;
GO
CREATE FUNCTION GetLastShipped()
RETURNS @CustomerOrder TABLE
(SaleOrderID INT NOT NULL,
CustomerID INT NOT NULL,
OrderDate DATETIME NOT NULL,
OrderQty INT NOT NULL)
AS
BEGIN
INSERT @CustomerOrder
SELECT a.SalesOrderID, a.CustomerID, a.OrderDate, b.OrderQty
FROM Sales.SalesOrderHeader a
INNER JOIN Sales.SalesOrderDetail b
ON a.SalesOrderID = b.SalesOrderID
INNER JOIN Production.Product c
ON b.ProductID = c.ProductID
WHERE a.OrderDate = ( Select Max(SH1.OrderDate)
FROM Sales.SalesOrderHeader As SH1
WHERE SH1.CustomerID = A.CustomerId)
RETURN
END
Example 28.5
USE AdventureWorks;
SELECT C = COUNT_BIG(*)
FROM GetLastShipped() C
Example 28.6
ALTER DATABASE SCOPED CONFIGURATION SET
INTERLEAVED_EXECUTION_TV = ON;
Example 28.7
EXEC sp_configure 'show advanced options', 1;
GO
RECONFIGURE WITH OVERRIDE;
GO
EXEC sp_configure 'max degree of parallelism', 4;
GO
RECONFIGURE WITH OVERRIDE;
GO
Example 28.8
USE sample;
CREATE NONCLUSTERED INDEX nCLI_IFactSales
ON dbo.FactInternetSales
(OrderDateKey, CustomerKey, SalesAmount) ;
Example 28.9
USE sample;
SELECT c.CommuteDistance,
d.CalendarYear,
SUM(f.SalesAmount) TotalSalesByCommuteDistance
FROM dbo.FactInternetSales as f
INNER JOIN dbo.DimCustomer as c ON
f.CustomerKey = c.CustomerKey
INNER JOIN dbo.DimDate d ON
d.DateKey = f.OrderDateKey
GROUP BY c.CommuteDistance, d.CalendarYear;
Example 28.10
USE AdventureWorksDW;
SET STATISTICS TIME ON;
SELECT count(DISTINCT(SalesOrderNumber))
FROM FactInternetSales;
SELECT APPROX_COUNT_DISTINCT(SalesOrderNumber)
FROM FactInternetSales;
Example 28.11
Example 28.12