Tuesday, 4 October 2011

Very fast Clearing of a Table That has an Indexed View

The new feature allows very fast clearing of a table that has an indexed view on it by creating a staging table (call it T1_Stage) with the same structure as our T1 table, switching T1 to T1_Stage, and then dropping T1_Stage. All this can be done without dropping the view or even the index on it. Here’s the code to achieve the task:

CREATE TABLE dbo.T1_STAGE

(

  col1 INT NOT NULL PRIMARY KEY,

  col2 INT NOT NULL,

  col3 NUMERIC(12, 2) NOT NULL

);



ALTER TABLE dbo.T1 SWITCH TO dbo.T1_STAGE;



DROP TABLE dbo.T1_STAGE;

No comments:

Post a Comment