次の2つのSQLクエリは95%同じですが、パフォーマンスは大きく異なります。非常に似た2つのSQLクエリのパフォーマンスは全く異なります
SQLクエリ1(< 0,1s):説明
SELECT CONCAT(a.`report_year`, '-', a. `report_month`) as `yearmonth`,
AVG(a.cost_leasing/b.rate*IF(`report_year`=2016,0.73235,
IF(`report_year`=2017,0.83430,1))) as average,
'current' as `type`
FROM `vehicles` as a, `exchange_rates` as b
WHERE cid='3' AND
STR_TO_DATE(CONCAT(`report_year`, '-', `report_month`, '-01'),
'%Y-%m-%d') >= '2016-01-01' AND
LAST_DAY(STR_TO_DATE(CONCAT(`report_year`, '-', `report_month`,
'-01'), '%Y-%m-%d')) <= '2017-06-30' AND
`country` IN ('XX','UK') AND
a.currency = b.currency AND
b.`year` = `report_year` AND
fxid=2
GROUP BY `yearmonth`
ORDER BY `yearmonth`;
クエリ1:
1 SIMPLE a ref new_selectors,... new_cost_leasing 4 const 10812 Using where; Using index; Using temporary; Using f...
1 SIMPLE b ref PRIMARY,date,fxid fxid 19 const,c1682fleet.a.report_year,c1682fleet.a.curren... 196 Using where; Using index
SQLクエリ2(> 3S):
SELECT CONCAT(c.`report_year`, '-', c.`report_month`) as `yearmonth`,
AVG(c.cost_leasing/d.rate*IF(`report_year`=2016,0.73235,
IF(`report_year`=2017,0.83430,1))),
'baseline'
FROM `kpis` as c, `exchange_rates` as d
WHERE cid='3' AND
STR_TO_DATE(CONCAT(`report_year`, '-', `report_month`, '-01'),
'%Y-%m-%d') >= '2016-01-01' AND
LAST_DAY(STR_TO_DATE(CONCAT(`report_year`, '-', `report_month`,
'-01'), '%Y-%m-%d')) <= '2017-06-30' AND
`country` IN ('XX','UK') AND
c.kid=1 AND
c.currency = d.currency AND
d.`year` = `report_year` AND
fxid=2
GROUP BY `yearmonth`
ORDER BY `yearmonth`;
説明クエリ2 :
1 SIMPLE c ref oem_group,... cost_leasing 8 const,const 30038 Using where; Using index; Using temporary; Using f...
1 SIMPLE d ref PRIMARY,date,fxid fxid 19 const,c1682fleet.c.report_year,c1682fleet.c.curren... 196 Using where; Using index
車両から
SHOWのINDEX:exchange_rates FROM
vehicles 0 PRIMARY 1 vid A 146068 BTREE
vehicles 1 new_cost_leasing 1 cid A 12 BTREE
vehicles 1 new_cost_leasing 2 cost_leasing A 4564 BTREE
vehicles 1 new_cost_leasing 3 currency A 5216 BTREE
vehicles 1 new_cost_leasing 4 report_month A 24344 BTREE
vehicles 1 new_cost_leasing 5 report_year A 29213 BTREE
vehicles 1 new_cost_leasing 6 country A 36517 BTREE
vehicles 1 new_cost_leasing 7 supplier A 29213 BTREE
vehicles 1 new_cost_leasing 8 jato_segment A 24344 BTREE
vehicles 1 new_cost_leasing 9 business_unit A 36517 BTREE
vehicles 1 new_cost_leasing 10 entity A 73034 BTREE
SHOWのINDEX:KPIのFROM
exchange_rates 0 PRIMARY 1 fxid A 2 BTREE
exchange_rates 0 PRIMARY 2 currency A 160 BTREE
exchange_rates 0 PRIMARY 3 date A 569250 BTREE
exchange_rates 1 date 1 fxid A 2 BTREE
exchange_rates 1 date 2 date A 28462 BTREE
exchange_rates 1 date 3 currency A 569250 BTREE
exchange_rates 1 date 4 rate A 569250 BTREE
exchange_rates 1 fxid 1 fxid A 2 BTREE
exchange_rates 1 fxid 2 year A 114 BTREE
exchange_rates 1 fxid 3 currency A 2904 BTREE
exchange_rates 1 fxid 4 rate A 569250 BTREE
SHOWのINDEX:
kpis 0 PRIMARY 1 vid A 60308 BTREE
kpis 1 cost_leasing 1 cid A 2 BTREE
kpis 1 cost_leasing 2 kid A 2 BTREE
kpis 1 cost_leasing 3 cost_leasing A 78 BTREE
kpis 1 cost_leasing 4 currency A 78 BTREE
kpis 1 cost_leasing 5 report_month A 1096 BTREE
kpis 1 cost_leasing 6 report_year A 3350 BTREE
kpis 1 cost_leasing 7 country A 1884 BTREE
kpis 1 cost_leasing 8 supplier A 4020 BTREE
kpis 1 cost_leasing 9 jato_segment A 3015 BTREE
kpis 1 cost_leasing 10 business_unit A 4307 BTREE
kpis 1 cost_leasing 11 entity A 6030 BTREE
kpis 1 avg_cost 1 cid A 2 BTREE
kpis 1 avg_cost 2 kid A 2 BTREE
kpis 1 avg_cost 3 country A 48 BTREE
kpis 1 avg_cost 4 report_year A 96 BTREE
kpis 1 avg_cost 5 currency A 96 BTREE
kpis 1 avg_cost 6 cost_leasing A 172 BTREE
質問: 私の質問は、なぜそこにありますあなたはたとえそうであっても、そのような重要な業績の違い(第30因子) ghでは、照会2(子)には索引の一部でもある追加の基準が1つしかありません。
誰でも私はどのようにクエリ2を最適化できますか?
完全に異なるテーブルを使用している場合、同じ95%であるとは言いません。潜在的に異なる構造、索引、レコード数...必要な情報が増えます。 –
のタブは、kpisにさらにフィールド「kid」が含まれていることと同じです。インデックスも同じですが、kpiインデックスに 'kid 'が列として追加されていることに匹敵します。 説明の回答に表示される影響を受ける行。 – faulix90
インデックス定義を投稿できますか? – eventHandler