View Full Version : Which query is much faster?

07-30-2008, 11:16 AM
Which query would be faster and why?

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


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


07-30-2008, 11:21 AM
The second but there wouldn't be enough difference for you or anyone else to notice.

It would be slightly faster simply because PHP only has to assign the $id variable instead of parsing the entire string in the first example to determine what may need to be assigned.

07-30-2008, 03:48 PM
djm is right, the second is faster but not by a noticeable amount of time. If you're running the query inside of a loop, you might notice the difference, but at that rate you should just be using prepared statements.

07-30-2008, 03:54 PM
The queries are identical. (???)

I believe your question was meant to be about the PHP code execution time, in which the second example would be negligibly faster as it's not looking for any variables to parse within double-quotes.

...but not much faster, as your question suggests. We're talking nanoseconds here.

07-30-2008, 04:15 PM
Which query is much faster? I'm with kbluhm. The queries are identical and the php parsing time is almost identical, so there is nothing in either method that is "much" faster than the other.

07-31-2008, 01:37 AM
Hopping on the bandwagon with kbluhm and CFMaBiSmAd.

07-31-2008, 01:57 AM
you run this in a loop and try to decide which one to use?

just curios, :)

PS: agree with previous posters about speed, :)


07-31-2008, 02:12 AM
The second is (minimally) faster. The first is in a parsable string format ("") while the second is only a string (''). The first would require the parser to parse the entirety of the query for variable replacement, while the second can ignore it.
In either case, you will not notice the speed difference. You're queries can be altered to make a more significant speed improvement by targeting which fields to query instead of a wildcard. The SQL itself will have no speed differential.