CREATE STATISTICS
s_sales_customer_product
ON
Sales.Sales (CustomerID, ProductID)
WITH FULLSCAN;
UPDATE STATISTICS Sales;
CREATE TABLE Sales.OrderDetails
(
OrderDetailID INT PRIMARY KEY IDENTITY(1,1),
SaleYear INT NOT NULL,
OrderID INT NOT NULL,
ProductID INT NOT NULL,
Quantity INT NOT NULL,
Price DECIMAL(10, 2) NOT NULL
)
CREATE CLUSTERED INDEX CI_OrderDetails_SaleYear
ON Sales.OrderDetails(SaleYear);
DECLARE @i INT = 1;
WHILE @i <= 5 -- Предполагаем, что таблица секционирована по 5 годам
BEGIN
EXEC('UPDATE STATISTICS Sales.OrderDetails PARTITION(' + CAST(@i AS NVARCHAR(10)) + ') WITH RESAMPLE;');
SET @i = @i + 1;
END;
SELECT
DB_NAME(database_id) as DatabaseName,
object_name(object_id) as TableName,
index_id,
avg_fragmentation_in_percent
FROM
sys.dm_db_index_physical_stats(DB_ID(), NULL, -1, NULL, 'LIMITED')
WHERE
avg_fragmentation_in_percent > 0
ORDER BY avg_fragmentation_in_percent DESC;
SELECT
100.0*(ISNULL(sum(deleted_rows),0))/NULLIF(sum(total_rows),0) as frag,
o.name as table_name,
i.name as object_name,
st.partition_number as partition
FROM
sys.dm_db_column_store_row_group_physical_stats st
inner join sys.objects o
on o.object_id = st.object_id
inner join sys.indexes i
on i.object_id = o.object_id
inner join sys.partitions p
on p.object_id = o.object_id
and p.index_id = i.index_id
and p.partition_number = st.partition_number
GROUP BY
o.name,
i.name,
st.partition_number
HAVING
100.0*(ISNULL(sum(deleted_rows),0))/NULLIF(sum(total_rows),0) > 0
ALTER INDEX ix_cl_dim_stores_store_id ON dbo.dim_stores
REBUILD
ALTER TABLE dbo.dim_items
REBUILD;
ALTER INDEX ix_ncl_dim_items_item_source_id ON dbo.dim_items
REORGANIZE;
ALTER TABLE dbo.dim_items
REORGANIZE;
ALTER INDEX CI_OrderDetails_SaleYear
ON Sales.OrderDetails
REBUILD PARTITION = 4;
ALTER INDEX ix_cl_dim_stores_store_id ON dbo.dim_stores
REBUILD WITH (ONLINE = ON)
ALTER INDEX ix_cl_dim_stores_store_id ON dbo.dim_stores
REBUILD WITH (SORT_IN_TEMPDB = ON)