How to optimize a query having multiple like conditions

LogicalDesk

I've following query into which I'm filling temporary table from multiple select statements which are using multiple like conditions but I'm unable to optimize the query.

if object_id('tempdb..#tempTable') is not null

    drop table #tempTable

select totalProcessed=(select  COUNT(*) as totalProcessed 
from DetailItem di
inner join DownloadFile df 
on di.DownloadFileID = df.DownLoadID
where 
(CONVERT(date, di.CompletionTime)) = '2018-01-31' and
di.DetailItemName<>'99999999' 
and df.CompanyID = 164 
and (df.filename like '%OA73030%'  or df.filename like '%OA73035%'
or df.FileName like '%OA77030%' or df.filename like '%OA77035%')),

totalErrored=(select  COUNT(*) as totalErrored 
from DetailItem di
inner join DownloadFile df 
on di.DownloadFileID = df.DownLoadID
where 
(di.CompletionDetail  like '%Not found%'or di.CompletionDetail  like '%error%')  and 
CONVERT(date, di.CompletionTime) ='2018-01-31' 
and di.DetailItemName<>'99999999' 
and df.CompanyID = 164 
and (df.FileName like '%OA73030%' or df.filename like '%OA73035%'
or df.FileName like '%OA77030%' or df.filename like '%OA77035%')),

totalSuccess=(select  COUNT(*) as totalSuccess 
from DetailItem di
inner join DownloadFile df 
on di.DownloadFileID = df.DownLoadID
where 
(di.CompletionDetail not like '%error%' and di.CompletionDetail not like '%Not Found%') and 
CONVERT(date, di.CompletionTime) ='2018-01-31' 
and di.DetailItemName<>'99999999' 
and df.CompanyID = 164
and (df.FileName like '%OA73030%' or df.filename like '%OA73035%'
or df.FileName like '%OA77030%' or df.filename like '%OA77035%')) into #tempTable
select totalProcessed, totalErrored, totalSuccess,cast(round(((totalSuccess*100.0)/totalProcessed),2) as decimal(5,2)) as '%ProcessedSuccessfully',
(totalProcessed-totalSuccess)as 'TotalRe-Processed(2ndBD)',0 as 'TotalSuccessfullyProcessedOutOfRe-order(3rdBD)',0 as 'TotalSLAMissed' 
from #tempTable
Alexander Volok

I assume that search values comes dynamically and no way to predict of what going to be selected. Then, you cannot do a lot with constructions like:

WHERE df.filename like '%OA73030%'  or df.filename like '%OA73035%'
or df.FileName like '%OA77030%' or df.filename like '%OA77035%'

Since there is no way to rewrite it to:

WHERE df.filename like IN ('%OA73030%','%OA73035%','%OA77030%','%OA77035%')

However, you entire logic of your query can be very much optimized this way:

SELECT COUNT(*) AS totalProcessed
,       COUNT(CASE WHEN    di.CompletionDetail LIKE '%Not found%' OR di.CompletionDetail LIKE '%error%'  THEN 1 END) AS totalErrored
,       COUNT(CASE WHEN    di.CompletionDetail NOT LIKE '%error%' AND di.CompletionDetail NOT LIKE '%Not Found%'  THEN 1 END) AS totalErrored
,       COUNT(CASE WHEN    di.CompletionDetail NOT LIKE '%error%' AND di.CompletionDetail NOT LIKE '%Not Found%'  THEN 1 END) AS totalSuccess
FROM   DetailItem di
       INNER JOIN DownloadFile df ON di.DownloadFileID = df.DownLoadID
WHERE  di.CompletionTime between '2018-01-31' AND '2018-01-31 23:59:59'
       AND di.DetailItemName <> '99999999'
       AND df.CompanyID = 164
       AND (   df.filename LIKE '%OA73030%'
               OR df.filename LIKE '%OA73035%'
               OR df.FileName LIKE '%OA77030%'
               OR df.filename LIKE '%OA77035%' );

In this case the data engine have to search data only once for all aggregates instead of four times.

Next to that, your approach of trimming time is not SARGable:

( CONVERT(DATE, di.CompletionTime)) = '2018-01-31'

and it has to be rewritten to:

di.CompletionTime between '2018-01-31' AND '2018-01-31 23:59:59'

Otherwise possible index on CompletionTime will not be used

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to query pass 2 of multiple conditions

分類Dev

How to optimize the following query

分類Dev

How to optimize query?

分類Dev

MongoDB Query with multiple conditions and slice

分類Dev

optimize pandas query on multiple columns / multiindex

分類Dev

MS Access Update Query (Multiple Where Conditions)

分類Dev

How can I optimize this mysql query please?

分類Dev

How to optimize find by date query in mongo

分類Dev

How to use multiple if conditions for Pandas?

分類Dev

Optimize select Query performance that is having join with various other tables in MS SQL

分類Dev

How can I link two selects without having any conditions?

分類Dev

How to perform a LIKE query using multiple keywords from search field using mysqli prepared statement

分類Dev

How to combine "LIKE" with "IN" in a MYSQL query?

分類Dev

How to optimize bash script parsing multiple gzipped files with multiple patterns?

分類Dev

How to correctly index where conditions of a query?

分類Dev

How to use Query Builder when different conditions

分類Dev

How to conditions in multiple associated table in laravel 5.2

分類Dev

How to filter string in multiple conditions python pandas

分類Dev

How to sort JSON with multiple conditions using PHP?

分類Dev

How to find dates from multiple categories by conditions

分類Dev

How to select certain columns by multiple conditions?

分類Dev

How do you write multiple conditions on if statements

分類Dev

How to include multiple conditions in an if function in PHP?

分類Dev

How do i make multiple conditions in java?

分類Dev

How to write IF statement in a optimize way for multiple condition for string variable?

分類Dev

Oracle Optimize Query

分類Dev

MySQL optimize huge query

分類Dev

Django query is slow, optimize

分類Dev

How to mock mongoose query like findOne()?

Related 関連記事

  1. 1

    How to query pass 2 of multiple conditions

  2. 2

    How to optimize the following query

  3. 3

    How to optimize query?

  4. 4

    MongoDB Query with multiple conditions and slice

  5. 5

    optimize pandas query on multiple columns / multiindex

  6. 6

    MS Access Update Query (Multiple Where Conditions)

  7. 7

    How can I optimize this mysql query please?

  8. 8

    How to optimize find by date query in mongo

  9. 9

    How to use multiple if conditions for Pandas?

  10. 10

    Optimize select Query performance that is having join with various other tables in MS SQL

  11. 11

    How can I link two selects without having any conditions?

  12. 12

    How to perform a LIKE query using multiple keywords from search field using mysqli prepared statement

  13. 13

    How to combine "LIKE" with "IN" in a MYSQL query?

  14. 14

    How to optimize bash script parsing multiple gzipped files with multiple patterns?

  15. 15

    How to correctly index where conditions of a query?

  16. 16

    How to use Query Builder when different conditions

  17. 17

    How to conditions in multiple associated table in laravel 5.2

  18. 18

    How to filter string in multiple conditions python pandas

  19. 19

    How to sort JSON with multiple conditions using PHP?

  20. 20

    How to find dates from multiple categories by conditions

  21. 21

    How to select certain columns by multiple conditions?

  22. 22

    How do you write multiple conditions on if statements

  23. 23

    How to include multiple conditions in an if function in PHP?

  24. 24

    How do i make multiple conditions in java?

  25. 25

    How to write IF statement in a optimize way for multiple condition for string variable?

  26. 26

    Oracle Optimize Query

  27. 27

    MySQL optimize huge query

  28. 28

    Django query is slow, optimize

  29. 29

    How to mock mongoose query like findOne()?

ホットタグ

アーカイブ