sqlschool.gr logo

articles

Articles of SQLschool.gr Team

Moving a table to another filegroup

Antonios Chatzipavlis
Monday 11 July 2016

Στο πρόσφατα ολοκληρωμένο σεμινάριο (20462) στο οποίο μιλάμε και για filegroups, έδειχνα στους μαθητές μου πως μπορούν αν εκμεταλλευτούν αυτά ώστε να επωφεληθούν από αυτά τόσο σε maintenance όσο και σε performance.

Αφού μέσα από τα δικά μου παραδείγματα που δεν υπάρχουν στο βιβλίο τους έχω βάλει το μικρόβιο να θέλουν σαν τρελοί να τα χρησιμοποιήσουν η αναμενόμενη επόμενη ερώτηση είναι πως μπορούν σε ήδη δημιουργημένες databases να κάνουν αυτό.

Η απάντηση πάντα από μένα στο μάθημα είναι μέσω παραδείγματος καθώς κάτι τέτοιο δεν το έχει το σεμινάριο. 

Σήμερα κατά μια διαβολεμένη σύμπτωση ένας καλός συνάδελφος από μια εξωτερική εταιρεία ήθελε να κάνει κάτι αντίστοιχο και επειδή μέχρι τώρα δεν το είχα γράψει σαν άρθρο θεώρησα ότι κάποια δύναμη θέλει να γίνει αυτό πραγματικότητα οπότε … 

Ας δούμε λοιπόν το παράδειγμα μας στο οποίο θα φτιάξουμε μια database με τα defaults και σε αυτή θα βάλουμε ένα πίνακα T στον οποίο θα βάλουμε 10000 records (STEP 1 στο script που ακολουθεί).

Προσέξετε ότι στο πίνακα έχω ένα primary key το οποίο είναι ένα constraint και όπως είναι γνωστό στο παρασκήνιο για να υλοποιηθεί δημιουργείται ένας clustered index εφόσον στο πίνακα δεν υπάρχει άλλος clustered index 

Επειδή δεν έχουμε ορίσει τίποτα σαν filegroup στην δημιουργία του πίνακα αυτός θα δημιουργηθεί στο default filegroup που δεν είναι άλλο από το PRIMARY. 

Αν τώρα με την sp_help (STEP 2 στο script που ακολουθεί) θα δούμε ότι αυτός όντως είναι στο primary filegroup.

Στην database (STEP 3 στο script που ακολουθεί) δημιουργούμε ένα filegroup με το όνομα SECONDARY και σε αυτό βάζουμε ένα data file.

To μόνο που πρέπει να κάνουμε είναι να μεταφέρουμε το πίνακα στο secondary filegroup (STEP 4 στο script που ακολουθεί).

Μπορεί να φαντάζει παράξενο ότι αυτό το κάνουμε με πάνω στο constraint του primary key αλλά για όσους γνωρίζουν στην ουσία τα leaf level του index είναι τα πραγματικά data pages. 

Μετά από το move θα πρέπει να φτιάξουμε ξανά το constraint.  

Αν εκτελέσουμε πάλι την sp_help θα δούμε ότι ο πίνακας πλέον είναι στο  secondary filegroup.

Θα πρέπει να σημειώσω ότι αν ο πίνακας είναι heap δηλαδή δεν έχει clustered index θα πρέπει να δημιουργηθεί πρώτα κάποιος και για την μεταφορά χρησιμοποιούμε την create index with drop existing για να κάνουμε την μεταφορά.

Στο παρακάτω παράδειγμα στο STEP 5 δείχνω το πως μπορεί να γίνει αυτό και σαν παράδειγμα έχω βάλει την col1.

/* STEP 1 */
use master;
create database DemoDB;
go

use demodb;
create table T 
(
    col1 int identity (1,1) not null ,
    col2 int,
    col3 int,
    col4 char(1000)
)
go

use demodb;
alter table T
add constraint PK_T primary key (col1)
go

use demodb;
insert into T (col2,col3,col4)
values (cast(RAND() * 100 as int),cast(RAND() * 1000 as int),REPLICATE('a',1000));
go 10000


/* STEP 2 */

use demodb;
sp_help T;
go


/* STEP 3 */

use master;
alter database DemoDB add filegroup SECONDARY;
go

USE [master]
GO
alter database [DemoDB] 
add file ( NAME = N'DemoDB_data2', FILENAME = N'D:\MSSQL13.MSSQLSERVER\MSSQL\DATA\DemoDB_data2.ndf' ) 
to filegroup [SECONDARY]
GO

/* STEP 4 */

use demodb;
alter table T
drop constraint PK_T with (move to secondary)
go


use demodb;
alter table T
add constraint PK_T primary key (col1)
go


use demodb;
exec sp_help T;
go


/* STEP 5 */
CREATE CLUSTERED INDEX CI
ON T(col1)
   WITH DROP_EXISTING
ON SECONDARY

Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios is a Data Solutions Consultant and Trainer. He has been working in IT since 1988. In his career, he has worked as senior developer, IT Manager, Solutions Architect and IT Consultant. Since 1995 he has been devoted on new technologies and software development tools, mainly by Microsoft, either by training company staff and colleagues or assisting them in design, development and implementation as a consultant or chief developer. He has focused in Databases and Data Science since 1995. He specialized in Microsoft SQL Server since version 6.0 in areas like SQL Server Internals, Database Design and Development, Business Intelligence and in 2010 he has started working with Azure Data Platform, NoSQL databases, Big Data Technologies and Machine Learning. He is an active member of many IT communities in Greece, answering colleagues' questions and writing articles in his web site. He is the owner of SQLschool.gr which is a community portal with a lot of information about Microsoft SQL Server. He has been a Microsoft Certified Trainer (MCT) since 2000. Microsoft honored him as MVP on Data Platform due to his activities in SQL Server since 2010. He holds a large number of Microsoft Certifications and Microsoft SQL Server Certifications since version 6.5.

Episode

Introduction to Microsoft Fabric

image

More Episodes...

Tip

Refresh Intellisence in SSMS

Για να κάνουμε refresh το intellisence μέσα στο SSMS αρκεί να πατήσουμε Ctrl+Shift+R

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-2023 All rights reserved

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