PHP/MySQL performance issues

Discussion in 'PHP' started by pondlife, Jan 11, 2008.

  1. #1
    Hi All,

    I'm having trouble with MySQL falling over on my dedicated server and I believe it may be due to some inefficient SQL queries.

    Here's what I started with:

    $numresults=mysql_query("select * from links where catagory=".$catagory." order by linknum");
    $numrows=mysql_num_rows($numresults);
    Code (markup):
    But I realised that the 'order by' clause was unnecessary because I'm only looking for a count and I'm not interested in the actual content.

    So now I have this:

    $numresults=mysql_query("select * from links where catagory=".$catagory."");
    $numrows=mysql_num_rows($numresults);
    Code (markup):
    I've been informed that the 'select count(*)...' approach would be more efficient BUT I don't know how to extract this using PHP - can anyone help?

    EDIT: I think I've actually found a solution here: http://us3.php.net/mysql_num_rows but the question below still stands:

    I'd also like to know if there are any logs that I could look in or any MySQL tools that may help me determine exactly why MySQL keeps crashing...

    Thanks in advance,

    p.
     
    pondlife, Jan 11, 2008 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    $numresults=mysql_query("SELECT COUNT(*) FROM links where catagory=".$catagory."");

    Thats all

    Regards

    Alex
     
    kmap, Jan 11, 2008 IP
  3. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #3


    The Query above is exactly how I would do it.

    However I would make some indexes on the table. Multi index, full text and unique where possible.

    Also be sure not to store null values into the table.

    I have explained why in another post:

    http://forums.digitalpoint.com/showthread.php?t=629690
     
    LittleJonSupportSite, Jan 11, 2008 IP
  4. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #4
    $numresults=mysql_query("SELECT COUNT(*) as cn FROM links where catagory=".$catagory."");

    $array= mysql_fetch_array($numresults, MYSQL_ASSOC);

    echo $array["cn"] ;
     
    kmap, Jan 11, 2008 IP
  5. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Without indexes this query is no better then the previous one you posted in my opinion.
     
    LittleJonSupportSite, Jan 11, 2008 IP
  6. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Except that the previous one doesn't work (his '$numresults' variable was set to a result set, not the count that he was after). The new one extends the old one to the point where he can actually get the count value.
     
    TwistMyArm, Jan 11, 2008 IP
  7. pondlife

    pondlife Peon

    Messages:
    898
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks guys - that's exactly the problem I was having :)

    I'll update my scripts when I get home and see how they fair - although I've been doing some googling on the subject of MySQL performance and monitoring and the more I learn the more I feel that these simple queries aren't the issue... needless to say it's worth making these selects more efficient.

    The following document looks pretty handy and should enable me to get a handle on what's really happening: http://www.scribd.com/doc/264633/Monitoring-and-Logging-a-MySQL-Database-Server

    Oh, and littlejon - I have indexes on each table but not multiple ones - each query uses the indexes... My indexes aren't sequential tho because some rows get deleted - is this an issue?

    Cheers, p.
     
    pondlife, Jan 11, 2008 IP