sqlschool.gr logo

articles

Articles of SQLschool.gr Team

How to truncate and save truncated data simultaneously

Antonios Chatzipavlis
Sunday 25 February 2018

Δεν είναι λίγες οι φορές που χρειάζεται να σβήσεις όλα τα δεδομένα από ένα table αλλά πριν το κάνεις αυτό χρειάζεται να τα φυλάξεις κάπου αλλού για διάφορους λόγους.

Μια τέτοια εργασία ειδικά σε μεγάλα table απαιτεί χρόνο και πολλά locks καθώς θα πρέπει να κάνεις την μεταφορά με ένα SELECT INTO FROM statement και μετά να κάνεις το TRUNCATE.

Υπάρχει και άλλος όμως τρόπος αρκετά γρηγορότερος και σαφώς με λιγότερα locks και αυτός δεν είναι άλλος από SWITCH partitions καθώς όλα τα tables έχουν ένα partition.

To πως μπορείτε να το κάνετε αυτό;

Αρχικά ας φτιάξουμε ένα table (S) που θα παίξει το ρόλο του table που θέλουμε να διαγράψουμε τα δεδομένα του οποίος έχει ένα clustered και ένα nonclustered index και να το γεμίζουμε με 1.000.000 γραμμές. Να τονίζω ότι έτσι όπως έχω δημιουργήσει το table κάθε γραμμή είναι ένα data page.

create database TruncateSaveDemo;
go

use TruncateSaveDemo;
go

create table S 
(
    col1 int identity(1,1) not null,
    col2 int not null ,
    col3 char(8000) not null
);
go

create clustered index cidx on S(col1);
go

create index idx on S(col2);
go

set nocount on;
declare @i int = 0
while (@i<=1000000)
begin
    set @i+=1
    insert into S(col2,col3) values (@i,REPLICATE('a',8000))
end
go

Αυτό τον πίνακα θέλουμε να κάνουμε truncate αλλά ταυτόχρονα να κρατήσουμε τα δεδομένα του.

Αυτό που χρειάζεται να κάνουμε είναι να φτιάξουμε ένα αντίγραφο του (table T) το οποίο θα πρέπει να είναι στο ίδιο filegroup με τον αρχικό και να έχει ακριβώς τους ίδιους indexes.

create table T
(
    col1 int identity(1,1) not null,
    col2 int not null ,
    col3 char(8000) not null
);
go

create clustered index cidx on T(col1);
go

create index idx on T(col2);
go

Για να κάνουμε την διαγραφή και μεταφορά απλά χρειάζεται να κάνουμε SWITCH partition με την παρακάτω εντολή η οποία διαρκεί milliseconds αρκεί να μην έχει κάποιος σε χρήση τον πίνακα S.

alter table S switch to T;
go

Αυτό ήταν έχουμε διαγράψει τα δεδομένα μας από τον S και αυτά είναι πλέον στο T


//antonch


Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios Chatzipavlis is a highly experienced Data Solutions Consultant and Trainer. He has been working in the IT industry since 1988, holding various roles such as senior developer, IT Manager, Data & AI Solutions Architect and Consultant.

Since 1995, Antonios has focused on modern technologies and software development tools, primarily by Microsoft. He has specialized in Data & AI since 2000, with expertise in Microsoft Data Platform (SQL Server, Azure SQL Databases, Azure Synapse Analytics, Microsoft Fabric, Power BI, AI) and Databricks.

Antonios is also a Microsoft Certified Trainer (MCT) for over 25 years, has been recognized as a Microsoft Most Valuable Professional (MVP) in Data Platform since 2010 and he is in the Data Expert 40 Powerlist 2024 by Boussias. He is the co-founder and visionary behind XLYTiCA, a company dedicated to Data & AI solutions.

Episode

Task Flows in Microsoft Fabric

image

More Episodes...

Tip

Get Certified: Become a Fabric Data Engineer

More Tips...

Become a member

If you want to receive updates from us become a member to our community.

Connect

Explore

Learn


sqlschool.gr © 2010-2025 All rights reserved

This site uses cookies for operational and analytics purposes only. By continuing to browse this site, you agree to their use.