父表及其映射表使用新guid复制数据库

时间:2015-03-24 08:12:37

标签: sql sql-server database time-complexity insert-select

我有3个表男孩女孩和关系,其中男孩和女孩r父表,其中thr主键是boy_id和girl_id(都是guids)。他们的映射是在关系表中完成的。其中每个映射集都是复合主键。

现在我想将所有这些表复制到另一个数据库(其中表名是man woman and affair,它们的结构类似于男孩,女孩和关系),table id将被转换为其他一些guid。

我所做的是 - 我创建了一个IDmapper表,在做任何事情之前,我将旧的guid和各自的新guid存储在一个表中。然后我用他们的新guid将数据从男孩复制到男人和女孩,再存储在IDmapper表中。后来我用下面的查询(1)和(2)来形成事件表。

男孩,男人,女人,女孩表具有以下结构 -

 (col1 varchar(50) not null,col2 varchar(50) not null,primary key(col1));

Relationship,Affair和IDMapper(临时)表具有以下结构

(col1 varchar(50) not null,col2 varchar(50) not null,primary key(col1,col2));

以下是分别从男孩(选择),女孩(选择),Realtionship(jon)到Man(opt),Woman(opt),Affair(jon)的复制查询

Insert into IDMapper Select Boy_ID,Boy_ID+'XY' from Boy; --Generate new guid and store mapping into IDMApper
Insert into Man Select New_ID,BoyName from Boy join IDMapper on Old_ID=Boy_ID; -- Copy table data

Insert into IDMapper Select Girl_ID,Girl_ID+'XX' from Girl; --Generate new guid and store mapping into IDMApper
Insert into Woman Select Old_ID,GirlName from Girl join IDMapper on Old_ID=Girl_ID; -- Copy table data

(1)

Insert into Affair Select M.New_ID,F.New_ID from Relationship
  join IDMapper M 
     on Boy_ID=M.OLD_ID 
  join IDMapper F 
     on Girl_ID=F.Old_ID; 

(2)

Insert into Affair Select 
   (Select New_ID from IDMapper where Old_ID=R.Boy_ID),
   (Select New_ID from IDMapper where Old_ID=R.GIRL_ID) from Relationship R;  

在查询(1)和(2)中,哪一个更快,它们的时间复杂度是多少?

我必须假设关系表将保存3-4个表的映射信息。

请解释哪个查询更好,为什么?

0 个答案:

没有答案
相关问题