此查询需要花费大量时间来执行,如何优化它?

时间:2017-07-18 00:42:17

标签: sql database replace database-performance query-performance

我使用了替换来清理字段。但它需要小时才能执行。

select c.*  
from  
(  
select distinct a.*, b.*   
from  
(  
--Table 1  
select replace(replace(replace(replace(AGENCY_NAME,'',''),'',''),'/',''),'\\','')
as agency_name,       
LEN(replace(replace(replace(replace(AGENCY_NAME,' ',''),'-',''),'/',''),'\\',''))     
as agency_len  
from dbo.tbl_stars_agency   
where replace(replace(replace(replace(AGENCY_NAME,' ',''),'-',''),'/',''),'\\','')   
not in ('')  
) a     
inner join  
(  
--Table 2  
select replace(replace(replace(replace(RESPONDENT_NAME_PER,' ',''),'.',''),'/',''),'\\','')   
as respondent_name,  
len(replace(replace(replace(replace(RESPONDENT_NAME_PER,' ',''),'-',''),'/',''),'\\',''))   
as respondent_len  
from dbo.TBL_cacs_ecb   
where replace(replace(replace(replace(RESPONDENT_NAME_PER,' ',''),'-',''),'/',''),'\\','') not in ('')     
) b  
on substring(a.agency_name,1,15)=SUBSTRING(b.respondent_name,1,15)  
) c  
inner join   
(  
--Table 3
select replace(replace(replace(replace(NM_ENTITY,' ',''),'-',''),'/',''),'\\','')  
as nm_entity,  
LEN(replace(replace(replace(replace(NM_ENTITY,' ',''),'-',''),'/',''),'\\',''))   
as nm_entity_len  
from dbo.RMFS010_TF1NAME   
where replace(replace(replace(replace(NM_ENTITY,' ',''),'-',''),'/',''),'\\','')   
not in ('')  
) d  
on substring(c.agency_name,1,5)=substring(d.nm_entity,1,5) or 
substring(c.respondent_name,1,5)=substring(d.nm_entity,1,5)  

我想根据表格中的名称字段比较三个表格。我已经计算了长度并使用了子串函数来匹配多达15个位置。

1 个答案:

答案 0 :(得分:0)

一种方法是添加计算列并将其编入索引:

ALTER TABLE dbo.tbl_stars_agency
ADD agency_len AS LEN(replace(replace(replace(
replace(AGENCY_NAME,' ',''),'-',''),'/',''),'\',''))
PERSISTED;

现在添加一个索引:

CREATE INDEX MyIndex dbo.tbl_stars_agency
(agency_len);

现在任何搜索都应该快得多。

您可以更改此行:

where replace(replace(replace(
replace(AGENCY_NAME,' ',''),'-',''),'/',''),'\','')  
not in ('')  

到此:

where agency_len = 0

尝试一下,如果有帮助,我们可以看看对其他列进行此操作

PS:您需要确定您的表达是否是水密的,并且不会因长度无效等原因而崩溃。否则会阻止您插入和更新记录