2

Any difference between the two, int terms of speed/performance?

$sql = "SELECT * "
     . "FROM `myTable` "
     . "WHERE `id` = 4";



$sql = "SELECT *
        FROM `myTable`
        WHERE `id` = 4";
3
  • You probably shouldn't worry because YAGNI. Concentrate on optimizing your SQL rather than the strings that make it. Commented Aug 6, 2009 at 19:55
  • Thanks guys. The issue wasn't really sql/db related. That was just the example I was using, because I like to line up my sql statements like that. If the difference is small, then I don't really care, but I just wanted to make sure one wasn't a LOT slower than the other. Thanks Commented Aug 6, 2009 at 20:33
  • For SQL queries, I like using HEREDOC syntax ; see php.net/manual/en/… ; I like the fact that you don't have to escape double-quotes ; and I find those easier to notice than normal strings. Only problem is : you can't indent :-( Commented Aug 6, 2009 at 20:53

4 Answers 4

6

Maybe a very very very small difference, the first one probably being a bit slower (because of concatenations)...

... But the time taken to execute your single simple SQL query will be thousands (maybe hundreds, with a simple query -- just a wild guess, but you'll see the point) of times more important than that very small difference !

So, you really shouldn't bother about that kind of "optimizations", and consider/choose what is the most easy to both write/read/understand and maintain.


EDIT : just for fun, here are the opcodes that are generated for the first portion of code :

$ php -dextension=vld.so -dvld.active=1 temp-2.php
Branch analysis from position: 0
Return found
filename:       /home/squale/developpement/tests/temp/temp-2.php
function name:  (null)
number of ops:  6
compiled vars:  !0 = $sql
line     #  op                           fetch          ext  return  operands
-------------------------------------------------------------------------------
   5     0  EXT_STMT
         1  CONCAT                                           ~0      'SELECT+%2A+', 'FROM+%60myTable%60+'
         2  CONCAT                                           ~1      ~0, 'WHERE+%60id%60+%3D+4'
         3  ASSIGN                                                   !0, ~1
   8     4  RETURN                                                   1
         5* ZEND_HANDLE_EXCEPTION

And, for the second one :

$ php -dextension=vld.so -dvld.active=1 temp-2.php
Branch analysis from position: 0
Return found
filename:       /home/squale/developpement/tests/temp/temp-2.php
function name:  (null)
number of ops:  4
compiled vars:  !0 = $sql
line     #  op                           fetch          ext  return  operands
-------------------------------------------------------------------------------
   7     0  EXT_STMT
         1  ASSIGN                                                   !0, 'SELECT+%2A%0A++++++++FROM+%60myTable%60%0A++++++++WHERE+%60id%60+%3D+4'
   9     2  RETURN                                                   1
         3* ZEND_HANDLE_EXCEPTION

So, yes, there is a difference... But, still, what I said before is still true : you shouldn't care about that kind of optimization : you'll do so many "not-optimized" stuff in the other parts of your application (or even if the configuration of your server) that a small difference like this one means absolutly nothing


Well, except if you are google and have thousands of servers, I guess ^^

Sign up to request clarification or add additional context in comments.

Comments

2

To test this kind of stuff, you use a big while loop and run the code over-and-over to compare. Do this twice (or more) to compare operations. Run it a few dozen times and track the results.

ob_start();
$t = microtime(true);
while($i < 1000) {
    // CODE YOU WANT TO TEST

    ++$i;
}
$tmp = microtime(true) - $t;
ob_end_clean();

echo $tmp

Comments

0

i think the latter is faster. because you have used the concatenation operator in the former which parsing it may take some time.

Comments

0

I think that the first one might take a little longer because of concatenation, but, it is not going to be much of a difference. If you are worried about optimization you can start by using stored procedures in your database instead of writing the SQLs in your php code. This will not only increase speed but also security.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.