Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 SSIS and Import/Export (2008)
 Redirect production requests whilst bulk inserting

Author  Topic 

Steve616
Starting Member

1 Post

Posted - 2014-05-30 : 03:34:59
Hi all, I have a need to bulk load some data from a set of files which I am doing through a set of staging tables. However at the next stage whilst I then bulk load these tables into the production database (they are read/reference only) I need to redirect live requests to the staging tables so that the site is not down whilst bulk loading. Once complete and row counts have been checked, I then want to move the live requests back to the product table again.

I've been ploughing through documentation on partitioning and synonyms, but does anyone have any advise as to the best way to achieve this. Ideally it would be great if there was a set of table aliases that the website was using which allowed me to redirect the actual data source underneath from SQL.

I think synonyms is the way but I know you have to drop and recreate a synonym which granted is only milliseconds to complete relative to the bulk load but I just wondered whether there was someone out there that can give me a steer please?
Thanks in advance.

robvolk
Most Valuable Yak

15732 Posts

Posted - 2014-05-30 : 16:52:12
If you're doing a full data replacement then the synonym method is pretty straightforward:

-- create original source data
create table one(a int not null);
insert one values(1);

-- synonym for source
create synonym source for one;

-- only select from synonym
select * from source;

-- load staging
create table stage(a int not null);
insert stage values(2);

-- swap sources via synonym
begin try
begin tran;
drop synonym source;
create synonym source for stage;
commit tran;
end try
begin catch;
rollback tran;
raiserror('Synonym swap failed',16,1)
end catch

select * from source;

-- clean up
drop synonym source;
drop table one,stage;
If you're doing something else that's more involved you'll need to post details.

Alternately you can do something similar using different schemas, then transfer/swap the objects:
-- schema swap
GO
-- create schema for staging
create schema stage authorization dbo;
GO
-- create holding schema for swap
create schema holding authorization dbo;
GO
--create tables and populate
create table dbo.source(a int not null)
create table stage.source(a int not null)
insert dbo.source values(1);
insert stage.source values(2);

-- make sure no holding table exists
if OBJECT_ID('holding.source') is not null drop table holding.source;

-- always select from dbo
select * from dbo.source;

-- swap data (dbo->holding, stage->dbo, holding->stage)
begin try
begin tran;
alter schema holding transfer dbo.source;
alter schema dbo transfer stage.source
alter schema stage transfer holding.source;
commit tran;
end try
begin catch;
rollback tran;
raiserror('Schema swap failed',16,1)
end catch

select * from dbo.source;

-- clean up
drop table stage.source, dbo.source;
if OBJECT_ID('holding.source') is not null drop table holding.source;
We use this at my current job, at the time we weren't using Enterprise Edition and needed to have the same effect as partition switching. This was the best way to manage while also keeping previous versions available, although the synonym method can do the same.

You can read more about it here:

http://sqlperformance.com/2012/08/t-sql-queries/t-sql-tuesday-schema-switch-a-roo
http://sqlperformance.com/2013/04/t-sql-queries/schema-switch-a-roo-part-2
Go to Top of Page
   

- Advertisement -