简体   繁体   中英

What is the best solution for adding INDEX to speed up the query?

Now I have a Query that runs 50 minutes on Mysql database and I can't accept that...

I want this process can running under 15 minutes....

insert into appianData.IC_DeletedRecords 
            (sourcetableid, 
             concatkey, 
             sourcetablecode) 
select mstid, 
        concatkey, 
        'icmstlocationheader' as sourcetablecode 
 from   appianData.IC_MST_LocationHeader
 where  concatkey not in(select concatkey 
                         from appianData.IC_PURGE_LocationHeader)

The "sourcetableid" and "mstid" are unique.

So what is the best way to add INDEX or optimize on this?

Thank you

I would write the select as:

select mstid, concatkey,  'icmstlocationheader' as sourcetablecode 
from  appianData.IC_MST_LocationHeader lh
where not exists (select 1 
                  from appianData.IC_PURGE_LocationHeader lhp
                  where lhp.concatkey = lh.concatkey
                 );

Then you want an index on IC_PURGE_LocationHeader(concatkey) .

Since it is a NOT IN condition you should be able to use a "LEFT JOIN ... WHERE rightTable has no match" without concern for multiple matches inflating the results.

INSERT INTO appianData.IC_DeletedRecords (sourcetableid, concatkey, sourcetablecode) 
SELECT m.mstid, m.concatkey, 'icmstlocationheader' as sourcetablecode 
FROM appianData.IC_MST_LocationHeader AS m
LEFT JOIN appianData.IC_PURGE_LocationHeader AS p
   ON m.concatkey = p.concatkey
WHERE p.concatkey IS NULL
;

With this version query, or the one you presented in the question, indexes on concatkey in both source tables should help significantly.

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