简体   繁体   中英

optimize the following query time in sql

select *
from ((select 'Temp', r.*
       from tab1 r 
       where (r.fa, r.date, r.status) not in (select r1.fa, r1.date, r1.status from tab2 r1)
      ) union all
      (select 'report', r.* 
       from tab2 r 
       where (r.fa, r.date, r.status) not in (select r1.fa, r1.date, r1.status from tab1 r1)
      )
     ) temp
order by fa;

As the data is increasing the query time is increasing. Please provide solution to optimize the time even if data keeps on increasing

so you want data A + B -(a intersect B). For that you are using 1 union and 2 not in clause. I tried to eliminate those in following query:

SELECT *
FROM(
SELECT X.*, COUNT(DISTINCT CN) OVER(PARTITION BY r.fa,r.date,r.status) CNT
FROM (
SELECT  'Temp' CN,r.* 
FROM    tab1 r 
UNION ALL
SELECT  'report' CN,r.* 
FROM    tab2 r 
)X)Y
WHERE Y.CNT=1

Basically first you should select all records using UNION ALL instead UNION. after that filter out those records which appeared in both tables(tab1, tab2) for that you can use analytical function.

For your query, you should start with indexes:

  • tab1(r.fa, r.date, r.status)
  • tab2(r.fa, r.date, r.status)

This should speed the not in portion of the queries. There isn't a way around the order by .

In general, using a subquery unnecessary imposes additional costs. However, because you are using order by , the subquery probably doesn't make a difference in terms of performance.

WHERE (a,b) ... has never been well optimized in MySQL. Avoid the construct.

IN ( SELECT ... ) has only recently been improved, but still it is not as good as EXISTS( SELECT * ...) or LEFT JOIN .. ON .. . Reformulate.

NOT IN , plus both of the above can be reformulated into LEFT JOIN ... ON ... WHERE ... IS NULL

You don't need the outer SELECT , you can sort a UNION thus:

( SELECT ... )
UNION ALL
( SELECT ... )
ORDER BY ...    -- applies to UNION

Optionally the SELECTs could also have ORDER BY , but it is useful only(?) if you have a LIMIT .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM