Any difference between the two, int terms of speed/performance?
$sql = "SELECT * "
. "FROM `myTable` "
. "WHERE `id` = 4";
$sql = "SELECT *
FROM `myTable`
WHERE `id` = 4";
Any difference between the two, int terms of speed/performance?
$sql = "SELECT * "
. "FROM `myTable` "
. "WHERE `id` = 4";
$sql = "SELECT *
FROM `myTable`
WHERE `id` = 4";
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 ^^
i think the latter is faster. because you have used the concatenation operator in the former which parsing it may take some time.
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
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.