I'm trying to
- get info from table final_stock_ul (4M records) only when there's an entry in another table final_stock_etablissements (10M records) matching on the "siren" code
- limit the result to 1000
- insert everything in a temporary table
Following code is very slow (15 sec) :
DROP TABLE IF EXISTS temp_results ;
CREATE TEMPORARY TABLE IF NOT EXISTS temp_results (
siren INT,
denomination VARCHAR(255)
) ENGINE=MYISAM DEFAULT CHARSET=utf8 ;
INSERT INTO temp_results (
siren,
denomination
)
SELECT
ul.siren,
ul.denomination
FROM dw.final_stock_ul ul
WHERE
exists
(
SELECT 1
FROM dw.final_stock_etablissements s
WHERE code_postal = 69001
AND s.siren = ul.siren
)
LIMIT 1000
However the "SELECT" part is very fast by itself (0.078 sec) :
SELECT
ul.siren,
ul.denomination
FROM dw.final_stock_ul ul
WHERE
exists
(
SELECT 1
FROM dw.final_stock_etablissements s
WHERE code_postal = 69001
AND s.siren = ul.siren
)
LIMIT 1000
How can the INSERT be so much slower than the SELECT ?
(with only 1000 records to insert)
EDIT: added missing field in INSERT statement