INSERT INTO table3
SELECT
tbl1.pk_1, tbl1.pk_2, tbl2.pk_3, tbl1.pk4
FROM
table1 tbl1, table2 tbl2
WHERE
tbl1.pk_1 = 'root' and
NOT EXISTS
(SELECT 1 FROM table3 tbl3
WHERE tbl3.pk_1 = tbl1.pk_1 and tbl3.pk_2 = tbl1.pk_2 and tbl3.pk_3 = tbl2.pk_3 and tbl3.pk_4 = tbl1.pk_4)
;
My SQL statements looks like this. table1 and table3 have over 1000000 rows,table2 has 100 rows. This SQL statement is very slow. It takes more than 100 seconds finish. Is there any way to improve it?