Partition switching fails with error 4947, level 16, State 1

Loading large volumes of data into a staging table and performing index build operation on the staging table can be much quicker than trying to load data directly into your main table.

In order to partition switch the schema for you staging table should match exactly the schema of your main table. 

If there are differences in the schemas you will probably come across this error.

Msg 4947, Level 16, State 1, Line 33
ALTER TABLE SWITCH statement failed. There is no identical index in source table

The error is caused by the partitioning column being automatically added onto the index on the main table when you create it on the partition schema and you not including the column on the index on the staging table.

So here's a quick example. The steps to setup the demo are:-

  • Create a database
  • Create the partition function and partition scheme (putting all filegroups to primary for ease of demo)
  • Create two schemas (the staging schema for the new data and the Sales schema which we load into)
  • Create the staging table on the primary filegroup
  • Create the sales table on the partition scheme, partitioning by DatePlaced.
  • Create a non-unique clustered index on each table with ID as the key
  • Insert a row of data into the staging table

View source
  1. USE master;
  2. IF EXISTS(SELECT * FROM sys.DATABASES WHERE name = 'SQLEganPartitioning')
  3. BEGIN
  4.     ALTER DATABASE [SQLEganPartitioning] SET Restricted_User WITH ROLLBACK Immediate;
  5.     DROP DATABASE [SQLEganPartitioning];
  6. END
  7. CREATE DATABASE SQLEganPartitioning;
  8. GO
  9. USE [SQLEganPartitioning];
  10. CREATE PARTITION FUNCTION pf_Orders(DATE)
  11. AS
  12.     RANGE RIGHT FOR VALUES ('2013-01-01', '2014-01-01', '2015-01-01');
  13. GO
  14. CREATE PARTITION SCHEME ps_Orders
  15. AS
  16.     PARTITION pf_Orders ALL TO ([PRIMARY])
  17. GO
  18. CREATE Schema [Sales] Authorization dbo;
  19. GO
  20. CREATE schema [Staging] Authorization dbo;
  21. GO
  22. CREATE TABLE [Staging].[Orders]
  23. (    ID INT NOT NULL,
  24.     DatePlaced DATE NOT NULL DEFAULT getdate(),
  25.     Quantity INT NOT NULL DEFAULT 1
  26.  ) ON [PRIMARY];
  27. CREATE TABLE [Sales].[Orders]
  28. (    ID INT NOT NULL,
  29.     DatePlaced DATE NOT NULL DEFAULT getdate(),
  30.     Quantity INT NOT NULL DEFAULT 1
  31.  ) ON [ps_Orders](DatePlaced);
  32. CREATE Clustered INDEX [pk_SalesOrders]  ON [Staging].[Orders] (ID) WITH(IGNORE_DUP_KEY=Off)
  33. CREATE Clustered INDEX [pk_SalesOrders]  ON [Sales].[Orders] (ID) WITH(IGNORE_DUP_KEY=Off)
  34. INSERT INTO Staging.Orders (ID,[DatePlaced] ) VALUES (1, '2012-06-09');
  35.  

Before we can switch from the staging to the main table we have to add a check constraint onto the table. This check constraint should validate the data in the staging table will fit into the destination tables partition. So looking at partition 1 in the partition functions definition we can see the data should be between '2012-01-01' And '2012-12-31'.

View source
  1. ALTER TABLE [Staging].[Orders] ADD CONSTRAINT [ck_OrderID] CHECK ([DatePlaced] BETWEEN '2012-01-01' AND '2012-12-31');

Now we can try and switch

View source
  1. ALTER TABLE [Staging].[Orders] Switch TO [Sales].[Orders] Partition 1;

When we run the above we get the error 4947.

 

If we look at the indexes on the tables using the code

View source
  1. SELECT *
  2. FROM sys.indexes
  3. WHERE object_id IN (object_id('Sales.Orders'), object_id('Staging.Orders'))
  4.  
  5.  
  6. SELECT schema_name(t.schema_id) + '.' + t.name, i.index_id, i.name,ic.index_column_id, c.name
  7. FROM sys.index_columns AS ic
  8.     INNER JOIN sys.COLUMNS AS c ON ic.column_id = c.column_id AND ic.object_id = c.object_id
  9.     INNER JOIN sys.indexes AS i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
  10.     INNER JOIN sys.TABLES AS t ON i.object_id = t.object_id
  11. WHERE i.object_id IN (object_id('Sales.Orders'), object_id('Staging.Orders'))

We can see both tables have the one index, but the columns are different. In the partitioned table the partitioning key has automatically been included. This is not visibile in the index properties in SSMS

 

So to fix the staging table we update the index to include the partitioning column and then we can switch in.

View source
  1. CREATE CLUSTERED INDEX [pk_SalesOrders] ON [Staging].[Orders]
  2. (
  3.     [ID] ASC,
  4.     [DatePlaced] ASC
  5. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = ON, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  6. GO
  7. ALTER TABLE [Staging].[Orders] Switch TO [Sales].[Orders] Partition 1
  8.  

 

 

Comments   

0 #1 Robert 2015-05-13 06:25
Hey Paul.

Thank you so much for this - I finally figured out why my indexes where not identical.

Your rock.

Regards
Quote

Add comment


Security code
Refresh

Joomla Templates: by JoomlaShack