Why does a query on the mysql server itself run quicker than in php..

Discussion in 'Databases' started by qazwsxed, Apr 5, 2011.

  1. #1
    Hi Guys,

    Just wondering..

    Why is it that when you ( mysql -uUSERNAME -pPASSWORD )
    and then run a query.
    Its like UBER quick and uses hardly any CPU.. ( 0.00 sec )

    However the same query thats run through PHP takes like 1second and uses 99% cpu when it happens?

    Ive just noticed this and wonders if these some odd setting in php misconfigured?

    Thanks
     
    qazwsxed, Apr 5, 2011 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    It's tough to answer this question. You first need to benchmark the php script to see where the bottleneck is. It could be the query, but it could also be outputting to the browser or some other php process.

    Generally, the speed of a query run from command line should be roughly equivalent to the speed of a query run from php or any other program. The time differences would occur where php has to process additional logic or render to the browser. If the database is not on the same server as the website, there is definitely going to be latency in communicating with the database server.
     
    jestep, Apr 6, 2011 IP
  3. Tomislav Rupcic

    Tomislav Rupcic Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    try to run your query from command prompt ( or shell ) ... if it runs faster then u know that its browser rendering that uses cpu :p
     
    Tomislav Rupcic, Apr 12, 2011 IP
  4. shield

    shield Active Member

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #4
    Can you post the PHP code so it would be easier to see if there are any bottlenecks?
     
    shield, Apr 13, 2011 IP