我創建了一個存儲過程。減少SQL存儲過程中的比較次數
以下是要求:
- 首場比賽是在公司名稱和人名做。
- 如果找不到匹配,則應在地址,城市和人名中進行第二次匹配。
- 如果找不到匹配,則應在zip和個人名稱上進行第三次匹配。
我寫的是什麼
exec('insert into ProcessedFile_'[email protected] +' ('[email protected]+ ',UploadedB2bFiled_id) select '[email protected] +',B2bFiled_id from UploadedFile_'[email protected]+' a , b2bdb b where ((a.CompanyDomain = b.domain and ISNULL(a.CompanyDomain,'''') <> '''' and a.CompanyDomain is not null and a.Name=b.Name)) group by B2bFiled_id,' + @concatAppendFieldForGroupBy)
exec('insert into ProcessedFile_'[email protected] +' ('[email protected]+ ',UploadedB2bFiled_id) select '[email protected] +',B2bFiled_id from UploadedFile_'[email protected]+' a , b2bdb b where (((a.CompanyDomain is null or a.CompanyDomain !=b.domain) and a.Address1 = b.address and a.City = b.city and a.Name = b.Name)) group by B2bFiled_id,' + @concatAppendFieldForGroupBy)
exec('insert into ProcessedFile_'[email protected] +' ('[email protected]+ ',UploadedB2bFiled_id) select '[email protected] +',B2bFiled_id from UploadedFile_'[email protected]+' a , b2bdb b where ((((a.CompanyDomain is null or a.CompanyDomain !=b.domain) and (a.Address1 = b.address and a.City = b.city and a.Name = b.Name)) and a.Zip = b.Zip and a.Name = b.Name)) group by B2bFiled_id,' + @concatAppendFieldForGroupBy)`
但是,這是絕對的低效率,在每個語句(除了第一個),我比較所有的字段我在之前做了第一個我比較公司名稱和人名和第二個陳述我再次比較這些領域(公司名稱和人名)。
如何擺脫這個?
如果您不強迫我們工作,只是爲了能夠閱讀您的問題並(可能)幫助您,您會得到更多回復。例如。在句子的開頭大寫,並格式化SQL查詢以便讀取。 – 2012-04-05 07:42:53
@AlexInParis對不起 – 2012-04-05 07:46:10