Any way to make less queries??

Discussion in 'MySQL' started by jkashu, May 28, 2008.

  1. #1
    I have a database of users. I need to display the number of signups during different time periods:

    today - 100 signups
    this week - 1000 signups
    this year - 100000 signups
    etc.

    Is there any way to do this without querying the database for each time period? Is it ok to query so many times?

    The database has tens of thousands of rows.
     
    jkashu, May 28, 2008 IP
  2. graham23s

    graham23s Well-Known Member

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #2
    either way you do it, you would NEED to query the database at one point to see the results?!

    Graham
     
    graham23s, May 28, 2008 IP
  3. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I would do one query to get the "TOTAL" then just do simple date/time functions to figure out signups with each portion.

    Personally hitting the db for those are very quick (provided you have decent indexing setup)

    They are one line instant queries.

    Going back to my first thought to avoid hitting the db just read they entire results into an array with a date stamp. Then count through each month, day & year to come up with your totals.

    I think the 3 queries would be less taxing on the system then the building this count function.

    Jon
     
    LittleJonSupportSite, May 28, 2008 IP
  4. flippers.be

    flippers.be Peon

    Messages:
    432
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You need at least these 3 queries..
    a database is made for sorting and counting, so it will be much faster than anything you try to write yourself (ie get all rows and make a count function yourself in perl/scripting/..)

    just make sure there is an index on the signup time

    the query will run for each hit but the database should cache the result

    if you do notice it delays the generating of the page, and as these are numbers don't really change a lot, you could make a seperate table to keep these numbers.. store the 3 values, and change the signup-page so it increases them when someone signs up.
    That way you only need to read the 3 values out of the table and the database never has to do a count() anymore.. this will be the quickest way.
     
    flippers.be, May 29, 2008 IP
  5. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    
    SELECT
      SUM(CASE WHEN signupdate = CURDATE() THEN 1 ELSE 0) AS numtoday,
      SUM(CASE WHEN WEEK(signupdate) = WEEK(CURDATE()) THEN 1 ELSE 0) AS numthisweek,
      SUM(CASE WHEN YEAR(signupdate) = YEAR(CURDATE()) THEN 1 ELSE 0) AS numthisyear
    FROM tablename
    
    Code (markup):
     
    CreativeClans, May 29, 2008 IP
  6. jkashu

    jkashu Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ok. Modifying the above snippett of code, this what I came up with:

    // Get clicks
    $clicks = mysql_query("SELECT
    					  SUM(CASE WHEN clickdate = CURDATE() THEN 1 ELSE 0) AS 'numtoday',
    					  SUM(CASE WHEN WEEK(clickdate) = WEEK(CURDATE()) THEN 1 ELSE 0) AS 'numthisweek',
    					  SUM(CASE WHEN MONTH(clickdate) = MONTH(CURDATE()) THEN 1 ELSE 0) AS 'numthismonth',
    					  SUM(CASE WHEN YEAR(clickdate) = YEAR(CURDATE()) THEN 1 ELSE 0) AS 'numthisyear'
    					  FROM clicks WHERE affiliateID = '$affiliateID'");
    
    // Check for errors
    if(!$clicks){ mysql_error(); }
    
    // Gets results from database
    while($row  = mysql_fetch_array($clicks, MYSQL_ASSOC)){
    
    	$clicksDay = $row['numtoday'];
    	$clicksWeek = $row['numthisweek'];
    	$clicksMonth = $row['numthismonth'];
    	$clicksYear = $row['numthisyear'];
    	
    	
    }
    Code (markup):
    But it tells me that:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
     
    jkashu, May 29, 2008 IP