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 730345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:54:04+00:00 2026-05-14T06:54:04+00:00

I am trying to get do this: <?php $good_customer = 0; $q = mysql_query(SELECT

  • 0

I am trying to get do this:

<?php
  $good_customer = 0;
  $q = mysql_query("SELECT user FROM users WHERE activated = '1'"); // this gives me about 40k users

  while($r = mysql_fetch_assoc($q)){
    $money_spent = 0;

    $user = $r['user'];
    // Do queries on another 20 tables
    for($i = 1; $i<=20 ; $i++){
      $tbl_name = 'data' . $i;

      $q2 = mysql_query("SELECT money_spent FROM $tbl_name WHERE user = '{$user}'");
      while($r2 = mysql_fetch_assoc($q2)){
        $money_spend += $r2['money_spent'];
      }

      if($money_spend > 1000000){
        $good_customer += 1;
      }
    }
  }

This is just an example. I am testing on localhost, for single user, it returns very fast. But when I try 1000, it takes forever, not even mentioned 40k users.

Anyway to optimise/improve this code?

EDIT:
By the way, each of the others 20 tables has ~20 – 40k records

EDIT2:

Okay, drop the “money spend” idea. This is my current structures:

user table => user is PK

logs_week_1 table => user is FK.

logs_week_2 table => user is FK

logs_week_3 table => user is FK

… will have more logs tables in future.

I want to find the “average time” they spend on my site which the time stored in each of the logs tables.

So you guys were saying, storing the logs weekly is a bad idea? I should merge into one table?

  • 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-14T06:54:05+00:00Added an answer on May 14, 2026 at 6:54 am

    Sounds like you have a problem with your model. Why do you have 20 data-tables instead of one with a week-column?

    Then you could do a

    Select user, Sum( money_spent ) As total_money_spent
    From data
    Group By user
    

    or even

    Select Count(*) As good_customer_count
    From data
    Group By user
    Having Sum( money_spent ) > 1000000
    

    With your current structure you can only do something like this:

    Select u.user, d1.money_spent + d2.money_spent + ...
    From users u
    Join data1 d1 On ( d1.user = u.user )
    Join data2 d2 On ( d2.user = u.user )
    ...
    

    or

    Select Count(*) As good_customer_count
    From
      ( Select d1.money_spent + d2.money_spent + ... As total_money_spent
        From data1 d1
        Join data1 d1 On ( d1.user = u.user )
        Join data2 d2 On ( d2.user = u.user )
        ...
      )
    Where total_money_spent > 1000000
    

    This will certainly be faster than your current solution.


    And the time spent on a page should be stored in a numeric field.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out how best to get this PHP statement $wpdb->get_results(SELECT verb,
I don't know how to do this in php. I am trying to get
I was trying to get PJAX working with my PHP site, this is the
Been trying to get this up and running for a while now. Basically i
I've been trying to get this working for a while now and am just
I am trying to get Oracle procedures to execute from my PHP program. I
I'm trying to get (using FB PHP SDK) number of users attending an open
Trying to get this expression to work, can someone look at it and tell
Trying to get this to work, with no luck: [DataMember] public Type ParameterType {
Im trying to get this working but for some reason it's just not right.

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.