sql >> Database teknologi >  >> RDS >> Mysql

MySql sammensat indeks

Dette er din første forespørgsel:

SELECT A.log_type, count(*) as distinct_count, sum(A.total_count) as total_count
from (SELECT log_type, count(subscriber_id) as total_count
      FROM stats.campaign_logs
      WHERE domain = 'xxx' AND campaign_id = '12345' AND
            log_type IN ('EMAIL_SENT', 'EMAIL_CLICKED', 'EMAIL_OPENED', 'UNSUBSCRIBED') AND
             DATE(CONVERT_TZ(log_time,'+00:00','+05:30')) BETWEEN DATE('2015-02-12 00:00:00') AND DATE('2015-02-19 23:59:58')
      GROUP BY subscriber_id,log_type) A
GROUP BY A.log_type;

Det er bedre skrevet som:

      SELECT log_type, count(DISTINCT subscriber_id) as total_count
      FROM stats.campaign_logs
      WHERE domain = 'xxx' AND campaign_id = '12345' AND
            log_type IN ('EMAIL_SENT', 'EMAIL_CLICKED', 'EMAIL_OPENED', 'UNSUBSCRIBED') AND
             DATE(CONVERT_TZ(log_time, '+00:00', '+05:30')) BETWEEN DATE('2015-02-12 00:00:00') AND DATE('2015-02-19 23:59:58')
      GROUP BY log_type;

Det bedste indeks på dette er sandsynligvis:campaign_logs(domain, campaign_id, log_type, log_time, subscriber_id) . Dette er et dækkende indeks for forespørgslen. De første tre nøgler skal bruges til where filtrering.



  1. Upload flere billeder med PHP, der kun gemmer én filsti til MySQL-databasen

  2. Henter MySQL med Kivy

  3. SQL Server Express vs Express localdb

  4. Udpak MySQL-felter af række til PHP-variabler og array