Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need help in complex search query optimization Expand / Collapse
Author
Message
Posted Thursday, September 2, 2010 11:22 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, January 31, 2013 4:03 AM
Points: 41, Visits: 34
Need help in optimizing following query.
I am having long delay if i am opening the commented lines in either of inner or outer query.


SELECT X.*
FROM
(
SELECT CON_SHIPPER_CONTENT_ID
,CON_SA_CREATE_DATETIME
,CON_PK_JOB_ID
,CON_PK_DATABASE_CONTAINER_ID
,CON_MAILCLASS
,CON_PROCESSINGCATEGORY
,CON_ENTRY_POINT_PHYSICAL
,VerificationLocation
,CON_ZONE_SKIPPING_INDICATOR
,PriorityMailIndicator
,CON_NEWS_INDICATOR
,CON_DESTINATION_DISCOUNT_INDICATOR
,CON_RATETYPE
,CON_SCHEDULED_INDUCTION_DATE
,CON_INHOME_START_DATETIME
FROM MDB_CONTENT_INFO
WHERE ((CON_CONSIGNEE_ID IS NULL) OR (CON_CONSIGNEE_ID = 'N/A'))
AND ((CON_FAST_CONTENT_ID IS NULL) OR (CON_FAST_CONTENT_ID = 'N/A'))
AND (SAC_IsLinked is null or SAC_IsLinked = 'False' )
AND (SAC_SchedulerContentID is null)
AND ShipperApptRequestID is null
AND (CON_SHIPPER_CONTENT_ID not in (SELECT items FROM dbo.GetCommaSplitValue(@InputVariable,',')))
AND (@MailClass = '-1-' OR CON_MailClass = @MailClass)
And (@MailShape = '-1-' OR CON_ProcessingCategory = @MailShape)
And (@InductionDate = '-1-' OR CON_SCHEDULED_INDUCTION_DATE = @InductionDate)
And (@VerificationLocation = '-1-' OR VerificationLocation like '%' + @VerificationLocation+ '%')
And (@LocaleKey = '-1-' OR CON_ENTRY_POINT_PHYSICAL like '%' + @LocaleKey+ '%')
And (@IsPriorityMail = '-1-' OR ( PriorityMailIndicator =
CASE WHEN @IsPriorityMail = 'True' THEN 1
WHEN @IsPriorityMail ='False' THEN 0
END
))
And (@IsZoneSkip = '-1-' OR ( CON_ZONE_SKIPPING_INDICATOR =
CASE WHEN @IsZoneSkip = 'True' THEN 1
WHEN @IsZoneSkip ='False' THEN 0
END
))
And (@IsNews = '-1-' OR ( CON_NEWS_INDICATOR =
CASE WHEN @IsNews = 'True' THEN 1
WHEN @IsNews ='False' THEN 0
END
))
And (@IsDestinationDiscount = '-1-' OR ( CON_DESTINATION_DISCOUNT_INDICATOR =
CASE WHEN @IsDestinationDiscount = 'True' THEN 1
WHEN @IsDestinationDiscount ='False' THEN 0
END
))
AND ISDATE(CON_SCHEDULED_INDUCTION_DATE) = 1
And ISDATE(CON_INHOME_START_DATETIME) = 1
/*AND CON_ENTRY_POINT_PHYSICAL is not null
And LEN(CON_ENTRY_POINT_PHYSICAL) > 6
AND Right(CON_ENTRY_POINT_PHYSICAL, 6) in
(
Select RIGHT(Rtrim(Ltrim(RAL_DROPSITE_KEY)), 6)
FROM REF_ADDRESS_LIST_FILE
Where RAL_DROPSITE_KEY IS NOT NULL
AND RAL_DS_DLVYADD_STATEABBRV IS NOT NULL
AND RTRIM(LTRIM(RAL_DS_DLVYADD_STATEABBRV))
in (
SELECT RTRIM(LTRIM(RAL_DS_PHYSADD_STATEABBRV))
FROM REF_ADDRESS_LIST_FILE
INNER JOIN
(
SELECT DISTRICTS.DIS_PK_ZIP_CODE
FROM DISTRICTS
WHERE DISTRICTS.DIS_PK_DISTRICT_ID in
(
SELECT UDR_FK_DISTRICT_ID = CAST(UDR_FK_DISTRICT_ID as int)
FROM USER_DISTRICT
WHERE UDR_FK_LOGIN_ID = @UserLoginID
)
)x ON x.DIS_PK_ZIP_CODE = LEFT(RAL_DS_PHYSADD_ZIP4CODE,3)
)
)
*/


) X
WHERE convert(datetime, X.CON_SCHEDULED_INDUCTION_DATE,112) >= GetDate()
And convert(datetime, X.CON_INHOME_START_DATETIME,112) >= GetDate()
/*AND CON_ENTRY_POINT_PHYSICAL is not null
And LEN(CON_ENTRY_POINT_PHYSICAL) > 6
AND Right(CON_ENTRY_POINT_PHYSICAL, 6) in
(
Select RIGHT(Rtrim(Ltrim(RAL_DROPSITE_KEY)), 6)
FROM REF_ADDRESS_LIST_FILE
Where RAL_DROPSITE_KEY IS NOT NULL
AND RAL_DS_DLVYADD_STATEABBRV IS NOT NULL
AND RTRIM(LTRIM(RAL_DS_DLVYADD_STATEABBRV))
in (
SELECT RTRIM(LTRIM(RAL_DS_PHYSADD_STATEABBRV))
FROM REF_ADDRESS_LIST_FILE
INNER JOIN
(
SELECT DISTRICTS.DIS_PK_ZIP_CODE
FROM DISTRICTS
WHERE DISTRICTS.DIS_PK_DISTRICT_ID in
(
SELECT UDR_FK_DISTRICT_ID = CAST(UDR_FK_DISTRICT_ID as int)
FROM USER_DISTRICT
WHERE UDR_FK_LOGIN_ID = @UserLoginID
)
)x ON x.DIS_PK_ZIP_CODE = LEFT(RAL_DS_PHYSADD_ZIP4CODE,3)
)
)
*/
Order by CON_PK_JOB_ID, CON_PK_DATABASE_CONTAINER_ID

Post #980038
Posted Friday, September 3, 2010 1:31 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 3:35 PM
Points: 2,393, Visits: 3,399
Also asked and answered her http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=149677
and here http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/32604047-54d5-4f15-9e4c-4ebc11c40f08



N 56°04'39.16"
E 12°55'05.25"
Post #980058
Posted Friday, September 3, 2010 2:55 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:21 AM
Points: 7,133, Visits: 13,520
Here's some more helpful advice to go with Dave B's.

The part you are commenting out is a query which can be run in isolation from the rest of the query. Like the main body of the query it's been written in a naive manner and can almost certainly be improved. Changing IN to INNER JOIN etc results in something like the query below, which is much easier to work with because it's far more readable (Changing IN to INNER JOIN often won't improve performance because the optimizer will probably do it anyway).
Once you've got this far, you can check out all of those string functions wrapped around your arguments which are preventing index usage.
The query looks as if it's been written without any knowledge of the data.
Remove the string functions (one JOIN at a time) and examine the results. Examine the data in the columns to see if the string functions are really necessary. Somewhere in there is a CAST to INT - check the datatype of the column to see if it is necessary.
Before you do any of this, read the recommendations posted by others.

SELECT RIGHT(RTRIM(LTRIM(r1.RAL_DROPSITE_KEY)), 6)

FROM REF_ADDRESS_LIST_FILE r1

INNER JOIN REF_ADDRESS_LIST_FILE r2
ON RTRIM(LTRIM(r2.RAL_DS_PHYSADD_STATEABBRV)) = RTRIM(LTRIM(r1.RAL_DS_DLVYADD_STATEABBRV))

INNER JOIN DISTRICTS d
ON d.DIS_PK_ZIP_CODE = LEFT(r2.RAL_DS_PHYSADD_ZIP4CODE,3)

INNER JOIN USER_DISTRICT u
ON CAST(u.UDR_FK_DISTRICT_ID AS INT) = d.DIS_PK_DISTRICT_ID
AND u.UDR_FK_LOGIN_ID = @UserLoginID

WHERE r1.RAL_DROPSITE_KEY IS NOT NULL
AND r1.RAL_DS_DLVYADD_STATEABBRV IS NOT NULL




“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #980077
Posted Friday, September 3, 2010 2:58 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, January 31, 2013 4:03 AM
Points: 41, Visits: 34
Thanks Peso
Post #980080
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse