Getting Started with Adobe After Effects - Part 6: Motion Blur
A collection of quick technology learning tips from what people around you learn every day

Using Partition Switching you can move millions of rows from one table to another in less than a second

Aug 18 2011 12:25PM by Madhivanan   

Assume that there are two tables table1 and table2 which have same structures and table1 has 10 millions rows and you want to move all rows from table1 to table2. The quickest method that I have learnt is to use Partition Switching

alter table table1 switch to table2

The reason why it is fast is that data are not moved actually but SQL Server updates metadata about the location of the data

Read More..   [116 clicks]

Published under: SQL Server Tips ·  ·  ·  · 


Madhivanan
3 · 39% · 12441
19
 
2
 
21
 
0
Incorrect
 
0
Interesting
 
0
Forgotten



Submit

8  Comments  

  • Madhi,

    Will the data copy or move from table1 to table2???

    commented on Aug 18 2011 5:57AM
    Ramireddy
    2 · 41% · 12972
  • can you provide more details?

    commented on Aug 18 2011 6:34AM
    Yogesh Bhadauriya
    267 · 1% · 163
  • RR, it will move the data. Table1 will have no data.

    commented on Aug 18 2011 8:46AM
    Madhivanan
    3 · 39% · 12441
  • yrbyogi, what do you want to know more?

    commented on Aug 18 2011 8:49AM
    Madhivanan
    3 · 39% · 12441
  • Hi Madhivanan,

    Are you speaking about http://msdn.microsoft.com/en-us/library/ms191160.aspx ? If yes, i am not sure to have understood the 2nd note "During an ALTER TABLE…SWITCH operation, a schema-modify lock is acquired on both the source and target tables to make sure that no other connections reference the tables during the change" and the Caution note in the Table and indexes requirements part "Partition switching can introduce duplicate values in IDENTITY columns of the target table, and gaps in the values of IDENTITY columns in the source table. Use DBCC CHECKIDENT to check the identity values of your tables and correct the values if you want"

    I think that , with mypoor english, i have missed something especially for the IDENTITY columns ( why these gaps and duplicate values ? )

    commented on Aug 18 2011 1:43PM
    Patrick Lambin
    162 · 1% · 296
  • Hello Madhivanan,

    Yes, In this case , it swap the tablename ,structures and references only. Not move data actually.

    commented on Aug 19 2011 4:37AM
    Paresh Prajapati
    6 · 22% · 7054
  • Hi, Is it possible to switch data to another table on condition basis other than the entire data?

    commented on Nov 2 2011 12:06AM
    RKA
    91 · 2% · 598
  • Hai madhi,

    Nice tip..

    Its worked for me only one time. I just create a copy of one existing table, then executed the "alter table table1 switch to table2" query. After the query execution i run the selection query of that i newly created table. The result shows exactly the same recordset of first table. then i delete all the records from the second table and run again the switching query but i i don't have get any result. I recreate the second table and run the same query, it won't be worked.

    Whats the reason for that.?

    commented on Apr 12 2012 12:03AM
    nikhildas
    1493 · 0% · 13

Your Comment


Sign Up or Login to post a comment.

"Using Partition Switching you can move millions of rows from one table to another in less than a second" rated 5 out of 5 by 19 readers
Using Partition Switching you can move millions of rows from one table to another in less than a second , 5.0 out of 5 based on 19 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]