我有 3 个表男孩女孩和关系,其中男孩和女孩 r 父表,其中 thr 主键是 boy_id 和 girl_id(两者都是 guid)。它们的映射是在关系表中完成的。其中每个映射集都是复合主键。
现在我想将所有这些表复制到另一个数据库(其中表名是男人女人和外遇,它们的结构类似于男孩、女孩和关系),表 ID 将转换为其他一些 guid。
我所做的是 - 我创建了一个 IDmapper 表,在做任何事情之前,我将旧的 guid 和他们各自的新 guid 存储在一个表中。然后我使用存储在 IDmapper 表中的新 guid 将数据从男孩复制到男人和女孩到女人。后来我使用以下查询(1)和(2)来形成事务表。
Boy,Man,Woman,Girl 表具有以下结构 -
(col1 varchar(50) not null,col2 varchar(50) not null,primary key(col1));
关系表、Affair 表和 IDMapper(temporary) 表的结构如下
(col1 varchar(50) not null,col2 varchar(50) not null,primary key(col1,col2));
以下分别是从 Boy(opt)、Girl(opt)、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 个表的映射信息的这种情况。
请解释哪个查询更好,为什么?