-- Materializing R2 s R1 DROP TABLE ResultR2sR1 CREATE TABLE [dbo].[ResultR2sR1] ( [tid1] int NOT NULL, [tid2] int NOT NULL, [sim] float NOT NULL, [S] int NOT NULL, PRIMARY KEY (S,tid1,tid2), FOREIGN KEY (tid1) REFERENCES R1, FOREIGN KEY (tid2) REFERENCES R2, ) GO DECLARE @S int DECLARE @I int DECLARE @UPPERLIMIT int -- The upper limit in the sample size will be 2^@UPPERLIMIT SET @I=0 SET @UPPERLIMIT=8 WHILE @I <= @UPPERLIMIT BEGIN SET @S=POWER(2, @I) BEGIN TRANSACTION INSERT INTO ResultR2sR1(tid1,tid2,sim,S) SELECT r1s.tid AS tid1, r2w.tid AS tid2, SUM( r2w.weight * r1sum.total * r1s.c)/@S AS sim, @S AS S FROM R2weights r2w, R1sample r1s, R1sum r1sum WHERE r2w.token=r1s.token AND r2w.token=r1sum.token AND r1s.s = @S GROUP BY r1s.tid , r2w.tid COMMIT TRANSACTION CHECKPOINT DBCC SHRINKDATABASE(ICDEQ2) DBCC SHRINKDATABASE(ICDEQ3) SET @I = @I+1 END GO