Which query is much faster?

Discussion in 'PHP' started by cancer10, Jul 30, 2008.

  1. #1
    Which query would be faster and why?

    
    mysql_query("select * from mytable where id=$id");
    Code (markup):

    OR


    
    mysql_query('select * from mytable where id='.$id);
    Code (markup):

    Thanx
     
    cancer10, Jul 30, 2008 IP
  2. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    the second, because php doesnt try to evaluate all of the code within ''
     
    matthewrobertbell, Jul 30, 2008 IP
  3. koolman

    koolman Peon

    Messages:
    76
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Agree that the second is faster
     
    koolman, Jul 30, 2008 IP
  4. andrewgjohnson

    andrewgjohnson Active Member

    Messages:
    180
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #4
    The query is the same...

    MySQL will return the results from both at the same speed. However PHP will execute the second command faster.

    If you're trying to optimize you should stay away from "SELECT *" queries. Only SELECT the columns you need.
     
    andrewgjohnson, Jul 30, 2008 IP
  5. Pos1tron

    Pos1tron Peon

    Messages:
    95
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    0.000021, 0.000022, 0.000019 <-- query one, repeated 3 times
    0.000017, 0.000017, 0.000017 <-- query two, repeated 3 times

    The second one is faster by at least 0.000002 seconds (seemingly more like 0.000004 the majority of the time)! It's only a tiny bit quicker, but it's still quicker :)

    (These times don't include the time for an actual query, but they still give the same difference in speed whether they actually query or not).
     
    Pos1tron, Jul 30, 2008 IP
  6. andrewgjohnson

    andrewgjohnson Active Member

    Messages:
    180
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #6
    Did you test those in MySQL? Please show how you tested this...
     
    andrewgjohnson, Jul 30, 2008 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    ^ This makes no difference. PHP will generate (parse) the string, and THEN pass it to MySQL. So the MySQL speed will be identical...
     
    nico_swd, Jul 30, 2008 IP
  8. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #8
    The difference will not be noticeable.

    Peace,
     
    Barti1987, Jul 30, 2008 IP
  9. Cri2T

    Cri2T Peon

    Messages:
    104
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I (and several other people around the web) have written multiple articles on Optimizing PHP. Here's a link to my most up-to-date article:

    http://www.sacredgfx.com/show_article.php?id=9

    As for why it's faster: PHP searches for Variables in the double quotes: ie: echo("blah blah $blah"); will echo out whatever $blah has in it; and echo('blah blah $blah'); will display the $blah as plain text rather than the variable data. The same applies for mysql_query() and pretty much any other function in PHP.

    As for how to test speed:
    the top of my article has a very basic, yet very effective way to benchmark your scripts.


    Hope this helps.


    Also: azizny, that's a bad attitude to have; it may not be noticeable right now, but down the line if his site becomes super popular, those loading times start multiplying themselves by the amount of users concurrently loading the page. Also, it's not like he has to jump through loops to optimize it, and proper PHP is all about properly optimizing and using the least amount of code possible.
     
    Cri2T, Jul 30, 2008 IP
  10. payu

    payu Peon

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    agreed ... the second one is faster ... in the php interpretation process
    mysql query is the same
     
    payu, Jul 30, 2008 IP
  11. Pos1tron

    Pos1tron Peon

    Messages:
    95
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Well what I did was this:

    For the first query, I wrote a file containing this, and opened it 3 times.
    <?php
    require_once '/home/*username*/php/Benchmark/Timer.php'; // Load PEAR Benchmark
    
    $timer =& new Benchmark_Timer(true);
    
    $timer->start();
    
    $id = 1;
    $query = "select * from mytable where id=$id";
    unset($id);
    
    $timer->stop();
    
    echo ($timer->timeElapsed());
    ?>
    PHP:
    For the second query, I wrote a file containing this, and opened it 3 times.
    <?php
    require_once '/home/*username*/php/Benchmark/Timer.php'; // Load PEAR Benchmark
    
    $timer =& new Benchmark_Timer(true);
    
    $timer->start();
    
    $id = 1;
    $query = 'select * from mytable where id='.$id;
    unset($id);
    
    $timer->stop();
    
    echo ($timer->timeElapsed());
    ?>
    PHP:
    The $timer is done via the Benchmark module of PEAR.
     
    Pos1tron, Jul 31, 2008 IP
  12. andrewgjohnson

    andrewgjohnson Active Member

    Messages:
    180
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #12
    Okay, ya my question should have been phrased as: "Where you testing the speed of the PHP script or the MySQL query?" It was PHP as I suspected, just wasn't sure. Thanks for the clarification.
     
    andrewgjohnson, Jul 31, 2008 IP
  13. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Its PHP and not mysql. Sorry


    thanx
     
    cancer10, Jul 31, 2008 IP