かなり大きなデータセットで実行されるこのクエリがあります。
それは非常に遅いです...
このクエリを最適化する必要がありますが、どこから始めればよいかわかりません (インデックスは別として)。
前もって感謝します!
SELECT d.distributor_id,
d.first_name,
d.last_name,
d.sponsor_id,
COUNT(f.business_level) AS total_enrollments,
SUM(CASE WHEN UPPER(f.business_level) = 'EXECUTIVE' THEN 1 else 0 end)
AS executive_enrollments,
SUM(CASE WHEN UPPER(f.business_level) = 'PERSONAL' THEN 1 else 0 end)
AS personal_enrollments,
SUM(CASE WHEN UPPER(f.business_level) = 'PREFERRED CUSTOMER' THEN 1 else 0 end)
AS preferred_customer_enrollments,
IFNULL(cf.commission_paid, 0) AS commission_paid,
IFNULL(cf.retention_earned, 0) AS retention_earned,
COUNT(df.order_type) AS total_autoships,
IFNULL(a.consecutive_streak, 0) AS autoship_streak,
IFNULL(a.enrollment_date, "Not Enrolled") AS autoship_enrollment,
d.highest_rank
FROM warehouse.distributor d
LEFT JOIN warehouse.enrollment_detail_fact f ON d.distributor_id = f.distributor_id
LEFT JOIN warehouse.country c ON d.country = c.name
AND c.country_id = 185
LEFT JOIN warehouse.autoship a ON d.distributor_id = a.distributor_id
LEFT JOIN warehouse.order_detail_fact df ON d.distributor_id = df.distributor_id
AND UPPER(order_type) = 'AUTOSHIP'
AND date_id IN(SELECT date_id FROM warehouse.date
WHERE DATE BETWEEN '2012-10-10'
AND '2012-10-11' ORDER BY date DESC)
LEFT JOIN warehouse.commission_detail_fact cf ON d.distributor_id = df.distributor_id
LEFT JOIN db.commission_level_type_details cl ON d.highest_rank = cl.name
WHERE d.active = 1
AND cl.commission_level_type_detail_id IN (23)
GROUP BY distributor_id
ORDER BY first_name;