0

Need your help optimising an insert query.

I have two tables in my MySQL DB. I will be inserting into Table1. Information will be retreived in Table2.

Table 2 contains a couple of million records. My insert query should look for all users who has a previous event in Table2 and insert it into Table1.

This is my insert query - but it's killing my server.

INSERT INTO Table1(
SELECT 
 d.created,
 d.userID, 
 d.eventID,
 d.previousEvent
FROM (
SELECT 
 e.created,
 e.userID,
 e.eventID,
 (SELECT eventID FROM TABLE2 pe WHERE e.userID=pe.userID AND e.created>pe.created ORDER BY pe.created DESC LIMIT 1) AS e.previousEvent
FROM 
 TABLE2 e
WHERE
 e.eventID not in (Select f.EventID from TABLE1 f)
ORDER BY 
 e.created desc
) d
WHERE
 d.preEvent is not null
GROUP by
 d.eventID,
 d.created,
 d.userID
);

eg:

Table1:

created | userID | eventID(unique) | previousEvent
2015年11月15日 66666 1045698664 4566660001

Table2:

created | userID | eventID(unique - random)
2015年11月15日 55555 4755500001
2015年11月15日 66666 4566660001
2015年11月15日 77777 8634757777
2015年11月15日 88888 1845562565
2015年11月15日 66666 1045698664
asked Nov 16, 2015 at 9:51
1
  • On your tables/columns have you tried creating an index? If so what is the fragmentation level of the index(s) Commented Nov 16, 2015 at 10:27

1 Answer 1

0

not in (Select f.EventID from TABLE1 f)

optimizes poorly. Turn it into a LEFT JOIN:

LEFT JOIN TABLE1 ON e.EventID = f.EventID
WHERE f.EventID IS NULL
answered Dec 3, 2015 at 4:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.