我正在尝试从我们的数据库中清除超过 13 个月的任何数据的大量历史数据。
我已经为截断编写了一个存储过程,但是当我尝试运行它们时,我遇到了两个问题。
问题 1 - 当磁盘驱动器空间不足时,删除会填满数据库事务日志并使数据库崩溃。
问题 2 - 脚本本身需要很长时间才能执行。
以下是 SP 中的语句:
DELETE FROM Header_Table WHERE Date_DT < @date
该Header_Table
表与其他 10 个表有ON DELETE CASCADE
关系。这意味着当我触发上述查询时,它会从Header_Table
表以及 10 个表引用中删除数据Header_Table
在第二个语句中,我将表中的数据插入
Information_Table
到临时表中。INSERT INTO Temp_Table (Key, AccNum, Exp, Name_VC) SELECT in.Key AS CRD_NFO_CIK, in.Acct_Num AS CRD_NFO_ACC, in.Exp AS CRD_NFO_EXP, in.Name_on_Card_VC AS CRD_NFO_NAME FROM Information_Table in LEFT OUTER JOIN Card_T crd ON in.Key = crd.Card_Info_Key LEFT OUTER JOIN Business_T business ON in.Key = business.CC_Info_Key LEFT OUTER JOIN Con_T contr ON in.Key = contr.Card_Info_Key LEFT OUTER JOIN Customer_Payment_T customer ON in.Key = customer.Card_Info_Key LEFT OUTER JOIN Temp_Table Temp ON in.Key = Temp.Key WHERE Temp.Key IS NULL AND crd.Card_Info_Key IS NULL AND business.CC_Info_Key IS NULL AND contr.Card_Info_Key IS NULL AND customer.Card_Info_Key IS NULL
在下面的语句中,我实际上是从 Information_Table 中删除数据
DELETE info FROM Information_Table in LEFT OUTER JOIN Card_T crd ON in.Key = crd.Card_Info_Key LEFT OUTER JOIN Business_T business ON in.Key = business.CC_Info_Key LEFT OUTER JOIN Con_T contr ON in.Key = contr.Card_Info_Key LEFT OUTER JOIN Customer_Payment_T customer ON in.Key = customer.Card_Info_Key WHERE crd.Card_Info_Key IS NULL AND business.CC_Info_Key IS NULL AND contr.Card_Info_Key IS NULL AND customer.Card_Info_Key IS NULL