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

  • Home
  • SEARCH
  • 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 217221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:38:13+00:00 2026-05-11T18:38:13+00:00

Ok, here’s a query that I am running right now on a table that

  • 0

Ok, here’s a query that I am running right now on a table that has 45,000 records and is 65MB in size… and is just about to get bigger and bigger (so I gotta think of the future performance as well here):

SELECT count(payment_id) as signup_count, sum(amount) as signup_amount
FROM payments p
WHERE tm_completed BETWEEN '2009-05-01' AND '2009-05-30'
AND completed > 0
AND tm_completed IS NOT NULL
AND member_id NOT IN (SELECT p2.member_id FROM payments p2 WHERE p2.completed=1 AND p2.tm_completed < '2009-05-01' AND p2.tm_completed IS NOT NULL GROUP BY p2.member_id)

And as you might or might not imagine – it chokes the mysql server to a standstill…

What it does is – it simply pulls the number of new users who signed up, have at least one “completed” payment, tm_completed is not empty (as it is only populated for completed payments), and (the embedded Select) that member has never had a “completed” payment before – meaning he’s a new member (just because the system does rebills and whatnot, and this is the only way to sort of differentiate between an existing member who just got rebilled and a new member who got billed for the first time).

Now, is there any possible way to optimize this query to use less resources or something, and to stop taking my mysql resources down on their knees…?

Am I missing any info to clarify this any further? Let me know…

EDIT:

Here are the indexes already on that table:

PRIMARY PRIMARY 46757 payment_id

member_id INDEX 23378 member_id

payer_id INDEX 11689 payer_id

coupon_id INDEX 1 coupon_id

tm_added INDEX 46757 tm_added, product_id

tm_completed INDEX 46757 tm_completed, product_id

  • 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-05-11T18:38:13+00:00Added an answer on May 11, 2026 at 6:38 pm

    Those kinds of IN subqueries are a bit slow in MySQL. I would rephrase it like this:

    SELECT COUNT(1) AS signup_count, SUM(amount) AS signup_amount
    FROM   payments p
    WHERE  tm_completed BETWEEN '2009-05-01' AND '2009-05-30'
    AND    completed > 0
    AND    NOT EXISTS (
               SELECT member_id
               FROM   payments
               WHERE  member_id = p.member_id
               AND    completed = 1
               AND    tm_completed < '2009-05-01');
    

    The check ‘tm_completed IS NOT NULL‘ is not necessary as that is implied by your BETWEEN condition.

    Also make sure you have an index on:

    (tm_completed, completed)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here total two union binding three queries, first query retrieving records from a virtual
Here is a snippet of my JavaScript Code that I want to DRY up:
Here is my T-SQL query SELECT ProductID, VendorID, ProductName= MAX(ProductName), VendorName = MAX(VendorName), IsActive
here is my problem: I have a jList and a popup menu. When I
here's the code: #include <string> class Config { public: static const std::string asdf =
Here's my simple first attempt at a python extension using boost. Can someone help
Here is an excerpt from Mixed Effects Models in S and S-Plus page 238
here's what I've got so far - i really need to ban any tags
Here's my scenario: $foo = bar; one() { two() { three() { // need
Here's how standard section header views handle scrolling/overlaping: I'm trying to replicate the same

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.