SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need help in complex search query optimization


Need help in complex search query optimization

Author
Message
Kamran Shahid
Kamran Shahid
Valued Member
Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)

Group: General Forum Members
Points: 73 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


SwePeso
SwePeso
SSCrazy Eights
SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)

Group: General Forum Members
Points: 9059 Visits: 3433
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"
ChrisM@Work
ChrisM@Work
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39310 Visits: 19991
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
Kamran Shahid
Kamran Shahid
Valued Member
Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)

Group: General Forum Members
Points: 73 Visits: 34
Thanks Peso
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search