Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove duplicate sub-query

I have a complex SQL query that can be simplified to the below:

Select ColA,ColB,ColC,ColD
From MyTable
Where (ColA In (Select ItemID From Items Where ItemName like '%xxx%') 
    or ColB In (Select ItemID From Items Where ItemName like '%xxx%'))

As you can see, the sub-query appears twice. Is the compiler intelligent enough to detect this and gets the result of the sub-query only once? Or does the sub-query run twice?

FYI, table Items has about 20,000 rows and MyTable has about 200,000 rows.

Is there another way to re-write this SQL statement so that the sub-query appears/runs only once?

Update: The Where clause in the main query is dynamic and added only when needed (i.e. only when a user searches for 'xxx'). Hence changes to the main select statement or re-structuring of the query are not possible.

like image 923
navigator Avatar asked Feb 29 '16 08:02

navigator


People also ask

How remove duplicates in SQL query inner join?

Solution. Select column values in a specific order within rows to make rows with duplicate sets of values identical. Then you can use SELECT DISTINCT to remove duplicates.

How do I remove duplicates in ETL?

To configure the Remove Duplicates transformation go to Transformation / MAPPING / Complex Transformations / Remove Duplicates .

Can delete have sub queries?

DELETE operations with subqueries that reference the same table object are supported only if all of the following conditions are true: The subquery either returns a single row, or else has no correlated column references. The subquery is in the DELETE statement WHERE clause, using Condition with Subquery syntax.


1 Answers

UPDATE Your request not to change the query, just the WHERE

You can pack the CTE directly in the place where it is called (untested):

Select ColA,ColB,ColC,ColD
From MyTable
Where EXISTS (SELECT 1 FROM (Select i.ItemID 
                             From Items AS i 
                             Where iItemName like '%xxx%') AS itm 
              WHERE itm.ItemID=MyTable.ColA OR itm.ItemID=MyTable.ColB) 

previous

I think this should be the same...

WITH MyCTE AS
(
    Select ItemID From Items Where ItemName like '%xxx%'
)
Select ColA,ColB,ColC,ColD
From MyTable
Where EXISTS (SELECT 1 FROM MyCTE WHERE ItemID=ColA OR ItemID=ColB) 

A substring LIKE search is - for sure - not performant.

If you can reduce your "Items" to just a few rows with your LIKE filter, you must test which is fastest.

like image 165
Shnugo Avatar answered Oct 24 '22 00:10

Shnugo