Comment purger une table avec un milliard d'enregistrements qui a une clé étrangère à une autre table? Si une partition de la table et de la table enfant et utilisez un échange de partition?
Essaye ça:
declare @CurDate datetime select * into NewTable from ExistingTable where datecol >= @CurDate and datecol <= dateadd(month,3,@CurDate)
Ma suggestion
1) Enregistrez le script Créer de toutes les clés étrangères impliquées
SELECT distinct 'alter table '+rcu.TABLE_SCHEMA+'.'+rcu.TABLE_NAME+' add constraint '+rc.CONSTRAINT_NAME+' foreign key ('+rcu.COLUMN_NAME+') references '+rcu1.TABLE_SCHEMA+'.'+rcu1.TABLE_NAME+'('+rcu1.COLUMN_NAME+')'
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE rcu ON rc.CONSTRAINT_CATALOG = rcu.CONSTRAINT_CATALOG AND rc.CONSTRAINT_NAME = rcu.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE rcu1 ON rc.UNIQUE_CONSTRAINT_CATALOG = rcu1.CONSTRAINT_CATALOG AND rc.UNIQUE_CONSTRAINT_NAME = rcu1.CONSTRAINT_NAME
where rcu.TABLE_NAME='YOUR_TABLE'
2) abandonné
SELECT distinct 'alter table '+rcu.TABLE_SCHEMA+'.'+rcu.TABLE_NAME+' drop constraint '+rc.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE rcu ON rc.CONSTRAINT_CATALOG = rcu.CONSTRAINT_CATALOG AND rc.CONSTRAINT_NAME = rcu.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE rcu1 ON rc.UNIQUE_CONSTRAINT_CATALOG = rcu1.CONSTRAINT_CATALOG AND rc.UNIQUE_CONSTRAINT_NAME = rcu1.CONSTRAINT_NAME
where rcu.TABLE_NAME='YOUR_TABLE'
3) exécuter la suppression comme celle-ci pour éviter la croissance de la journal
declare @var bigint, @var2 bigint, @var3 bigint
set @var = (select count_big(1) from [Your_Database]..[YOUR_TABLE](nolock) --where filter if it need
set @var2 = 0
set @var3 = 1
while @var2 <> @var
begin
set rowcount 500000
delete from [Your_Database]..[YOUR_TABLE] --where filter if it need
set @var2 = @var2+@@ROWCOUNT
set @var3 = @var3+1
end
4) Enfin recréer toutes les clés Foriegn
Rappelez-vous simplement de lancer RowCount 0 après Star d'un nouveau script dans la même session