Ich entwickle ein Marketing-System. Auf der ersten Seite müssen Vertriebsmitarbeiter die Anzahl der Verkaufschancen sehen, die sie derzeit haben.Wie komplexe Abfrage optimieren?
dh.
Birthdays | 10
Anniversaries | 15
Introductions | 450
Recurring | 249
Das Problem ist, ich bin UNION
all diese ing und die Abfrage über 10s in einigen Fällen nimmt. (Wir haben Caching an Ort und Stelle, so dass dies nur ein Problem ist, wenn sich ein Benutzer zum ersten Mal für den Tag anmeldet).
Es gibt eine Menge anderer Kriterien beteiligt:
- in der Zählung enthalten sollte nur die letzte einer pro Kunde Typ sein (dh, wenn ein Kunde zwei Einführungen hat, sollte es nur einmal gezählt werden -. ich bin mit der greatest-n-per-group Methode, dies zu erreichen)
- für Geburtstage und Jubiläen, sollte das Datum +/- 7 Tage ab heute
- für alle von ihnen, nur die Datensätze in den letzten 60 Tagen sind zu gezählt
- Diese Datensätze müssen beitreten ed mit dem Kunden-Tabelle, um sicherzustellen, dass der Verkäufer die Person ist
Hier aktuellen Verkaufs des Kunden passt die Möglichkeit die erzeugte Abfrage (Es ist lang):
SELECT 'Birthdays' AS `type`, COUNT(*) AS `num`
FROM `opportunities`
INNER JOIN `customers`
ON `opportunities`.`customer_id` = `customers`.`customer_id`
AND `opportunities`.`sales_person_id` = `customers`.`sales_person_id`
LEFT JOIN `opportunities` AS `o2`
ON `opportunities`.`customer_id` = `o2`.`customer_id`
AND `opportunities`.`marketing_message` = `o2`.`marketing_message`
AND opportunities.communication_alert_date < o2.communication_alert_date
WHERE ((`opportunities`.`org_code` = ?))
AND (opportunities.marketing_message = 'Birthday Alert')
AND ((opportunities.communication_alert_date BETWEEN
DATE_SUB(NOW(), INTERVAL 7 DAY) AND DATE_ADD(NOW(), INTERVAL 7 DAY)))
AND (opportunities.communication_alert_date >= DATE_SUB(NOW(), INTERVAL 60 DAY))
AND (o2.customer_id IS NULL)
UNION ALL
SELECT 'Anniversaries' AS `type`, COUNT(*) AS `num`
FROM `opportunities`
INNER JOIN `customers`
ON `opportunities`.`customer_id` = `customers`.`customer_id`
AND `opportunities`.`sales_person_id` = `customers`.`sales_person_id`
LEFT JOIN `opportunities` AS `o2`
ON `opportunities`.`customer_id` = `o2`.`customer_id`
AND `opportunities`.`marketing_message` = `o2`.`marketing_message`
AND opportunities.communication_alert_date < o2.communication_alert_date
WHERE ((`opportunities`.`org_code` = ?))
AND (opportunities.marketing_message = 'Anniversary Alert')
AND ((opportunities.communication_alert_date BETWEEN
DATE_SUB(NOW(), INTERVAL 7 DAY) AND DATE_ADD(NOW(), INTERVAL 7 DAY)))
AND (opportunities.communication_alert_date >= DATE_SUB(NOW(), INTERVAL 60 DAY))
AND (o2.customer_id IS NULL)
UNION ALL
SELECT 'Introductions' AS `type`, COUNT(*) AS `num`
FROM `opportunities`
INNER JOIN `customers`
ON `opportunities`.`customer_id` = `customers`.`customer_id`
AND `opportunities`.`sales_person_id` = `customers`.`sales_person_id`
LEFT JOIN `opportunities` AS `o2`
ON `opportunities`.`customer_id` = `o2`.`customer_id`
AND `opportunities`.`marketing_message` = `o2`.`marketing_message`
AND opportunities.communication_alert_date < o2.communication_alert_date
WHERE ((`opportunities`.`org_code` = ?))
AND ((opportunities.Intro_Letter = 'Yes'))
AND (opportunities.communication_alert_date >= DATE_SUB(NOW(), INTERVAL 60 DAY))
AND (o2.customer_id IS NULL)
UNION ALL
SELECT 'Recurring' AS `type`, COUNT(*) AS `num`
FROM `opportunities`
INNER JOIN `customers`
ON `opportunities`.`customer_id` = `customers`.`customer_id`
AND `opportunities`.`sales_person_id` = `customers`.`sales_person_id`
LEFT JOIN `opportunities` AS `o2`
ON `opportunities`.`customer_id` = `o2`.`customer_id`
AND `opportunities`.`marketing_message` = `o2`.`marketing_message`
AND opportunities.communication_alert_date < o2.communication_alert_date
WHERE ((`opportunities`.`org_code` = ?))
AND ((opportunities.marketing_message != 'Anniversary Alert'
AND opportunities.marketing_message != 'Birthday Alert'
AND opportunities.Intro_Letter != 'Yes'))
AND (opportunities.communication_alert_date >= DATE_SUB(NOW(), INTERVAL 60 DAY))
AND (o2.customer_id IS NULL)
Ich habe Indizes auf folgenden Feldern in der opportunities
Tabelle:
- org_code
- CUSTOMER_ID
- Intro_Letter
- marketing_message
- sales_person_id
- org_code, marketing_message
- org_code, Intro_Letter
- org_code, marketing_message, Intro_Letter
Jede Hilfe dieser Optimierung würde sehr geschätzt werden. Ich bin offen dafür, andere Tabellen oder Ansichten zu erstellen, wenn es sein muss.
zu zitieren "oder nicht zu zitieren - das ist die Frage –