-- Materializing R1 s R2 DROP TABLE ResultR1sR2 CREATE TABLE [dbo].[ResultR1sR2] ( [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 ResultR1sR2(tid1,tid2,sim,S) SELECT r1w.tid AS tid1, r2s.tid AS tid2, SUM( r1w.weight * r2sum.total * r2s.c)/@S AS sim, @S AS S FROM R1weights r1w, R2sample r2s, R2sum r2sum WHERE r1w.token=r2s.token AND r1w.token=r2sum.token AND r2s.s = @S GROUP BY r1w.tid, r2s.tid COMMIT TRANSACTION CHECKPOINT DBCC SHRINKDATABASE(ICDEQ2) DBCC SHRINKDATABASE(ICDEQ3) SET @I = @I+1 END GO