我寫了腳本來爲表中的值做一些修復。但它需要永遠完成..實際上從來沒有看到它完成..只是掛在那裏..任何一點如何優化,將不勝感激。如何讓這個SQL腳本運行得更快?
有兩個表格:FundCorrespondencePreference
其中可以有一些在記錄中檢查的IDContacts
列。如果他們被選中,那麼相應的記錄應該存在於ContentManagementRights
表中(除了一些字段外,幾乎相同的字段)。
的想法是要經過ContentManagemtnRights
並找到所有結合 4列獨特的記錄,挑了一些列的值,並從FundCorrespondencePreference table
收集所有的用戶,然後檢查,如果該用戶是集合中籤, 去回到ContentManagementRights
表並檢查是否存在這樣的記錄,如果不存在,則在列中插入具有相應值的記錄。
希望我或多或少地一致解釋。任何方式,感謝您的時間和建議。這裏去腳本:
--the script will check if any of the contacts for a partner in FundCorrespondencePreferences are missing in ContentManagementRight table
-- some of the contacts in FundCorrespondencePreference just not appear in ContentManagementRight as checked
declare @IDFundInfo char(30)
declare @IDCompany char(30)
declare @IDFundTransactionParameter char(30)
declare @IDContentManagement char(30)
declare @IDUserInfo char(30)
declare @ID char(30)
declare @curContentManagementRights cursor
set @curContentManagementRights = cursor fast_forward for
--first select distinct records from ContentManagementRights for IDFundInfo, IDCompany,IDFundTransactionParameter and IDContentManagement where
--bFundLevelAccess == 0
SELECT distinct
ContentManagementRights.IDFundInfo,
ContentManagementRights.IDCompany,
ContentManagementRights.IDFundTransactionParameter,
ContentManagementRights.IDContentManagement
FROM ContentManagementRights
WHERE
(ContentManagementRights.bFundLevelAccess = 0)
ORDER BY
idfundinfo, idcompany,idfundtransactionparameter, idcontentmanagement
open @curContentManagementRights;
fetch from @curContentManagementRights into @IDFundInfo, @IDCompany,@IDFundTransactionParameter,@IDContentManagement
WHKLE @@fetch_status = 0
BEGIN
--get all IDContactInfo for the chosen fund, partnerid,fundtranactionparameterid from fundcorrespondencepreference table + make sure that at least one correspTO parameter
declare @curFundCorrespondencePreference cursor
set @curFundCorrespondencePreference = cursor fast_forward for
SELECT
UserInfo.IDUserInfo
FROM
FundCorrespondencePreference
INNER JOIN
FundTransactionParameter ON FundCorrespondencePreference.IDFundTransactionParameter = FundTransactionParameter.IDFundTransactionParameter
INNER JOIN
ContactInfo ON FundCorrespondencePreference.IDContactInfo = ContactInfo.IDContactInfo
INNER JOIN
UserInfo ON ContactInfo.IDContactInfo = UserInfo.IDContactInfo
INNER JOIN
UserGroupInfo ON UserInfo.IDUserInfo = UserGroupInfo.IDUserInfo
INNER JOIN
GroupInfo ON UserGroupInfo.IDGroupInfo = GroupInfo.IDGroupInfo
WHERE
(FundTransactionParameter.IDFundInfo = @IDFundInfo)
AND (FundCorrespondencePreference.IDCompany = @IDCompany)
AND (FundCorrespondencePreference.IDFundTransactionParameter = @IDFundTransactionParameter)
AND (FundCorrespondencePreference.bEmail = 1)
AND (GroupInfo.sName = N'Xtranet Partner Group')
OR (FundCorrespondencePreference.bFax = 1)
OR (FundCorrespondencePreference.bLetterTo = 1)
OR (FundCorrespondencePreference.bLetterCc = 1)
OR (FundCorrespondencePreference.bEmailCc = 1)
OR (FundCorrespondencePreference.bFaxCc = 1)
--go through all the contacts chosen from FundCorrespondencePreference table and
--if not present in ContentManagementRight table, insert into the table record with previously selected parameters
open @curFundCorrespondencePreference;
fetch from @curFundCorrespondencePreference into @IDUserInfo
WHILE @@fetch_status = 0
BEGIN
if
(Select Count(IDContentManagementRights)
from ContentManagementRights
where
IDContentManagement = @IDContentManagement
and IDFundInfo = @IDFundInfo
and IDCompany = @IDCompany
and IDUserInfo = @IDUserInfo) = 0
begin
--insert a new record in ContentManagementRights if there is none for chosen parameters
Exec GetNextID 'ContentManagementRights', @ID output
INSERT INTO ContentManagementRights (IDContentManagementRights,IDContentManagement,IDFundInfo,IDCompany,sCreatedBy,dtCreatedDate,sUpdatedBy,dtUpdatedDate,IDUserInfo,IDFundTransactionParameter,bFundLevelAccess)
VALUES (@ID,@IDContentManagement,@IDFundInfo,@IDCompany,'admin',GETDATE(),'admin',GETDATE(),@IDUserInfo,@IDFundTransactionParameter,0)
end
end
close @curFundCorrespondencePreference;
deallocate @curFundCorrespondencePreference;
--
end
close @curContentManagementRights;
deallocate @curContentManagementRights;
那麼,用**兩個嵌套遊標**,這是難怪它需要永遠....我的建議是這樣的:嘗試創建一個單一的查詢,與SELECT,JOIN等,給你的名單你想插入的行。不要以程序思考 - 想想成組的行,想想數據之間的關係。如果你可以創建它,它會快得多 – 2011-03-04 06:03:08
@marc_s我同意..將嘗試創建一個巨大的:) .. sql語句.. – HotFrost 2011-03-04 16:05:10
我明白你爲什麼使用遊標。你有一個「Sequence」表,你使用GetNextID proc來獲得ID。如果您可以發佈GetNextID proc的代碼,我可能會向您展示解決單個ID返回問題的方法。我也會仔細檢查你的代碼,因爲這樣的過程通常是一個巨大的死鎖源。 – 2011-03-07 03:58:34