so I have this:
EXPLAIN (SELECT f.prn AS 'prn', f.id AS 'xnyz', 'f' AS 'omnf', f.wwn, (COUNT(fr.rid) > 0 OR COUNT(frr.rid) > 0) AS 'rrr', fr.rid AS 'rid', f.xyzz
FROM scmf f
LEFT JOIN scmr fr
ON fr.omnf = 'f' AND fr.xnyz = f.id
LEFT JOIN scmr frr
ON frr.fid = f.id
WHERE f.prn IN (0,43570,43571,43572,43573,43574,43575) AND f.xyzz IN (490923) AND f.id IN (0,43570,43571,43572,43573,43574,43575)
GROUP BY f.id)
UNION
(SELECT fi.fxyz AS 'prn', fi.iix AS 'xnyz', 'n' AS 'omnf', fi.wwn,(COUNT(fir.rid) > 0) AS 'rrr', 0 AS 'rid', fif.xyzz
FROM scmf_item fi
LEFT JOIN scmr fir ON fir.omnf = 'n' AND fir.xnyz = fi.iix
LEFT JOIN scmf fif ON fif.id = fi.fxyz
WHERE fi.fxyz IN (0,43570,43571,43572,43573,43574,43575) AND fif.xyzz IN (490923)
GROUP BY fi.iix
)
ORDER BY wwn ASC;
And an explain reveals:
1 PRIMARY f range PRIMARY,xyzz,prn PRIMARY 4 7 Using where
1 PRIMARY fr ref omnf_id omnf_id 9 const,s.f.id 1 Using index
1 PRIMARY frr ref Index1 Index1 4 s.f.id 1 Using index
2 UNION fi range fxyz fxyz 4 24 Using where; Using temporary; Using filesort
2 UNION fif eq_ref PRIMARY,xyzz PRIMARY 4 s.fi.fxyz 1 Using where
2 UNION fir ref omnf_id omnf_id 9 const,s.fi.iix 1 Using index
UNION RESULT <union1,2> ALL Using filesort
Why is the UNION clause using filesort/have type as ALL
is this bad?
Can I use indices to fix this?
Related
If I execute the following query
select * from medienkatalog_nct.sphinx_index limit 380000, 10000
the query hang forever between status "none" and "closing tables".
explain select * from medienkatalog_nct.sphinx_index;
# id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
1 SIMPLE katalognummer index PRIMARY,artikelNr,katalognr,idvideothek artikelNr 4 275870 Using index
1 SIMPLE artikel ref PRIMARY PRIMARY 4 medienkatalog_nct.katalognummer.katalogNr 1
1 SIMPLE artikel eq_ref PRIMARY,id_artikelnr PRIMARY 4 medienkatalog_nct.katalognummer.artikelNr 1
1 SIMPLE relation_kategorie ref PRIMARY PRIMARY 4 medienkatalog_nct.artikel.artikelNr 1 Using index
1 SIMPLE kategorie eq_ref PRIMARY PRIMARY 4 medienkatalog_nct.relation_kategorie.id_kategorie 1
1 SIMPLE relation_inhalt ref PRIMARY PRIMARY 4 medienkatalog_nct.artikel.artikelNr 1 Using index
1 SIMPLE inhalt eq_ref PRIMARY,id_inhalt PRIMARY 4 medienkatalog_nct.relation_inhalt.id_inhalt 1
1 SIMPLE relation_medium ref PRIMARY PRIMARY 4 medienkatalog_nct.katalognummer.artikelNr 1 Using index
1 SIMPLE medium eq_ref PRIMARY PRIMARY 4 medienkatalog_nct.relation_medium.id_medium 1
1 SIMPLE relation_plattform ref PRIMARY PRIMARY 4 medienkatalog_nct.artikel.artikelNr 1 Using index
1 SIMPLE plattform eq_ref PRIMARY PRIMARY 4 medienkatalog_nct.relation_plattform.id_plattform 1
1 SIMPLE relation_hersteller ref PRIMARY PRIMARY 4 medienkatalog_nct.artikel.artikelNr 1 Using index
1 SIMPLE hersteller eq_ref PRIMARY,id_hersteller PRIMARY 4 medienkatalog_nct.relation_hersteller.id_hersteller 1
1 SIMPLE relation_titel eq_ref PRIMARY PRIMARY 4 medienkatalog_nct.katalognummer.artikelNr 1
1 SIMPLE titel eq_ref id_titel id_titel 4 medienkatalog_nct.relation_titel.id_titel 1 Using index
1 SIMPLE relation_freigabe ref PRIMARY PRIMARY 4 medienkatalog_nct.artikel.artikelNr 1 Using index
1 SIMPLE freigabe eq_ref PRIMARY,id_freigabe PRIMARY 4 medienkatalog_nct.relation_freigabe.id_freigabe 1
1 SIMPLE media eq_ref PRIMARY PRIMARY 4 medienkatalog_nct.katalognummer.artikelNr 1
1 SIMPLE artikel_videothek ref PRIMARY,i_katalognr,i_id_videothek PRIMARY 4 medienkatalog_nct.katalognummer.katalogNr 5 Using where
1 SIMPLE videothek eq_ref PRIMARY,INDEX PRIMARY 4 medienkatalog_nct.artikel_videothek.id_videothek 1 Using where
Edit: the SQL query
CREATE
ALGORITHM = UNDEFINED
DEFINER = `dbadmin`#`%`
SQL SECURITY DEFINER
VIEW `sphinx_index` AS
SELECT
((`medienkatalog_nct`.`artikel_videothek`.`id_videothek` * 1000000000) + `medienkatalog_nct`.`artikel`.`artikelNr`) AS `pkey`,
`medienkatalog_nct`.`fkey`(`medienkatalog_nct`.`artikel`.`artikelNr`,
`medienkatalog_nct`.`media`.`key_media`) AS `fkey`,
`medienkatalog_nct`.`artikel_videothek`.`id_videothek` AS `id_videothek`,
`medienkatalog_nct`.`artikel`.`artikelNr` AS `artikelnr`,
`medienkatalog_nct`.`artikel_videothek`.`katalognr` AS `katalognr`,
`medienkatalog_nct`.`media`.`key_media` AS `key_media`,
`medienkatalog_nct`.`media`.`has_werbebanner` AS `has_werbebanner`,
`medienkatalog_nct`.`media`.`has_mailbanner` AS `has_mailbanner`,
`medienkatalog_nct`.`media`.`has_animation` AS `has_animation`,
`medienkatalog_nct`.`media`.`has_klappe` AS `has_klappe`,
`medienkatalog_nct`.`media`.`has_klappeemo` AS `has_klappeemo`,
`medienkatalog_nct`.`media`.`has_podcast` AS `has_podcast`,
IF(ISNULL(`medienkatalog_nct`.`media`.`top20rang`),
100,
IF((`medienkatalog_nct`.`media`.`top20rang` = 0),
50,
`medienkatalog_nct`.`media`.`top20rang`)) AS `top20rang`,
`medienkatalog_nct`.`media`.`has_trailer` AS `has_trailer`,
`medienkatalog_nct`.`media`.`has_traileremo` AS `has_traileremo`,
UNIX_TIMESTAMP(`medienkatalog_nct`.`media`.`verleihstart`) AS `verleihstart`,
`medienkatalog_nct`.`media`.`has_vorschaubanner` AS `has_vorschaubanner`,
TOP_PREIS(`medienkatalog_nct`.`artikel_videothek`.`id_videothek`,
`medienkatalog_nct`.`artikel_videothek`.`katalognr`) AS `is_toppreis`,
IF(((`nachbestellsystem`.`artikel`.`aktiv` = 1)
AND (`nachbestellsystem`.`artikel`.`lieferbar` = 1)
AND ISNULL(`nachbestellsystem`.`artikel`.`dekommissionierung`)),
1,
0) AS `is_kommission`,
`medienkatalog_nct`.`kategorie`.`beschreibung` AS `kategorie`,
`medienkatalog_nct`.`titel`.`titel` AS `titel`,
`medienkatalog_nct`.`titel`.`titel` AS `titel_sort`,
`medienkatalog_nct`.`str_to_ord`(`medienkatalog_nct`.`titel`.`titel`) AS `titel_ord`,
`medienkatalog_nct`.`titel`.`untertitel` AS `untertitel`,
`medienkatalog_nct`.`medium`.`id_medium` AS `id_medium`,
`medienkatalog_nct`.`medium`.`beschreibung` AS `medium`,
`medienkatalog_nct`.`plattform`.`id_plattform` AS `id_plattform`,
`medienkatalog_nct`.`plattform`.`beschreibung` AS `plattform`,
ID_VERSIONS(`medienkatalog_nct`.`artikel`.`artikelNr`) AS `vkey`,
`medienkatalog_nct`.`version`(`medienkatalog_nct`.`artikel`.`artikelNr`) AS `version`,
ID_GENRES(`medienkatalog_nct`.`artikel`.`artikelNr`) AS `gkey`,
GENRE(`medienkatalog_nct`.`artikel`.`artikelNr`) AS `genre`,
UNIX_TIMESTAMP(`medienkatalog_nct`.`artikel`.`artikelstart`) AS `artikelstart`,
UNIX_TIMESTAMP(`medienkatalog_nct`.`artikel_videothek`.`erfassungsdatum`) AS `erfassungsdatum`,
`medienkatalog_nct`.`hersteller`.`id_hersteller` AS `id_hersteller`,
`medienkatalog_nct`.`hersteller`.`beschreibung` AS `hersteller`,
ACTOR(`medienkatalog_nct`.`artikel`.`artikelNr`) AS `darsteller`,
COUNTRY(`medienkatalog_nct`.`artikel`.`artikelNr`) AS `land`,
DIRECTOR(`medienkatalog_nct`.`artikel`.`artikelNr`) AS `regie`,
FORMAT(RANKING(`medienkatalog_nct`.`artikel`.`artikelNr`),
2) AS `sterne`,
COUNT_RANKING(`medienkatalog_nct`.`artikel`.`artikelNr`) AS `bewertungen`,
ANZAHL_VL(`medienkatalog_nct`.`artikel_videothek`.`id_videothek`,
`medienkatalog_nct`.`artikel`.`artikelNr`,
(NOW() - INTERVAL 1 MONTH),
NOW()) AS `anzahl_vl`,
ANZAHL_VK(`medienkatalog_nct`.`artikel_videothek`.`id_videothek`,
`medienkatalog_nct`.`artikel`.`artikelNr`,
(NOW() - INTERVAL 1 MONTH),
NOW()) AS `anzahl_vk`,
`medienkatalog_nct`.`inhalt`.`beschreibung` AS `inhalt`,
`medienkatalog_nct`.`inhalt`.`aufmacher` AS `aufmacher`,
`medienkatalog_nct`.`relation_kategorie`.`id_kategorie` AS `id_kategorie`,
`medienkatalog_nct`.`relation_titel`.`id_titel` AS `id_titel`,
`medienkatalog_nct`.`relation_freigabe`.`id_freigabe` AS `id_freigabe`,
`medienkatalog_nct`.`freigabe`.`beschreibung` AS `freigabe`,
CONCAT(IF((`medienkatalog_nct`.`artikel_videothek`.`ist_verleih` <> 0),
'Leihartikel ',
''),
IF((`medienkatalog_nct`.`artikel_videothek`.`ist_gebraucht` <> 0),
'Gebrauchtartikel Verkaufsartikel ',
''),
IF(((`medienkatalog_nct`.`artikel_videothek`.`ist_verkauf` <> 0)
AND (`medienkatalog_nct`.`artikel_videothek`.`bestand_vk` > 0)),
IF((`medienkatalog_nct`.`artikel_videothek`.`ist_gebraucht` = 0),
'Verkaufsartikel Neuware',
'Neuware'),
'')) AS `artikelart`,
`medienkatalog_nct`.`artikel_videothek`.`deleted` AS `deleted`
FROM
(((((((((((((((((((`medienkatalog_nct`.`artikel`
JOIN `medienkatalog_nct`.`katalognummer` ON ((`medienkatalog_nct`.`artikel`.`artikelNr` = `medienkatalog_nct`.`katalognummer`.`artikelNr`)))
JOIN `medienkatalog_nct`.`artikel_videothek` ON (((`medienkatalog_nct`.`katalognummer`.`katalogNr` = `medienkatalog_nct`.`artikel_videothek`.`katalognr`)
AND (`medienkatalog_nct`.`katalognummer`.`vtid` IN (0 , `medienkatalog_nct`.`artikel_videothek`.`id_videothek`)))))
LEFT JOIN `medienkatalog_nct`.`relation_kategorie` ON ((`medienkatalog_nct`.`artikel`.`artikelNr` = `medienkatalog_nct`.`relation_kategorie`.`artikelNr`)))
LEFT JOIN `medienkatalog_nct`.`kategorie` ON ((`medienkatalog_nct`.`relation_kategorie`.`id_kategorie` = `medienkatalog_nct`.`kategorie`.`id_kategorie`)))
LEFT JOIN `medienkatalog_nct`.`relation_inhalt` ON ((`medienkatalog_nct`.`artikel`.`artikelNr` = `medienkatalog_nct`.`relation_inhalt`.`artikelNr`)))
LEFT JOIN `medienkatalog_nct`.`inhalt` ON ((`medienkatalog_nct`.`relation_inhalt`.`id_inhalt` = `medienkatalog_nct`.`inhalt`.`id_inhalt`)))
LEFT JOIN `medienkatalog_nct`.`relation_medium` ON ((`medienkatalog_nct`.`artikel`.`artikelNr` = `medienkatalog_nct`.`relation_medium`.`artikelNr`)))
LEFT JOIN `medienkatalog_nct`.`medium` ON ((`medienkatalog_nct`.`relation_medium`.`id_medium` = `medienkatalog_nct`.`medium`.`id_medium`)))
LEFT JOIN `medienkatalog_nct`.`relation_plattform` ON ((`medienkatalog_nct`.`artikel`.`artikelNr` = `medienkatalog_nct`.`relation_plattform`.`artikelNr`)))
LEFT JOIN `medienkatalog_nct`.`plattform` ON ((`medienkatalog_nct`.`relation_plattform`.`id_plattform` = `medienkatalog_nct`.`plattform`.`id_plattform`)))
LEFT JOIN `medienkatalog_nct`.`relation_hersteller` ON ((`medienkatalog_nct`.`artikel`.`artikelNr` = `medienkatalog_nct`.`relation_hersteller`.`artikelNr`)))
LEFT JOIN `medienkatalog_nct`.`hersteller` ON ((`medienkatalog_nct`.`relation_hersteller`.`id_hersteller` = `medienkatalog_nct`.`hersteller`.`id_hersteller`)))
LEFT JOIN `medienkatalog_nct`.`relation_titel` ON ((`medienkatalog_nct`.`artikel`.`artikelNr` = `medienkatalog_nct`.`relation_titel`.`artikelNr`)))
LEFT JOIN `medienkatalog_nct`.`titel` ON ((`medienkatalog_nct`.`relation_titel`.`id_titel` = `medienkatalog_nct`.`titel`.`id_titel`)))
LEFT JOIN `medienkatalog_nct`.`relation_freigabe` ON ((`medienkatalog_nct`.`artikel`.`artikelNr` = `medienkatalog_nct`.`relation_freigabe`.`artikelNr`)))
LEFT JOIN `medienkatalog_nct`.`freigabe` ON ((`medienkatalog_nct`.`freigabe`.`id_freigabe` = `medienkatalog_nct`.`relation_freigabe`.`id_freigabe`)))
LEFT JOIN `medienkatalog_nct`.`media` ON ((`medienkatalog_nct`.`artikel`.`artikelNr` = `medienkatalog_nct`.`media`.`artikelnr`)))
JOIN `videotaxi`.`videothek` ON ((`medienkatalog_nct`.`artikel_videothek`.`id_videothek` = `videotaxi`.`videothek`.`id_videothek`)))
LEFT JOIN `nachbestellsystem`.`artikel` ON ((`nachbestellsystem`.`artikel`.`katalogNr` = `medienkatalog_nct`.`katalognummer`.`katalogNr`)))
WHERE
(((`medienkatalog_nct`.`artikel_videothek`.`ist_gebraucht` <> 0)
OR (`medienkatalog_nct`.`artikel_videothek`.`ist_verleih` <> 0)
OR (`medienkatalog_nct`.`artikel_videothek`.`ist_verkauf` <> 0))
AND (`videotaxi`.`videothek`.`offen` <> 0)
AND (`medienkatalog_nct`.`artikel_videothek`.`deleted` = 0))
The query worked well for more than 3 years until last week.
Does anybody have ideas?
following mysql query taking more time to fetch results so i need help to optimise
SELECT status,
count(status) AS COUNT,
build_id,
results_bu.name,
results_bu.creation_ts,
results_bu.release_date
FROM
(SELECT NHTC.parent_id AS tsuite_id,
NHTC.id AS tcase_id,
NHTC.name AS name,
TPTCV.tcversion_id,
TPTCV.platform_id,
E.build_id,
E.tcversion_number AS VERSION,
TCV.tc_external_id AS external_id,
E.id AS executions_id,
E.status AS status,
(TPTCV.urgency * TCV.importance) AS urg_imp
FROM testplan_tcversions TPTCV
/* GO FOR Absolute LATEST exec ID On BUILD,PLATFORM */
JOIN
(SELECT EE.tcversion_id,
EE.testplan_id,
EE.platform_id,
EE.build_id,
MAX(EE.id) AS id
FROM executions EE
WHERE EE.testplan_id=13954
AND EE.build_id IN
(SELECT id
FROM builds
WHERE testplan_id = 13954
AND active=1
AND (creation_ts BETWEEN DATE('2015-01-01') AND DATE('2015-05-10'))
ORDER BY name ASC)
GROUP BY EE.tcversion_id,
EE.testplan_id,
EE.platform_id,
EE.build_id) AS LEBBP ON LEBBP.testplan_id = TPTCV.testplan_id
AND LEBBP.platform_id = TPTCV.platform_id
AND LEBBP.tcversion_id = TPTCV.tcversion_id
AND LEBBP.testplan_id = 13954
/* Get execution status WRITTEN on DB */
JOIN executions E ON E.id = LEBBP.id
AND E.build_id = LEBBP.build_id
/* Get Test Case info from Test Case Version */
JOIN nodes_hierarchy NHTCV ON NHTCV.id = TPTCV.tcversion_id
/* Get Test Suite info from Test Case */
JOIN nodes_hierarchy NHTC ON NHTC.id = NHTCV.parent_id
/* Get Test Case Version attributes */
JOIN tcversions TCV ON TCV.id = E.tcversion_id
WHERE TPTCV.testplan_id=13954
AND E.build_id IN
(SELECT id
FROM builds
WHERE testplan_id = 13954
AND active=1
AND (creation_ts BETWEEN DATE('2015-01-01') AND DATE('2015-05-10'))
ORDER BY name ASC)
UNION ALL
/* sqlUnion Test suites - not run */
SELECT NHTC.parent_id AS tsuite_id,
NHTC.id AS tcase_id,
NHTC.name AS name,
TPTCV.tcversion_id,
TPTCV.platform_id,
BU.id AS build_id,
TCV.VERSION,
TCV.tc_external_id AS external_id,
COALESCE(E.id,-1) AS executions_id,
COALESCE(E.status,'n') AS status,
(TPTCV.urgency * TCV.importance) AS urg_imp
FROM testplan_tcversions TPTCV
/* Needed to be able to put a value on build_id on output set */
JOIN builds BU ON BU.id IN
(SELECT id
FROM builds
WHERE testplan_id = 13954
AND active=1
AND (creation_ts BETWEEN DATE('2015-01-01') AND DATE('2015-05-10'))
ORDER BY name ASC)
/* GO FOR Absolute LATEST exec ID On BUILD,PLATFORM */
LEFT OUTER JOIN
(SELECT EE.tcversion_id,
EE.testplan_id,
EE.platform_id,
EE.build_id,
MAX(EE.id) AS id
FROM executions EE
WHERE EE.testplan_id=13954
AND EE.build_id IN
(SELECT id
FROM builds
WHERE testplan_id = 13954
AND active=1
AND (creation_ts BETWEEN DATE('2015-01-01') AND DATE('2015-05-10'))
ORDER BY name ASC)
GROUP BY EE.tcversion_id,
EE.testplan_id,
EE.platform_id,
EE.build_id) AS LEBBP ON LEBBP.testplan_id = TPTCV.testplan_id
AND LEBBP.platform_id = TPTCV.platform_id
AND LEBBP.tcversion_id = TPTCV.tcversion_id
AND LEBBP.build_id = BU.id
AND LEBBP.testplan_id = 13954
/* Get execution status WRITTEN on DB */
LEFT OUTER JOIN executions E ON E.build_id = LEBBP.build_id
AND E.testplan_id = TPTCV.testplan_id
AND E.platform_id = TPTCV.platform_id
AND E.tcversion_id = TPTCV.tcversion_id
/* Get Test Case info from Test Case Version */
JOIN nodes_hierarchy NHTCV ON NHTCV.id = TPTCV.tcversion_id
/* Get Test Suite info from Test Case */
JOIN nodes_hierarchy NHTC ON NHTC.id = NHTCV.parent_id
/* Get Test Case Version attributes */
JOIN tcversions TCV ON TCV.id = TPTCV.tcversion_id
WHERE TPTCV.testplan_id=13954
AND BU.id IN
(SELECT id
FROM builds
WHERE testplan_id = 13954
AND active=1
AND (creation_ts BETWEEN DATE('2015-01-01') AND DATE('2015-05-10'))
ORDER BY name ASC)
/* Get REALLY NOT RUN => BOTH LEBBP.id AND E.id NULL */
AND E.id IS NULL
AND LEBBP.id IS NULL) AS RESULT
LEFT OUTER JOIN builds AS results_bu ON results_bu.id=RESULT.build_id
GROUP BY status,
build_id;
results for EXPLAIN as follows
id select_type table type possible_keys key key_len ref rows Extra
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 PRIMARY <derived2> ALL 32768 Using temporary; Using filesort
1 PRIMARY results_bu eq_ref PRIMARY PRIMARY 4 result.build_id 1
2 DERIVED <derived3> ALL 32229 Using where
2 DERIVED NHTCV eq_ref PRIMARY,pid_m_nodeorder PRIMARY 4 LEBBP.tcversion_id 1
2 DERIVED NHTC eq_ref PRIMARY PRIMARY 4 bitnami_testlink.NHTCV.parent_id 1
2 DERIVED TPTCV eq_ref testplan_tcversions_tplan_tcversion testplan_tcversions_tplan_tcversion 12 LEBBP.tcversion_id,LEBBP.platform_id 1
2 DERIVED E eq_ref PRIMARY PRIMARY 4 LEBBP.id 1 Using where
2 DERIVED TCV eq_ref PRIMARY PRIMARY 4 bitnami_testlink.E.tcversion_id 1
4 DEPENDENT
SUBQUERY builds unique_subquery PRIMARY,name,testplan_id PRIMARY 4 func 1 Using where
3 DERIVED EE ref executions_idx1 executions_idx1 4 135350 Using where; Using index
3 DERIVED BB eq_ref PRIMARY,name,testplan_id PRIMARY 4 bitnami_testlink.EE.build_id 1 Using where
5 UNION BU index testplan_id 4 569 Using where; Using index
5 UNION TPTCV ref testplan_tcversions_tplan_tcversion testplan_tcversions_tplan_tcversion 4 1472
5 UNION TCV eq_ref PRIMARY PRIMARY 4 bitnami_testlink.TPTCV.tcversion_id 1
5 UNION NHTCV eq_ref PRIMARY,pid_m_nodeorder PRIMARY 4 bitnami_testlink.TPTCV.tcversion_id 1
5 UNION NHTC eq_ref PRIMARY PRIMARY 4 bitnami_testlink.NHTCV.parent_id 1
5 UNION <derived6> ALL 32229 Using where
5 UNION E ref executions_idx1 executions_idx1 16 bitnami_testlink.TPTCV.tcversion_id,
bitnami_testlink.TPTCV.platform_id,
LEBBP.build_id 1 Using where; Not exists
8 DEPENDENT
SUBQUERY builds unique_subquery PRIMARY,name,testplan_id PRIMARY 4 func 1 Using where
7 DEPENDENT
SUBQUERY builds unique_subquery PRIMARY,name,testplan_id PRIMARY 4 func 1 Using where
6 DERIVED EE ref executions_idx1 executions_idx1 4 135350 Using where; Using index
6 DERIVED BB eq_ref PRIMARY,name,testplan_id PRIMARY 4 bitnami_testlink.EE.build_id 1 Using where
UNION RESULT <union2,5> ALL
Actually we are trying to get how many number of execution records passed, failed between period of time for selected Test plan from test link database
Thanks,
Ram
I have the following query:
(
select f.Product, bpsrelation.bps, f.Week ForecastWeek,
f.FC Forecast, `Order qty` quantity,
s.FC Sales, s.Week salesWeek, s.`Plant Code`, f.`APO Location`
from forecastnew f
right outer join bpsrelation on bpsrelation.Product = f.Product
right outer join sales s on bpsrelation.bps = s.BPS and f.Week = s.Week
and s.`PLant Code` = f.`APO Location`
right outer join orders on orders.Product = f.Product
group by s.Week, s.`PLant Code`,s.Customer,s.bps
)
union
(
select f.Product, bpsrelation.bps, f.Week ForecastWeek,
f.FC Forecast, `Order qty` quantity,
s.FC Sales, s.Week salesWeek, s.`Plant Code`, f.`APO Location`
from forecastnew f
left outer join orders on orders.Product = f.Product
left outer join bpsrelation on bpsrelation.Product = f.Product
left outer join sales s on bpsrelation.bps = s.BPS
and f.Week = s.Week and s.`PLant Code` = f.`APO Location`
where f.`APO Location` is null
group by s.Week, f.`APO Location`, s.Customer,s.bps
);
The explain extended is as follows:
1 PRIMARY orders ALL 33335 100.00 Using temporary; Using filesort
1 PRIMARY f ref idx_forecastnew_Product idx_forecastnew_Product 23 demandplaning.orders.Product 103 100.00
1 PRIMARY bpsrelation ref idx_bpsrelation_Product,idx_bpsrelation_bps idx_bpsrelation_Product 23 demandplaning.f.Product 1 100.00
1 PRIMARY s ref idx_sales_BPS idx_sales_BPS 33 demandplaning.bpsrelation.bps 41 100.00 Using where
2 UNION f ALL 329325 100.00 Using where; Using temporary; Using filesort
2 UNION orders ref idx_orders_Product idx_orders_Product 23 demandplaning.f.Product 64 100.00
2 UNION bpsrelation ref idx_bpsrelation_Product idx_bpsrelation_Product 23 demandplaning.f.Product 1 100.00
2 UNION s ref idx_sales_BPS idx_sales_BPS 33 demandplaning.bpsrelation.bps 41 100.00 Using where
UNION RESULT <union1,2> ALL Using temporary
The forecast and the sales table need to be joined by plant code, week and product.
This takes a lot of time. I searched stackoverflow and internet, it is adviced to do this with foreach or cursor. Can anyone help me with that?
Thanks a lot.
Im using PDO Mysql, and made a request to select cheapest offers for a product in my database. It works fine, only problem is it is slow (for 200 offers (and still just 25 to return)) it takes almost a second, which is a lot higher than what I aim.
I'm no expert in SQL, so I seek for your help on this matter. Here is the request and I'll be happy to provide more info if needed :
SELECT
mo.id AS id,
mo.stock AS stock,
mo.price AS price,
mo.promotional_price AS promotional_price,
mo.picture_1 AS picture_1,
mo.picture_2 AS picture_2,
mo.picture_3 AS picture_3,
mo.picture_4 AS picture_4,
mo.picture_5 AS picture_5,
mo.title AS title,
mo.description AS description,
mo.state AS state,
mo.is_new AS is_new,
mo.is_original AS is_original,
c.name AS name,
u.id AS user_id,
u.username AS username,
u.postal_code AS postal_code,
p.name AS country_name,
ra.cache_rating_avg AS cache_rating_avg,
ra.cache_rating_nb AS cache_rating_nb,
GROUP_CONCAT(md.delivery_mode_id SEPARATOR ', ') AS delivery_mode_ids,
GROUP_CONCAT(ri.title SEPARATOR ', ') AS delivery_mode_titles
FROM
mp_offer mo, catalog_product_i18n c,
ref_country_i18n p, mp_offer_delivery_mode md,
ref_delivery_mode r,
ref_delivery_mode_i18n ri, user u
LEFT JOIN mp_user_review_rating_i18n ra
ON u.id = ra.user_id
WHERE (mo.product_id = c.id
AND mo.culture = c.culture
AND mo.user_id = u.id
AND u.country_id = p.id
AND mo.id = md.offer_id
AND md.delivery_mode_id = ri.id
AND mo.culture = ri.culture)
AND (mo.culture = 1
AND p.culture = 1)
AND mo.is_deleted = 0
AND mo.product_id = 60
AND ((u.holiday_start IS NULL)
OR (u.holiday_start = '0000-00-00')
OR (u.holiday_end IS NULL)
OR (u.holiday_end = '0000-00-00')
OR (u.holiday_start > '2012-05-03')
OR (u.holiday_end < '2012-05-03'))
AND mo.stock > 0
GROUP BY mo.id
ORDER BY IF (mo.promotional_price IS NULL,
mo.price,
LEAST(mo.price, mo.promotional_price)) ASC
LIMIT 25 OFFSET 0;
I take the offers for a particular product that have their "culture" set to 1, are not deleted, that have some stock and whose seller is not in holidays. I order by price (promotional_price when there is one).
Is LEAST a slow function?
Here is the output of EXPLAIN :
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE c const PRIMARY,catalog_product_i18n_product,catalog_product_i18n_culture PRIMARY 8 const,const 1 "Using temporary; Using filesort"
1 SIMPLE mo ref PRIMARY,culture,is_deleted,product_id,user_id culture 4 const 3 "Using where with pushed condition"
1 SIMPLE u eq_ref PRIMARY,user_country PRIMARY 4 database.mo.user_id 1 "Using where with pushed condition"
1 SIMPLE p eq_ref PRIMARY,ref_country_i18n_culture PRIMARY 8 database.u.country_id,const 1
1 SIMPLE r ALL NULL NULL NULL NULL 3 "Using join buffer"
1 SIMPLE ra ALL NULL NULL NULL NULL 4
1 SIMPLE md ref PRIMARY,fk_offer_has_delivery_mode_delivery_mode1,fk_offer_has_delivery_mode_offer1 PRIMARY 4 database.mo.id 2
1 SIMPLE ri eq_ref PRIMARY PRIMARY 2 database.md.delivery_mode_id,const 1
Thanks in advance for your help on optimizing this request.
J
You are not making use of ref_delivery_mode table that you have included in from clause. It's getting cause of Cartesian product of tables result.
I have a query (see below) that I have a custom developed UDF that is used to calculate whether or not certain points are within a polygon (first query in UNION) or circular (second query in UNION) shape.
select e.inquiry_match_type_id
, a.geo_boundary_id
, GeoBoundaryContains(c.tpi_geo_boundary_coverage_type_id, 29.287437, -95.055807, a.lat, a.lon, a.geo_boundary_vertex_id ) in_out
, e.inquiry_id
, e.external_id
, COALESCE(f.inquiry_device_id,0) inquiry_device_id
, b.external_info1
, b.external_info2
, b.geo_boundary_id
, b.geo_boundary_type_id
from geo_boundary_vertex a
join geo_boundary b on b.geo_boundary_id = a.geo_boundary_id
join trackpoint_index_geo_boundary_mem c on c.geo_boundary_id = b.geo_boundary_id
join trackpoint_index_mem d on d.trackpoint_index_id = c.trackpoint_index_id
join inquiry_mem e on e.inquiry_id = b.inquiry_id left
outer join inquiry_device_mem f on f.inquiry_id = e.inquiry_id and f.device_id = 3201
where d.trackpoint_index_id = 3127
and b.geo_boundary_type_id = 3
and e.expiration_date >= now()
group by
a.geo_boundary_id
UNION
select e.inquiry_match_type_id
, b.geo_boundary_id
, GeoBoundaryContains( c.tpi_geo_boundary_coverage_type_id, 29.287437, -95.055807, b.centroid_lat, b.centoid_lon, b.radius ) in_out
, e.inquiry_id
, e.external_id
, COALESCE(f.inquiry_device_id,0) inquiry_device_id
, b.external_info1
, b.external_info2
, b.geo_boundary_id
, b.geo_boundary_type_id
from geo_boundary b
join trackpoint_index_geo_boundary_mem c on c.geo_boundary_id = b.geo_boundary_id
join trackpoint_index_mem d on d.trackpoint_index_id = c.trackpoint_index_id
join inquiry_mem e on e.inquiry_id = b.inquiry_id
left outer join inquiry_device_mem f on f.inquiry_id = e.inquiry_id and f.device_id = 3201
where d.trackpoint_index_id = 3127
and b.geo_boundary_type_id = 2
and e.expiration_date >= now()
group by
b.geo_boundary_id
When I run an explain for the query I get the following:
id select_type table type possible_keys key key_len ref rows Extra
------ -------------- ---------- ------- --------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------- ---------- ------------------------ ------- -------------------------------
1 PRIMARY d const PRIMARY PRIMARY 4 const 1 Using temporary; Using filesort
1 PRIMARY c ref PRIMARY,fk_mtp_idx_geo_boundary_mtp_idx,fk_mtp_idx_geo_boundary_geo_boundary,fk_mtp_idx_geo_boundary_mtp_mem_idx,fk_mtp_idx_geo_boundary_geo_boundary_mem fk_mtp_idx_geo_boundary_mtp_idx 4 const 9
1 PRIMARY b eq_ref PRIMARY,fk_geo_boundary_inquiry,fk_geo_boundary_geo_boundary_type PRIMARY 4 gothim.c.geo_boundary_id 1 Using where
1 PRIMARY e eq_ref PRIMARY PRIMARY 4 gothim.b.inquiry_id 1 Using where
1 PRIMARY f ref fk_inquiry_device_mem_inquiry fk_inquiry_device_mem_inquiry 4 gothim.e.inquiry_id 2
1 PRIMARY a ref fk_geo_boundary_vertex_geo_boundary fk_geo_boundary_vertex_geo_boundary 4 gothim.b.geo_boundary_id 11 Using where
2 UNION d const PRIMARY PRIMARY 4 const 1 Using temporary; Using filesort
2 UNION c ref PRIMARY,fk_mtp_idx_geo_boundary_mtp_idx,fk_mtp_idx_geo_boundary_geo_boundary,fk_mtp_idx_geo_boundary_mtp_mem_idx,fk_mtp_idx_geo_boundary_geo_boundary_mem fk_mtp_idx_geo_boundary_mtp_idx 4 const 9
2 UNION b eq_ref PRIMARY,fk_geo_boundary_inquiry,fk_geo_boundary_geo_boundary_type PRIMARY 4 gothim.c.geo_boundary_id 1 Using where
2 UNION e eq_ref PRIMARY PRIMARY 4 gothim.b.inquiry_id 1 Using where
2 UNION f ref fk_inquiry_device_mem_inquiry fk_inquiry_device_mem_inquiry 4 gothim.e.inquiry_id 2
(null) UNION RESULT <union1,2> ALL (null) (null) (null) (null) (null) Using filesort
12 record(s) selected [Fetch MetaData: 1ms] [Fetch Data: 5ms]
Now, I can split the queries up and use the ORDER BY NULL trick to get rid of the filesort however when I attempt to add that to the end of a UNION it doesn't work.
I am considering splitting the query apart into 2 queries or possibly re-writing it completely not to use a UNION (though that is a bit more difficult of course). The other thing I have working against me is that we have this in production and I'd like to limit changes - I would have loved just to be able to add ORDER BY NULL to the end of the query and be done with it, but it doesn't work w/ the UNION.
Any help would be greatly appreciated.
Normally, ORDER BY can be used for the individual queries within a UNION like this:
(
SELECT *
FROM table1, …
GROUP BY
id
ORDER BY
NULL
)
UNION ALL
(
SELECT *
FROM table2, …
GROUP BY
id
ORDER BY
NULL
)
However, as the docs state:
However, use of ORDER BY for individual SELECT statements implies nothing about the order in which the rows appear in the final result because UNION by default produces an unordered set of rows. Therefore, the use of ORDER BY in this context is typically in conjunction with LIMIT, so that it is used to determine the subset of the selected rows to retrieve for the SELECT, even though it does not necessarily affect the order of those rows in the final UNION result. If ORDER BY appears without LIMIT in a SELECT, it is optimized away because it will have no effect anyway.
This is of course a smart move, however, not too smart, since they forgot to optimize away the ordering behavior of GROUP BY as well.
So as for now, you should add a very high LIMIT to your individual queries:
(
SELECT *
FROM table1, …
GROUP BY
id
ORDER BY
NULL
LIMIT 100000000
)
UNION ALL
(
SELECT *
FROM table2, …
GROUP BY
id
ORDER BY
NULL
LIMIT 100000000
)
I'll post it as a bug to MySQL, hope they'll fix it in the next release, but meanwhile you could use this solution.
Note that a similar solution (using TOP 100%) was used to force ordering of the subqueries in SQL Server 2000, however, it stopped working in 2005 (ORDER BY has no effect in subqueries with TOP 100% for the optimizer).
It is safe to use it though since it won't break your queries even if the optimizer behavior changes in the next releases, but will just make them as slow as they are now.
Maybe try something like
SELECT *
FROM
(
[your entire query here]
) DerivedTable
ORDER BY NULL
I've never used MySQL so forgive me if I'm missing the plot :)
EDIT: What if you run each individual query separately (which, as you say, works), but insert the data into a temporary table. Then, at the end just do a select from the temp table.
Have you tried changing the UNION to UNION ALL?
A UNION tries to remove duplicate rows. In order to do that, it would have to sort the intermediate results what might explain what you are seeing in your execution plan.
From MySQL Union
By default the MySQL UNION removes all
duplicate rows from the result set
even if you don’t explicit using
DISTINCT after the keyword UNION.
If you use UNION ALL explicitly, the
duplicate rows remain in the result
set. You only use this in the cases
that you want to keep duplicate rows
or you are sure that there is no
duplicate rows in the result set.
Edit
I doubt it will make any difference (might even be worse) but could you try following "equivalent" query
select *
from (
select b.geo_boundary_id
, GeoBoundaryContains( c.tpi_geo_boundary_coverage_type_id, 29.287437, -95.055807, b.centroid_lat, b.centoid_lon, b.radius ) in_out
from geo_boundary b
join trackpoint_index_geo_boundary_mem c on c.geo_boundary_id = b.geo_boundary_id
where b.geo_boundary_type_id = 2
group by
b.geo_boundary_id
union all
select a.geo_boundary_id
, GeoBoundaryContains(c.tpi_geo_boundary_coverage_type_id, 29.287437, -95.055807, a.lat, a.lon, a.geo_boundary_vertex_id ) in_out
from geo_boundary_vertex a
join geo_boundary b on b.geo_boundary_id = a.geo_boundary_id
join trackpoint_index_geo_boundary_mem c on c.geo_boundary_id = b.geo_boundary_id
where b.geo_boundary_type_id = 3
group by
a.geo_boundary_id
) s
inner join (
select e.inquiry_match_type_id
, e.inquiry_id
, e.external_id
, COALESCE(f.inquiry_device_id,0) inquiry_device_id
, b.external_info1
, b.external_info2
, b.geo_boundary_id
, b.geo_boundary_type_id
from geo_boundary b
join trackpoint_index_geo_boundary_mem c on c.geo_boundary_id = b.geo_boundary_id
join trackpoint_index_mem d on d.trackpoint_index_id = c.trackpoint_index_id
join inquiry_mem e on e.inquiry_id = b.inquiry_id left
outer join inquiry_device_mem f on f.inquiry_id = e.inquiry_id and f.device_id = 3201
where d.trackpoint_index_id = 3127
and b.geo_boundary_type_id IN (2, 3)
and e.expiration_date >= now()
) r on r.geo_boundary_id = s.geo_boundary_id