MCDBA Brasil


  • Home
  • Sobre
  • Contato

Livros





Links Rápidos

SQL Server Builds (All Versions/Editions)


Download SQL Server 2017 (trial)


SQL Server 2017 Feature Pack


SQL Server 2016 Feature Pack


Cumulative Update SQL Server 2017 builds


Cumulative Update SQL Server 2016 builds


Cumulative Update SQL Server 2014 builds


Cumulative Update SQL Server 2012 builds


SQL Server 2005/2008 Samples Databases


Documentando o Servidor SQL Server


Analisando a Performance do Servidor-CheckList


Virtual PASS PT


Faça parte do maior virtual chapter do PASS com conteúdos técnicos em Português sobre SQL Server.

Todos os meses um evento Online para você! Acompanhe aqui os WebCasts já agendados

Sindicalize seu blog ou site ao VirtualPASSPT

SQL Server Blogs

SQL Server Query Processing Team


SQL Programmability & API Development Team


SQL Server Manageability Team


Latin America Support Team


Database + Disk + Performance


Microsoft SQL Server Support


SQL CLR Team


SQL Query Optimization Team


SQL 2005 Code Samples


SQL Server Express Team


SQL SMO Samples


SQL Storage Engine Team


SQL CAT Team


SQL Protocols Team


PSS SQL Server Engineers


Slava Oks on SQLOS


Ken Henderson’s blog


LUTI@Microsoft Blog


kimberly L. Trip’s blog


Fernando Garcia Blog

Dica da Semana

Movendo Tabelas Entre Filegroups

por Nilton Pinheiro outubro 14, 2011 Nenhum comentário

Pessoal, tudo se baseia em a gente conseguir criar um indice cluster no novo filegroup. Por que ?

O BOL diz : “Because the leaf level of a clustered index and its data pages are the same by definition, creating a clustered index and using the ON filegroup clause effectively moves a table from the file on which the table was created to the new filegroup. Before creating tables or indexes on specific filegroups, verify which filegroups are available and that they have enough empty space for the index. It is important that the filegroup has at least 1.2 times the space required for the entire table.”


Ou seja, por definição o nivel folha de um índice cluster e as suas paginas de dados são os mesmos. Então movendo o índice cluster estaremos movendo também a tabela. Pros nossos testes temos o filegroup Primary e estaremos passando pro filegroup NovoFilegroup


Vamos lá..


Caso 1 – Tenho uma tabela com campo Identity e PK clustered nele. Podemos fazer assim: o Alter Table With Move dropa a constraint e recria a tabela no novo filegroup sendo que depois temos que criar a constraint novamente


ALTER TABLE [Teste_1] DROP CONSTRAINT [PK_Teste_1]
with (move to NovoFilegroup)

ALTER TABLE [Teste_1] WITH NOCHECK ADD CONSTRAINT [PK_Teste_1]PRIMARY KEY NONCLUSTERED ([Codigo] ASC ) ON [NovoFilegroup]


OU dropar a constraint e recriá-la novo filegroup. Eu particularmente acho este método mais eficiente pois teremos que recriar a constrainst de qualquer maneira.

ALTER TABLE [Teste_1] DROP CONSTRAINT [PK_Teste_1]
ALTER TABLE [Teste_1] WITH NOCHECK ADD CONSTRAINT [PK_Teste_1] PRIMARY KEY NONCLUSTERED ([Codigo] ASC ) ON [NovoFilegroup]

Caso 2 – Tenho uma tabela com campo Identity sem PK ou Indice Cluster, ou seja uma heap. Criamos um indice cluster no campo identity apontando para o novo filegroup e depois dropamos este indice para a tabela voltar ao estado original, ou seja, voltar a ser uma heap. 


CREATE CLUSTERED INDEX [MeuIndiceCluster] ON [Teste_7]([codigo])
ON [NovoFilegroup]
DROP INDEX [Teste_7].MeuIndiceCluster


Caso 3 – Tenho uma tabela com Unique Index NONCLUSTERED e sem Identity. Adiciono um campo identity na tabela e depois crio um indice cluster sobre este campo apontando para o novo filegroup. Logo após dropamos o indice cluster e o campo identity, dropamos o Unique index ([IX_Teste_2]) e logo após o criamos no novo filegroup. Assim a tabela voltará ao estado original.


ALTER TABLE [Teste_2] ADD [MinhaColunaIdentity] BIGINT IDENTITY (1,1)

CREATE CLUSTERED INDEX MeuIndiceCluster ON [Teste_2] ([MinhaColunaIdentity])
ON [NovoFilegroup]
DROP INDEX [Teste_2].MeuIndiceCluster


ALTER TABLE [Teste_2] DROP COLUMN [MinhaColunaIdentity]
DROP INDEX [Teste_2].[IX_Teste_2]

CREATE UNIQUE NONCLUSTERED INDEX [IX_Teste_2] ON [Teste_2]([Codigo] ASC ) ON [NovoFilegroup]


Caso 4 – Tenho uma tabela com Unique Index NONCLUSTERED com Identity. Criamos um indice cluster neste campo identity apontando para o novo filegroup e dropamos este indice após finalizado. Dropamos o unique index ([IX_Teste_3]) e o recriamos no novo filegroup.


CREATE CLUSTERED INDEX MeuIndiceCluster ON [Teste_3]([Codigo])
ON [NovoFilegroup]
DROP INDEX [Teste_3].MeuIndiceCluster
DROP INDEX [Teste_3].[IX_Teste_3]


CREATE UNIQUE NONCLUSTERED INDEX [IX_Teste_3]ON [Teste_3]([Codigo] ASC ) ON [NovoFilegroup]


Caso 5 – Tenho uma tabela com Unique Index CLUSTERED e sem Identity. Dropamos este unique index ([IX_Tabela_4]) e recriamos ele apontando para o novo filegroup.


DROP INDEX [Teste_4].[IX_Tabela_4]
CREATE UNIQUE CLUSTERED INDEX [IX_Tabela_4] ON [Teste_4]([Codigo] ASC ) ON [NovoFilegroup]


Caso 6 – Tenho uma tabela com Unique Index CLUSTERED e com Identity. Dropamos este unique index ([IX_Teste_5]) e recriamos ele apontando para o novo filegroup.


DROP INDEX [Teste_5].[IX_Teste_5]
CREATE UNIQUE CLUSTERED INDEX [IX_Teste_5] ON [Teste_5]([Codigo] ASC ) ON [NovoFilegroup]


É isso galerinha, sempre lembrando que o novo filegroup precisará ter 1.2 vezes o tamanho original da tabela como espaço livre.

Abraços
laertejunior

Avaliação:
Compartilhe:
  • Anterior SQL University – Treinamento Online SQL SERVER Gratuito – SQLU11 anos atrás
  • Próximo Introdução ao Transaction Log11 anos atrás

Deixe uma resposta Cancelar resposta

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

MVP Reconnect Award

Categorias

  • Artigos (359)
  • Dica da Semana (95)
  • Documentação (54)
  • Downloads (113)
  • MSDE 2000 (3)
  • Sem categoria (1)
  • Tutoriais (9)

Posts recentes

  • #FechouBrasil #PartiuPortugal
  • Brigando com o erro “The cached MSI file is missing”
  • MCDBABRASIL está de volta
  • Documentando o Servidor SQL Server
  • Brigando com os Erros 17182, 17826 e 17120

SQL Server AlwaysOn Video Series

Video1: Introdução ao SQLServer2012 AlwaysOn


Video2: Introdução ao SQLServer2012 AlwaysOn Availability Group


Video3: Introdução ao SQLServer2012 AlwaysOn AVG-Demo


Video4: Introdução ao SQLServer2012 AlwaysOn Listener


Video5: Introdução ao SQLServer2012 AlwaysOn Readable Secondaries


Video6: Introdução ao SQLServer2012 AlwaysOn Readable Secondaries-Demo


Video7: Introdução ao SQLServer2012 AlwaysOn Failover Clustering


Serie SQL Server Failover Clustering End-to-End

Parte 1: Configuração da Rede e Ambiente


Parte 2: Configurando o Windows 2008 R2 Domain Controler e DNS


Parte 3: Preparando os nós para o Failover Cluster


Parte 4: Configurando um Failover Cluster de 2 nós


Parte 5: Configurando as LUNs no iSCSI Software Target (Parte 1)


Parte 6: Configurando as LUNs no iSCSI Software Target (Parte 2)


Parte 7: Apresentando as LUNs para os nós do Failover Cluster


Parte 8: Configurando os discos no Failover Cluster


Parte 9: Instalando a primeira instância virtual do SQL Server 2008


Parte 10: Instalando a segunda instância virtual do SQL Server 2008


Parte 11: Instalando e Configurando o MSDTC no Failover Cluster


Parte 12: Configurando Mount Points no Cluster e SQL Server 2008


Vídeo Extra: Removendo uma Instância do SQL Server 2008 R2 em Cluster


Alta Disponibilidade no SQL Server 2008 R2: Failover Clustering Overview


Alta Disponibilidade no SQL Server 2008 R2: Failover Clustering na Prática

Menu

  • Home
  • Sobre
  • Contato

Mais

  • RSS Feeds
2022 MCDBA Brasil.