2014-02-23 123 views
0

此查詢導致我們的事務日誌增長到25GB。數據庫處於SIMPLE模式。限制查詢大小以控制事務日誌大小

INSERT INTO updbl.dbo.PopulationRelatives 
    (personid, 
     personsex, 
     relativeid, 
     relativesex, 
     degree, 
     relationship, 
     maternalpaternal) 
    SELECT DISTINCT 
      personid = relative1, 
      relative1sex, 
      relative2, 
      relative2sex, 
      degree, 
      relationship = Rel1Rel2, 
      maternalpaternal 
    FROM UPDBwork.dbo.DegreeRelationship 

通過循環我可以限制增長到8GB。

SELECT @PID = 0, @BatchSize = 1000000, @ROWCOUNT = 0 
SELECT @MaxPID = MAX(relative1) FROM updbwork.dbo.DegreeRelationship 
WHILE @PID < @[email protected] 
BEGIN 
INSERT INTO updbl.dbo.PopulationRelatives 
     (personid, 
      personsex, 
      relativeid, 
      relativesex, 
      degree, 
      relationship, 
      maternalpaternal) 
     SELECT DISTINCT 
       personid = relative1, 
       relative1sex, 
       relative2, 
       relative2sex, 
       degree, 
       relationship = Rel1Rel2, 
       maternalpaternal 
     FROM UPDBwork.dbo.DegreeRelationship 
     WHERE relative1 BETWEEN @PID+1 AND @[email protected] 
     SET @PID = @PID + @BatchSize 
CHECKPOINT 
END 

這不是最好的策略,因爲每個循環根據DISTINCT值產生不同數量的行。不幸的是,沒有好的ID來分區數據。有什麼方法可以控制每個組的大小?我正在考慮添加TOP(X),但引擎仍然需要進行大量計算才能滿足DISTINCT語句。遊標會很好,但是又如何找到我的DISTINCT值?我只是希望在這裏頭腦風暴。 謝謝。

回答