In almost all cases, the database is what needs optimizing, not your script. Remove dead records and optimize your tables often; set up indexes for large, high volume queries; and make sure that you're not using a series of inefficient queries when one query will do. Also, use the smallest field size possible for each piece of data, and stay away from variable length fields unless you really have to use them. If you do use them, always put them at the end of the record, and be sure to optimize your tables even more often to remove dead space.