Help reduced query size!!!

Discussion in 'MySQL' started by adzeds, Feb 1, 2010.

  1. #1
    Ok. The gist:
    I am writing a small app that collects web stats. I am focusing on the bit that works out return visits by judging if 30 minutes past since they last accessed a page.

    I am searching the database for distinct IP address, then comparing the time field to their last accessed time. I have a field that stores a DATETIME of access and also one for DATE and TIME broken down individually.

    Feel free to ask any questions, here is my code (it is probs awful)

    //return visits
            $Query = "SELECT DISTINCT ipaddress from analytics";
            $Result = mysql_query( $Query );
            $num = mysql_num_rows($Result);
            $uniques = mysql_num_rows($Result);
            $return[1] = array("title" => "Unique Visits", "value" => "$uniques");    
            
            $visitcount = 0;
                    
            $i=0;
            while ($i < $num) {
            $ipaddress[] = mysql_result($Result,$i,"ipaddress");
            $i++;
            }
            
            $a=0;
            while ($a < $num) {
            $Query = "SELECT time from analytics WHERE ipaddress = '" . $ipaddress[$a] . "'";
            $Result = mysql_query( $Query );
            $num2 = mysql_num_rows($Result);
            
                $b = 0;
                while ($b < $num2) {
                $accessed = mysql_result($Result,$b,"time");
                    
                
                
                if (isset($lastaccessed)) 
                {
                    $temptime = strtotime("30 minutes", $lastaccessed);
                    if($accessed > $temptime) 
                    {
                    $visitcount++;
                    $pagesvisited[$b]++;
                    } else {
                    $pagesvisited[$b]++;
                    }
                    
                }
                else     
                {
                $lastaccessed = $accessed;
                $visitcount++;
                $pagesvisited[$b]++;
                } 
                $b++;
                }
                
            $a++;
            }
            
            $return[2] = array("title" => "Visits", "value" => "$visitcount");  
    PHP:
     
    adzeds, Feb 1, 2010 IP
  2. swarg

    swarg Peon

    Messages:
    105
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What do you mean "query size"? It looks short for me. Query execution time maybe?
     
    swarg, Feb 1, 2010 IP
  3. systematical

    systematical Peon

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    answer the posters question above.

    One thing too look into involving optimizing your query execution time is adding indexes. Don't just blindy add indexes, a good place to start for someone like you asking such a basic question (no offense) would be to index an date/time columns.
     
    systematical, Feb 2, 2010 IP
  4. adzeds

    adzeds Well-Known Member

    Messages:
    1,209
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    100
    #4
    Yeah, I meant query execution time!

    Anyone have any good links to learn about indexing columns?
     
    adzeds, Feb 2, 2010 IP
  5. systematical

    systematical Peon

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    you could try googling the topic. You could also post structure of your table, the database engine type (you're likely using MyISAM), any relations to other tables the table in question has, and what your current indexes are. I could give you a crash course based on that.

    Also, try running this query:

    
    OPTIMIZE TABLE `your_table_name_here`  
    
    Code (markup):
    and post the output.
     
    systematical, Feb 2, 2010 IP