Tag Archives: optimize

PHP: Optimize and Analyze All Your MySQL Tables CodeUnit 19 MAR 2010

20090910-MySQL-logoWhen you start working with sizeable datasets and the like, things like database optimization become more and more important for you to pay attention to.

So for today’s quick tutorial I present to you a script which can be used as either a cron or scheduled task and will run MySQL’s nifty analyze and optimize functions against all the tables in your database. Looking at it, you’ll see it is a pretty simple affair, basically we connect to our database in the usual fashion (note I’m simply using a connect function of my own design here) and then run a query to return the list of all tables in the selected database (again obscured by the connect function).

Once you have the list, it is a simple matter of looping through all the tables and executing the ANALYZE and OPTIMIZE statements against each table.

$conn = createconnection();

$alltables = mysql_query("SHOW TABLES");
while ($table = mysql_fetch_assoc($alltables))
{
   foreach ($table as $db => $tablename)
   {
       mysql_query("ANALYZE TABLE `".$tablename."`")
       or die(mysql_error());
       mysql_query("OPTIMIZE TABLE `".$tablename."`")
       or die(mysql_error());
   }
}
closeconnection($conn);

Simple, but effective.

Optimize Your Javascript by Minifying with Google’s Closure Compiler CodeUnit 02 MAR 2010

Optimize your Javascript by minifying with Google’s Closure Compiler. Well, that pretty much says it all. By now we all no that there is plenty of scope for reducing the size of one’s Javascript code by replacing bulky variable names with shorter versions and stripping out whitespace, etc., but naturally as one would expect, achieving this optimization by hand is a rather tiresome affair.

Enter the nifty Google Closure Compiler, simply put, a tool for making Javascript download and run faster. It’s not a traditional code compiler mind you, it doesn’t compile source code into machine code but rather compiles Javascript to better Javascript, analyzing, clearing dead code, and rewriting and minimizing what’s left over. It checks syntax, variable references, types and even warns about common javascript pitfalls just for fun.

There are a number ways in which you can set the compiler loose on your code: you can use the open source java command line application, you can simply plug your script into the simple online web application or you can even make use of their full RESTful API.

The benefits of using this great little system do of course not need that much explanation. Firstly, in terms of efficiency, using the Closure Compiler will result in smaller javascript files which in turn means faster loading applications which obviously means reduced bandwidth needs. In terms of code checking, Closure Compiler provides warnings for illegal javascript as well as for potentially dangerous operations, resulting in less buggy scripts and scripts that are simply easier to maintain.

And just in case you were wondering why you should give them a spin, take note that jQuery have moved to the Closure Compiler to produce their minified scripts.

So what are you waiting for? ;)

Related Link: http://code.google.com/closure/compiler/