Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8084109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:41:22+00:00 2026-06-05T17:41:22+00:00

I have a query that basically combines tables of actions and selects from them

  • 0

I have a query that basically combines tables of actions and selects from them in chronological order while preserving pagination..

Is there a more efficient / better way to do this? The query takes 3 seconds. Not terrible.. but I think there is room for improvement and I will be using it alot..

Thanks!

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:41:25+00:00Added an answer on June 5, 2026 at 5:41 pm

    What you need is indexes that the query can use. A compound index on (newsletter_id, subscribe_id) on each one of the three tables would help.

    You can also rewrite the query like this:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that, basically, says this: SELECT DISTINCT [DB_ID] FROM [TableX] WHERE
I currently have a query that is built based on order details passed from
I have a query that is basically like this: SELECT foo FROM bar where
I have a query that is more complicated, but basically creates an HTML page
I have a method that basically returns the results of a mysql query. public
I have a query that looks a bit like this: SELECT weekEnd, MAX(timeMonday) FROM
I have a linq query that is causing some timeout issues. Basically, I have
I have this query that works: select name, location_id from all_names where location_id in
I have a query that's basically count all the items of type X, and
I have a query that looks like this: var TheQuery = (from.... where x.TheDate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.