MySQL datatype for storing IP addresses


The best way to store a (version 4) IP address in a MySQL database is as an integer. This may sound strange but MySQL has two powerful functions to enable you to store an IP address as an unsigned INT. Searching an integer is much faster than searching a string and additionally integers take up less storage space which is great when working with large datasets.

mysql> SELECT INET_ATON('10.0.5.9');
---------------------
INET_ATON('10.0.5.9')
---------------------
167773449
---------------------
1 row in set (0.00 sec)

and the reverse function

mysql> SELECT INET_NTOA(167773449);
---------------------
INET_NTOA(167773449)
---------------------
10.0.5.9
---------------------
1 row in set (0.00 sec)

MySQL datatype for storing IP addresses Read More »

Speeding up Firefox on OSX

Firefox had been running really slowly lately and as I am an awful hoarder of tabs closing the application took forever. I’d heard that since version 3 there was a sql lite table that according to Google could do with vacuuming – whatever that meant.

The SQLite database uses flat files for local storage and I wondered how big and bloated they had become over time. It turned out to be much worse than I expected. To view I typed the following into a command terminal
find ~/Library/Application\ Support/Firefox/Profiles -type f -name '*.sqlite' -exec ls -arlth {} \;
My .places file was 40 megabytes which certainly wasn’t helping speed at all. The following command took care of the necessary housekeeping
find ~/Library/Application\ Support/Firefox/Profiles -type f -name '*.sqlite' -exec sqlite3 {} VACUUM \;
find ~/Library/Application\ Support/Firefox/Profiles -type f -name '*.sqlite' -exec sqlite3 {} REINDEX \;

Speeding up Firefox on OSX Read More »

Speeding up PHP FOR loops


Refactoring some client code the other day I found a lot of inefficient logic. Whilst this example is written in PHP the concept is applicable in other programming languages.

Many people use for loops in the belief that they are faster than a foreach, and while this is often the case it can be slower and less flexible if you make some simple mistakes. Consider the following:

A slow FOR loop

$text="thequickbrownfoxjumpsoverthelazydog";
for($i=0; $i < strlen($text); $i++){ // Loop through the characters
echo ( substr($text,$i,1) == 'o' ) ? "Its an o" : "Not an o"; // Display true or false as relevant
}

A faster FOR loop

$text="thequickbrownfoxjumpsoverthelazydog";
$length = strlen($text); // obtain length of the string
for($i=0; $i < $length ; $i++){ // Loop through the characters
echo ( substr($text,$i,1) == 'o' ) ? "Its an o" : "Not an o"; // Display true or false as relevant
}

The second loop is faster because it does not evaluate the string length each time the loop runs strlen($text) as in the first example. Small differences like this can have a large effect on the speed of scripts especially if there are many loops or iterations.

Speeding up PHP FOR loops Read More »

LinkedIn dynamic button rendering

It has become standard to offer sharing functionality via major platforms such as Facebook, Twitter and LinkedIn on websites. Most of the platform providers have multiple options for inserting their sharing features often including the use of an iFrame.
LinkedIn is slightly different however choosing only to offer a javascript based solution similar to the following:
<script src="//platform.linkedin.com/in.js" type="text/javascript"></script>
Above the inclusion of the required javascript function, and below the addition of sharing functionality where required.
<script type="IN/Share"></script>
When asynchronous pagination gets involved however this lack of an iFrame causes an issue: the javascript is not called as the asynchronous load takes place. This issue stumped me for for a while until I found the simple solution to call:
IN.init();
Eugene O’Neill (Web Developer for LinkedIn) has stated:

1) IN.Parse() and IN.Init() are here to stay. While we do employ the policy that any undocumented methods may or may not be supported indefinitely, these two are uniquely crucial to the functionality of the framework. The amount of work it would require to remove IN.Parse()… I don’t even want to think about it. IN.Init() is our preferred method for loading the framework asynchronously and won’t be going anywhere. Feel free to use either method.

LinkedIn dynamic button rendering Read More »

MySQL zerofill for numerical types


MySQL has a feature for numerical types known as zerofill which effects the display size of numerical types. Unlike string types, the number inside the parentheses is not the storage size in characters for the type. For numerical types the type name itself solely determines storage size.

Column Type Bytes On Disk Signed Storage Range Unsigned Storage Range
tinyint 1 byte -128 to 127 0 to 255
smallint 2 bytes -32768 to 32767 0 to 65535
mediumint 3 bytes -8388608 to 8388607 0 to 16777215
int 4 bytes -2147483648 to 2147483647 0 to 4294967295
bigint 8 bytes -9223372036854775808 to 9223372036854775807 0 to 18446744073709551615

So zerofill with default width – the same as int(10):
mysql> create table test_table (t int zerofill);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into test_table set t = 10;
Query OK, 1 row affected (0.02 sec)

mysql> select * from test_table;
+————+
| t |
+————+
| 0000000010 |
+————+
1 row in set (0.08 sec)

And without zerofill:
mysql> create table test_table (t int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test_table set t = 10;
Query OK, 1 row affected (0.01 sec)

mysql> select * from test_table;
+——+
| t |
+——+
| 10 |
+——+
1 row in set (0.01 sec)

A common usage for is creating invoice ids such as INV00000945.

Notes:
If you do not have zerofill specified there is no difference between int(3) and int(10)
When doing comparisons: if compared as integers then the values are the same; if you compare as strings the values are different.

MySQL zerofill for numerical types Read More »

Using Google to host jQuery


Using Google to host your jQuery (or other applicable content) has several advantages over hosting a version on your own server including better caching, decreased latency, and increased parallelism. There are plenty of other articles to discuss the merits of decreased latency benefits of a CDN or the effects of parallelism on browsers so it wont be covered here.

Caching
Perhaps the most compelling reason to use Google to host your jQuery is that your users actually may not need to download it at all. If you’re hosting jQuery locally then your users must download a copy at least once. Your users probably already have multiple identical copies of jQuery in their browsers cache from other sites, but those copies are ignored when they visit your site.
When a browser sees references to a CDN-hosted copy of jQuery it understands they all refer to exactly same file. The browser trusts that those files are identical and wont re-request the file if it’s already cached.

Execution
So, now that we know that Google is a good place to serve up our jQuery from how are we going to do it? I believe the best way is also the simplest:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.7.1.min.js"><\/script>')</script>

This will point to Google to host the jQuery library, but fall back to local hosting in the event of connectivity issues.

Using Google to host jQuery Read More »

08/02/2012 Emergency PHP Updates on server

EMERGENCY PHP UPDATES

We will be applying a PHP update to the servers later this evening.

This will take the primary PHP install from v5.3.9 to v5.3.10 and will resolve a vulnerability that has been patched in the 5.3.10 release.

The vulnerability present in 5.3.9 allows for the possibility of remote code execution depending on how large numbers and arrays are used.

As this is a security issue we will be performing emergency maintenance between midnight and 3am (GMT).

There should be no interruption to service however, as the work is being done on components critical to PHP based sites, this period should be considered at risk.

The work will cause slightly higher than normal load on the servers and may involve a restart of the web server.

In the worst cast scenario (such as a failed compile) the installer will restore a backup of the current working configuration.

No module or configuration changes will be made to the PHP stack and scripts should notice no difference.

You can view the change log on the php.net site: http://php.net/ChangeLog-5.php

08/02/2012 Emergency PHP Updates on server Read More »