2009-07-31 5 views
0

Le nombre total inscrits dans le tableau gym_membercommon sont 40352.Une requête qui prend beaucoup de temps pour exécuter près de 2 minutes 10 secondes S'il vous plaît me aider à faire cette requête rapide

Le nombre total de dossiers pour le locataire 3 est 10250

Dans la table gym_membercommon, je dois trouver tous les enregistrements en double qui ont un nombre commun parmi ce locataire.

create table #temp    
(   
    meco_Commonid int,    
    meco_tenantid int,    
    meco_OfficeTelno varchar(30),    
    meco_HomeNo varchar(20),    
    meco_MobileNo varchar(20),    
    meco_Fax varchar(20) 
) 

CREATE CLUSTERED INDEX idxCL_TEMP ON #temp(meco_Commonid) 

CREATE NONCLUSTERED INDEX idxNC_TEMP ON #temp(meco_OfficeTelno,meco_HomeNo,meco_MobileNo,meco_Fax) 

insert into #temp    
select 
    meco_Commonid, 
    meco_tenantid,    
    meco_OfficeTelno,    
    meco_HomeNo,    
    meco_MobileNo,    
    meco_Fax 
from gym_membercommon a     
where    
    meco_tenantId = 1    
    And    
    lower(ltrim(rtrim(meco_status))) <> 'erroneous'    

Select distinct a.*    
from #temp a      
inner join #temp b     
on    
(      
    (ltrim(rtrim(isnull(a.meco_officeTelno,''))) <>'' and a.meco_officeTelno in (b.meco_OfficeTelno,b.meco_HomeNo,b.meco_MobileNo,b.meco_Fax)) or     
    (ltrim(rtrim(isnull(a.meco_HomeNo,''))) <>'' and a.meco_HomeNo in (b.meco_OfficeTelno,b.meco_HomeNo,b.meco_MobileNo,b.meco_Fax)) or     
    (ltrim(rtrim(isnull(a.meco_MobileNo,''))) <>'' and a.meco_MobileNo in (b.meco_OfficeTelno,b.meco_HomeNo,b.meco_MobileNo,b.meco_Fax)) or     
    (ltrim(rtrim(isnull(a.meco_Fax,''))) <>'' and a.meco_Fax in (b.meco_OfficeTelno,b.meco_HomeNo,b.meco_MobileNo,b.meco_Fax))     
)     
and a.meco_Commonid <> b.meco_commonid     
And a.meco_tenantId = 1    

En attente de votre réponse

merci à l'avance.

Dasharath Yadav
Fitness Force

Répondre

1

Vos 'ou' créent d'horribles tablescans.

create table phonenumbers (
    commonid int, 
    phonenumber varchar(30) 
) 

insert into phonenumbers select commonid, meco_HomeNo from gym_membercommon; 
insert into phonenumbers select commonid, meco_OfficeTelno from gym_membercommon; 
insert into phonenumbers select commonid, meco_MobileNo from gym_membercommon; 
insert into phonenumbers select commonid, meco_Fax from gym_membercommon; 

select distinct commonid, phonenumber from phonenumbers 
where phonenumber in 
    (select phonenumber from phonenumbers 
    group by phonenumber 
    having count(*) >= 2) 
order by phonenumber 

Ceci vous donne tout le monde avec des numéros de téléphone partagés.

+0

Merci une tonne. cela a résolu mon problème, il prend maintenant moins de 3 secondes. J'apprécie grandement votre réponse –

0

Est-ce le temps d'aller dans la création #temp ou exeucting la requête?

Je pense que

in (b.meco_OfficeTelno,b.meco_HomeNo,b.meco_MobileNo,b.meco_Fax) 

ne bénéficie pas de l'indice

INDEX idxNC_TEMP ON #temp(meco_OfficeTelno,meco_HomeNo,meco_MobileNo,meco_Fax) 

Est-ce qu'un travail de table temporaire plus normalisée?

meco_Commonid int, 
meco_tenantid int, 
meco_ANY_OLD_NO varchar(30) 

que des extraits Populate de tous les 4 numéros (probablement que ce sera plus lent que votre seul extrait) mais la requête ne devrait être que une jointure simple qui devrait exploiter l'indice.

0

Que diriez-vous:

with temp (
    meco_Commonid, 
    meco_tenantid, 
    meco_OfficeTelno, 
    meco_HomeNo, 
    meco_MobileNo, 
    meco_Fax 
    ) 
as (
    select meco_Commonid, meco_tenantid, 
    meco_OfficeTelno, 
    meco_HomeNo, 
    meco_MobileNo, 
    meco_Fax from gym_membercommon a 
    where 
    meco_tenantId = 1 
    And 
    lower(ltrim(rtrim(meco_status))) <> 'erroneous' 
    ) 
Select distinct a.* 
    from temp a 
    inner join temp b 
    on 
    (
    (ltrim(rtrim(isnull(a.meco_officeTelno,''))) <>'' and a.meco_officeTelno in (b.meco_OfficeTelno,b.meco_HomeNo,b.meco_MobileNo,b.meco_Fax)) or 
    (ltrim(rtrim(isnull(a.meco_HomeNo,''))) <>'' and a.meco_HomeNo in (b.meco_OfficeTelno,b.meco_HomeNo,b.meco_MobileNo,b.meco_Fax)) or 
    (ltrim(rtrim(isnull(a.meco_MobileNo,''))) <>'' and a.meco_MobileNo in (b.meco_OfficeTelno,b.meco_HomeNo,b.meco_MobileNo,b.meco_Fax)) or 
    (ltrim(rtrim(isnull(a.meco_Fax,''))) <>'' and a.meco_Fax in (b.meco_OfficeTelno,b.meco_HomeNo,b.meco_MobileNo,b.meco_Fax)) 
    ) 
    and a.meco_Commonid <> b.meco_commonid 
    And a.meco_tenantId = 1 

Mais ... essayer de se débarrasser des garnitures et abaisse. Cela empêchera les index d'être utilisés correctement.

Et:

create index ixBlah on gym_membercommon (meco_tenantId, meco_status) include (meco_Commonid, meco_OfficeTelno, meco_HomeNo, meco_MobileNo, meco_Fax) 

Rob

Questions connexes