This query takes ~90 seconds:
CREATE TABLE filtered
(
SELECT DISTINCT
f2.cityCode,
f2.productCode,
f2.productDesc,
f2.inventoryDate,
f2.inventoryLevel
FROM
etm
INNER JOIN f2avail AS f2 ON
etm.cityCode = f2.cityCode AND
etm.tvlDate = f2.inventoryDate
);
but the subquery on its own takes ~0.07 seconds:
-- CREATE TABLE filtered
-- (
SELECT DISTINCT
f2.cityCode,
f2.productCode,
f2.productDesc,
f2.inventoryDate,
f2.inventoryLevel
FROM
etm
INNER JOIN f2avail AS f2 ON
etm.cityCode = f2.cityCode AND
etm.tvlDate = f2.inventoryDate
-- );
Why is that?
Following this solution, I ran OPTIMIZE TABLE f2avail
(the one with ~246,000 rows) and the create query ran about 25% faster, but still slow.
How can I speed up saving the subquery results? This is just one step in a series of filtering stages in an internal DB for reporting. Both tables are InnoDB.
Subquery explain
results:
Update
The solution was to use composite indices on both tables.
-- Add the composite indices
ALTER TABLE `etm` ADD INDEX (`cityCode`, `tvlDate`);
ALTER TABLE `f2avail` ADD INDEX (`cityCode`, `inventoryDate`);
The query went from ~90s to about 6s.
Best Answer
Your screen images leave out details that would show up in
SHOW CREATE TABLE
. But I will make some guesses.You need, but don't have, a "composite"
INDEX(cityCode, tvlDate)
onetm
and/or a similar index on the other table.Other issues:
DATE
to aDATETIME
may not work as you expect. If theDATETIME
has any time other than00:00:00
, it will not match anyDATE
.etm
when all the data comes from the other table?Back to the question... Perhaps nothing was cached in RAM when the 90s run was done. Try each query twice.