|
|
|
SSC 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
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 1:26 PM
Points: 2,359,
Visits: 3,292
|
|
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"
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: 2 days ago @ 8:21 AM
Points: 5,602,
Visits: 10,950
|
|
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
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Thursday, January 31, 2013 4:03 AM
Points: 41,
Visits: 34
|
|
|
|
|