Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 slow query need help

Author  Topic 

send2sev
Starting Member

5 Posts

Posted - 2009-08-18 : 06:02:02
please help me I run the following query it takes 30 seconds, and the record count is 2000000.
----------------------------------
SELECT [cfcifno],[cffrnm],[cfltnm],[cfisexcod],[cfissn],[cfimobil],
[cadrphon],[CADRFAX],[cadremail],[cfjobcode],[tbdptype],[tcd_cfcifno],[tdserial],[cadrdsc1],[cadrphon_office] FROM customer
where ((CadrFax<>'') and(( Len (CadrFax) >= 10 and PatIndex('%2184840001',CadrFax)>0) or
(Len(CadrFax)<=9 and PatIndex('%184840001',CadrFax)>0) or (Len(CadrFax)<=8 and
PatIndex('%84840001',CadrFax)>0) or (Len(CadrFax)<=7 and PatIndex('%4840001',CadrFax)>0))) or

(( (CadrPhon<>'') and(Len (CadrPhon) >= 10 and PatIndex('%2184840001',CadrPhon)>0) or
(Len(CadrPhon)<=9 and PatIndex('%184840001',CadrPhon)>0) or (Len(CadrPhon)<=8 and
PatIndex('%84840001',CadrPhon)>0) or (Len(CadrPhon)<=7 and PatIndex('%4840001',CadrPhon)>0))) or

(( (CfiMobil<>'') and(Len (CfiMobil) >= 10 and PatIndex('%2184840001',CfiMobil)>0) or
(Len(CfiMobil)<=9 and PatIndex('%184840001',CfiMobil)>0) or (Len(CfiMobil)<=8 and
PatIndex('%84840001',CfiMobil)>0) or (Len(CfiMobil)<=7 and PatIndex('%4840001',CfiMobil)>0))) or

(( (cadrphon_office<>'') and(Len (cadrphon_office) >= 10 and
PatIndex('%2184840001',cadrphon_office)>0) or (Len(cadrphon_office)<=9 and
PatIndex('%184840001',cadrphon_office)>0) or
(Len(cadrphon_office)<=8 and PatIndex('%84840001',cadrphon_office)>0) or
(Len(cadrphon_office)<=7 and PatIndex('%4840001',cadrphon_office)>0)))

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-08-18 : 06:41:53
Try this
SELECT	cfcifno,
cffrnm,
cfltnm,
cfisexcod,
cfissn,
cfimobil,
cadrphon,
CADRFAX,
cadremail,
cfjobcode,
tbdptype,
tcd_cfcifno,
tdserial,
cadrdsc1,
cadrphon_office
FROM (
SELECT cfcifno,
cffrnm,
cfltnm,
cfisexcod,
cfissn,
cfimobil,
cadrphon,
CADRFAX,
cadremail,
cfjobcode,
tbdptype,
tcd_cfcifno,
tdserial,
cadrdsc1,
cadrphon_office,
CASE
WHEN LEN(CADRFAX) < 7 THEN 7
WHEN LEN(CADRFAX) >= 10 THEN 10
ELSE LEN(CADRFAX)
END AS y1,
CASE
WHEN LEN(CadrPhon) < 7 THEN 7
WHEN LEN(CadrPhon) >= 10 THEN 10
ELSE LEN(CadrPhon)
END AS y2,
CASE
WHEN LEN(CfiMobil) < 7 THEN 7
WHEN LEN(CfiMobil) >= 10 THEN 10
ELSE LEN(CfiMobil)
END AS y3,
CASE
WHEN LEN(cadrphon_office) < 7 THEN 7
WHEN LEN(cadrphon_office) >= 10 THEN 10
ELSE LEN(cadrphon_office)
END AS y4
FROM Customer
) AS d
WHERE RIGHT(CadrFax, y1) = RIGHT('2184840001', y1)
OR RIGHT(CadrPhon, y2) = RIGHT('2184840001', y2)
OR RIGHT(CfiMobil, y3) = RIGHT('2184840001', y3)
OR RIGHT(cadrphon_office, y4) = RIGHT('2184840001', y4)



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

send2sev
Starting Member

5 Posts

Posted - 2009-08-19 : 00:42:37
thank you for your reply sir but your query took 46 seconds! lol I really don't know what to do else, I have even installed Full text search services but no luck.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-08-19 : 03:14:43
You only ran the query once? Run the query a few times so there will be a cached version of the execution plan.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-08-19 : 05:11:12
Am I right in thinking that yo can't use any indexes with that WHERE clause?

Would this be better (even tough it looks worse)

WHERE
CadrFax IN ('2184840001', '184840001', '84840001', '4840001')
OR CadrPhon IN ('2184840001', '184840001', '84840001', '4840001')
OR CfiMobil IN ('2184840001', '184840001', '84840001', '4840001')
OR cadrphon_office IN ('2184840001', '184840001', '84840001', '4840001')


You'd be able to get rid of all the CASE LEN statements with that as well.

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-08-19 : 05:22:07
Meh -- noticed that that won't work for anything greater than ten or smaller than 7..... please ignore

NB: -- It's also slower!
Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -