Counting number of execued mysql queries per script

Discussion in 'PHP' started by Kalyse, Apr 18, 2007.

  1. #1
    Is there any way to count easily, the number of executed queries per script?

    I just want to know what sort of load there is per page on my site.

    One of my sites is Entirely driven by a database. Completely.
    I want to get an idea what sort of a problem this is, but see how many queries it does also.
     
    Kalyse, Apr 18, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    You can make your own function to run queries.

    
    function query($string, $link = false)
    {
          static $querycount = 0;
          global $querycount;
    
          $result = @mysql_query($string, $link) OR die( mysql_error() );
    
          $querycount++;
          return $result;
    }
    
    PHP:
    Now replace all mysql_query()s with query(), and echo $querycount to see how many queries you did run. I didn't try this script, but I think it should work.

    
    
    $result = query("SELECT * FROM table");
    
    echo $querycount;
    
    PHP:
     
    nico_swd, Apr 18, 2007 IP
  3. Kalyse

    Kalyse Peon

    Messages:
    1,221
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I was sort of hoping there was some function already existing, I have over 2000 queries in my sites already. Changing them would be difficult.

    Well actually... I supose I could mass preg_replace on every single file..
     
    Kalyse, Apr 18, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    I don't think there's a build-in function to do this.

    And I don't know how big your site is, or how many pages it has, but 2000 queries is pretty much. You may should work on your site structure.

    Anyway, if you have Dreamweaver, you can use its replace function. Very useful, and it also replaces strings in entire directories.
     
    nico_swd, Apr 18, 2007 IP
  5. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If it's entirely database driven with that number of queries, it would have been a good idea to have developed it with a DB abstraction layer. You could then add in a counter painlessly, with the addition of just a couple of lines of code. Obviously a bit too late now, but maybe an idea for future reference.

    As nico_swd said, there is nothing else you can do other than modify all your queries at this point.
     
    rodney88, Apr 18, 2007 IP