0

基本的にアクションのテーブルを組み合わせて、ページネーションを維持しながら時系列で選択するクエリがあります..

これを行うためのより効率的/より良い方法はありますか? クエリには 3 秒かかります。ひどいものではありません..しかし、私は改善の余地があり、私はそれをたくさん使うと思います..

ありがとう!

SELECT 
   `newsletters_subscribers`.`email`,
   `newsletters_subscribers`.`first_name`, 
   `newsletters_subscribers`.`last_name`,
   `newsletters_subscribers`.`id` AS subscriber_id,
    COUNT(DISTINCT newsletters_opens.id) AS opens,
    COUNT(DISTINCT newsletters_clicks.id) AS clicks,
    COUNT(DISTINCT newsletters_forwards.id) AS forwards
FROM `thebookrackqccom_newsletters_subscribers` newsletters_subscribers
  LEFT JOIN 
   `thebookrackqccom_newsletters_opens` newsletters_opens
      ON `newsletters_opens`.`subscriber_id` = `newsletters_subscribers`.`id` 
      AND newsletters_opens.newsletter_id = 1
  LEFT JOIN
   `thebookrackqccom_newsletters_clicks` newsletters_clicks 
      ON `newsletters_clicks`.`subscriber_id` = `newsletters_subscribers`.`id` 
      AND newsletters_clicks.newsletter_id = 1
  LEFT JOIN
   `thebookrackqccom_newsletters_forwards` newsletters_forwards 
      ON `newsletters_forwards`.`subscriber_id` = `newsletters_subscribers`.`id` 
      AND newsletters_forwards.newsletter_id = 1
WHERE
     ( newsletters_opens.id IS NOT NULL 
    OR newsletters_clicks.id IS NOT NULL 
    OR newsletters_forwards.id IS NOT NULL ) 
GROUP BY 
   `newsletters_subscribers`.`id`
ORDER BY 
   `newsletters_subscribers`.`email` ASC
LIMIT 25
4

2 に答える 2

1

必要なのは、クエリが使用できるインデックスです。(newsletter_id, subscribe_id)3 つのテーブルのそれぞれの複合インデックスが役立ちます。

次のようにクエリを書き直すこともできます。

SELECT 
    s.email,
    s.first_name, 
    s.last_name,
    s.id                AS subscriber_id,
    COALESCE(o.opens, 0)    AS opens,
    COALESCE(c.clicks, 0)   AS clicks,
    COALESCE(f.forwards, 0) AS forwards
FROM    thebookrackqccom_newsletters_subscribers AS s
  LEFT JOIN 
    ( SELECT subscriber_id,
             COUNT(*) AS opens
      FROM  thebookrackqccom_newsletters_opens 
      WHERE newsletters_opens.newsletter_id = 1
    ) AS o    ON o.subscriber_id = s.id
  LEFT JOIN 
    ( SELECT subscriber_id,
             COUNT(*) AS clicks
      FROM  thebookrackqccom_newsletters_clicks
      WHERE newsletter_id = 1
    ) AS c    ON c.subscriber_id = s.id
  LEFT JOIN 
    ( SELECT subscriber_id,
             COUNT(*) AS forwards
      FROM  thebookrackqccom_newsletters_forwards
      WHERE newsletter_id = 1
    ) AS f    ON f.subscriber_id = s.id
WHERE ( o.subscriber_id IS NOT NULL 
     OR c.subscriber_id IS NOT NULL 
     OR f.subscriber_id IS NOT NULL ) 
ORDER BY 
    s.email ASC
LIMIT 25
于 2012-06-08T08:19:20.490 に答える
0

このクエリを試してください。実行時間が短縮されることを願っています

クエリ

SELECT 
`newsletters_subscribers`.`email`,
`newsletters_subscribers`.`first_name`, 
`newsletters_subscribers`.`last_name`,
`newsletters_subscribers`.`id` AS subscriber_id,
@nopen := coalesce( N_OPEN.NOPENIDCOUNT, 000000 ) as opens,
@nclick := coalesce( N_CLICK.NCLICKIDCOUNT, 000000 ) as clicks,
@nfwd := coalesce( N_FWD.NFWDIDCOUNT, 000000 ) as forwards

FROM 
(select @nopen := 0,@nclick := 0,@nfwd :=0) sqlvars,

`thebookrackqccom_newsletters_subscribers` AS newsletters_subscribers

LEFT JOIN (SELECT `newsletters_opens`.`subscriber_id`,
COUNT(newsletters_opens.id) AS NOPENIDCOUNT 
FROM `thebookrackqccom_newsletters_opens` AS  newsletters_opens 
WHERE newsletters_opens.newsletter_id = 1) AS N_OPEN 
ON N_OPEN.subscriber_id = `newsletters_subscribers`.`id` 


LEFT JOIN (SELECT `newsletters_clicks`.`subscriber_id`,
COUNT(newsletters_clicks.id) AS NCLICKIDCOUNT
FROM `thebookrackqccom_newsletters_clicks` AS  newsletters_clicks 
WHERE newsletters_clicks.newsletter_id = 1) AS N_CLICK
ON N_CLICK.subscriber_id = `newsletters_subscribers`.`id`


LEFT JOIN (SELECT `newsletters_forwards`.`subscriber_id`,
COUNT(newsletters_forwards.id) AS NFWDIDCOUNT
FROM `thebookrackqccom_newsletters_forwards` AS  newsletters_forwards 
WHERE newsletters_forwards.newsletter_id = 1) AS N_FWD
ON N_FWD.subscriber_id = `newsletters_subscribers`.`id` 

GROUP BY `newsletters_subscribers`.`id`
ORDER BY `newsletters_subscribers`.`email` ASC
LIMIT 25
于 2012-06-08T07:34:35.847 に答える