Tip to Speeding Up PHP Code - Consolidate Queries

Tip to Speeding Up PHP Code - Consolidate Queries

Queries which we are using in our scripts may decrease the our speed of the site. Here is the example to speed up our php code.

There are two ways of handling these queries. We can execute each one with a separate call to the appropriate _query() function or we can concatenate them into one string and send them all with one call to the _query() function, cutting down some overhead that occurs with each call to the _query() function.

The following code block connects, selects a database, and executes 200 queries one at a time with individual calls to the query() function.

$link = mysql_connect(”localhost”, “root”, “”)

or die(”Could not connect”);

mysql_select_db (”dbname”);

for ($i = 0; $i < 200; $i++) {

$sql = “update user set rank=’$i’ where uid = ‘5003′ “;

mysql_query($sql);

}

mysql_cose($link);

The block below connects, selects a database, and creates a single string composed of 200 queries. It then sends that string to the server in one call to the query() function.

$link = mysql_connect(”localhost”, “root”, “”)

or die(”Could not connect”);

mysql_select_db (”dbname”);

for ($i = 0; $i < 200; $i++) {

$sql = “update user set rank=’$i’ where uid = ‘5003′ “;

}

mysql_query($sql);

mysql_cose($link);

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.