PHP Optimizations

PDO (http://www.php.net/pdo) is a must! Speeds up database
transactions

1) Store as much data as possible into database
2) Store data as arrays for each updating and fetching (see serialize)
3) Always use an index field in mysql
4) Limit echos/prints

Most Important: If connecting to pages (ex. API calls) use CURL Multi
Connects
(http://www.ibuildings.com/blog/archives/811-Multithreading-in-PHP-
with-CURL.html) (use 2nd example)

Also try to make use of Forking (http://immike.net/blog/2007/04/08/
fork-php-and-speed-up-your-scripts/)

MYSQL Optimizations

1) Use BIGINT 255 unsigned fields for ids or anything that could get
really large
2) Remember OPTIMIZE


Finally, if you are storing a lot of data. Do the following:
Take the ID (index), hopefully not a number, and md5 it.
Then store into a specific table based on the first letter of the md5.
Example:

$user = "fastest963"; //store user
$let = md5($user); //md5 username
"SELECT * FROM".$let[0]."users WHERE `username` = '$user' "; //$let[0]
is the first letter of the md5

Tables would be created like this:
First make "0users" table. Then copy and make 1-F tables.
0users, 1users, 2users, 3users, 4users, 5users, 6users, 7users,
8users, 9users, ausers, busers, cusers, dusers, eusers, fusers
Those would be all the table names, thus splitting your data per table
by 16.


I hope that helps people out when optimizing their scripts. These have
allowed me to process 2000+ tweets per sec via PHP. Any questions?
Feel free to comment!

Thanks,
James Hartig

Reply via email to