简体   繁体   中英

Table Partitioning in Sql server 2012 sql Azure

I have done partition on table on eventdate field from jan 2014 to dec 2020 for every two months. Here is my whole query for Table Partitioning :- Creating Parttioning Function

DECLARE @DatePartitionFunction nvarchar(max) = N'CREATE PARTITION FUNCTION EventDatePartition (date) AS RANGE RIGHT FOR VALUES (';  
DECLARE @i datetime2 = '20140101';  
WHILE @i < '20201231'  
BEGIN  
SET @DatePartitionFunction += '''' + CAST(@i as nvarchar(10)) + '''' + N', ';  
SET @i = DATEADD(MM, 2, @i);  
END  
SET @DatePartitionFunction += '''' + CAST(@i as nvarchar(10))+ '''' + N');';  
EXEC sp_executesql @DatePartitionFunction;  
GO 

Create Partition Schema :-

create drop  partition scheme EventDatePartitioning as partition EventDatePartition ALL TO ([PRIMARY]);

Finally creating clustered index on partition schema:-

CREATE CLUSTERED INDEX ClickStreamData_Eventdate_Partition  ON ClickStreamData (Eventdate)
  WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
        ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
  ON EventDatePartitioning(Eventdate)
GO

After that I checked no of partition made by the following query :-

SELECT t.name AS TableName, i.name AS IndexName, p.partition_number, p.partition_id, i.data_space_id, f.function_id, f.type_desc, r.boundary_id, r.value AS BoundaryValue   
FROM sys.tables AS t  
JOIN sys.indexes AS i  
    ON t.object_id = i.object_id  
JOIN sys.partitions AS p  
    ON i.object_id = p.object_id AND i.index_id = p.index_id   
JOIN  sys.partition_schemes AS s   
    ON i.data_space_id = s.data_space_id  
JOIN sys.partition_functions AS f   
    ON s.function_id = f.function_id  
LEFT JOIN sys.partition_range_values AS r   
    ON f.function_id = r.function_id and r.boundary_id = p.partition_number  
WHERE t.name = 'ClickStreamData' AND i.type <= 1  
ORDER BY p.partition_number;

This shows 44 partition made with boundary values. Last partition ie 44th has boundary values 2021-01-01.

Now when I checked the no of rows associated with partition no with the following query :-

SELECT * FROM sys.dm_db_partition_stats   
WHERE object_id = OBJECT_ID('ClickStreamData');  

Now the result that this query is giving to me has 6 extra partition after 44th partition with total no of rows of table associated with it and all of these 6 partition has partition no 1.

Please help me out why it is showing extra six partitions with total rows of table associated with each partition.

I will roll my 8 ball and make a shot in the dark: you have non-aligned non-partitioned non-clustered indexes on the object ClickStreamData . Non-partitioned indexes count as one partition :

If a heap or index is not partitioned, it is made up of one partition (with partition number = 1);

Mandatory warning about non-aligned indexes Special Guidelines for Partitioned Indexes :

Memory limitations can affect the performance or ability of SQL Server to build a partitioned index. This is especially the case when the index is not aligned with its base table or is not aligned with its clustered index, if the table already has a clustered index applied to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM