Closed Thread
Results 1 to 4 of 4

Thread: Help me optimise my query please!

  1. #1
    adzeds is offline Newbie
    Join Date
    Oct 2009
    Location
    Bristol, UK
    Posts
    27
    Rep Power
    0

    Help me optimise my query please!

    Ok. The gist:
    I am writing a small app that collects webstats. 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)

    Code:
    //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"); 

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,494
    Blog Entries
    75
    Rep Power
    143

    Re: Help me optimise my query please!

    What is the purpose of having both a datetime and distinct date and time fields?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    adzeds is offline Newbie
    Join Date
    Oct 2009
    Location
    Bristol, UK
    Posts
    27
    Rep Power
    0

    Re: Help me optimise my query please!

    It just made things a little easier for querying.

    Am I doing this wrong?

  5. #4
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: Help me optimise my query please!

    you have built in functions in MySQL to return the time or the date from a datetime field, that can be used easier than doublestore like this, they are time() and date().

    MySQL has very good support to perform many different tasks for dates and times built in, see MySQL :: MySQL 5.0 Reference Manual :: 11.6 Date and Time Functions for a list and explanation.

    I guess your php-part in bottom can be replaced with a single SQL query
    I haven't tested it, but something like this should do the job:

    Code:
    SELECT count(ipaddress) AS visits FROM analytics WHERE ipaddress='$ip' AND
    time(now()) < timeadd(time(`timefield`), "00:30:00"))
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. 1 query vs multiple query
    By Vaielab in forum Database & Database Programming
    Replies: 1
    Last Post: 08-30-2011, 12:16 PM
  2. What is the query?
    By Tank314 in forum Database & Database Programming
    Replies: 4
    Last Post: 06-19-2011, 04:53 AM
  3. SQL query in vb6 Help
    By avosoft in forum Visual Basic Programming
    Replies: 5
    Last Post: 05-06-2010, 04:12 AM
  4. SQL query
    By chili5 in forum PHP Development
    Replies: 18
    Last Post: 04-03-2009, 08:07 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts