OK... This is my second entry on this blog. And it is about something I want every mysql/php newbie to know at the starting gate:
DON'T USE CHAR(15) FOR IP Address fields in mysql!
Well... what about the periods in ip addresses, you say?
Well... mysql has a FANTASTIC way of solving this issue! Convert the standard ip address into an unsigned integer!
ok... at first it sounds a little cryptic, but say it at least 3 times before your morning coffee:
INET_ATON
INET_NTOA
loosely meaning....
INET_ATON = Internet address to number
INET_NTOA = Internet number to address
SO... try this:
mysql> SELECT INET_ATON('192.168.1.5');
+--------------------------+
| INET_ATON('192.168.1.5') |
+--------------------------+
| 3232235781 |
+--------------------------+
1 row in set (0.04 sec)
Now, let's reverse it!
mysql> SELECT INET_NTOA(3232235781);
+-----------------------+
| INET_NTOA(3232235781) |
+-----------------------+
| 192.168.1.5 |
+-----------------------+
1 row in set (0.01 sec)
Fun, eh?
Now... what are the savings on this, you ask?
On a 17 million row table consisting only of an auto_increment id (another must; don't leave home without an auto_increment id!!), non-indexed exact searches are about the same. In fact, the searches on the char column sometimes perform up to 10% faster.
After indexing, exact searches are once again the same, although they take a fraction of the time they did when they were not indexed. However, where the crown jewel shines is on ranges. On a count of an ip range, on the int based table, the cost was 3.78 seconds, but on the char(15) based table, the cost was 26.21 seconds.
Also -- the size of the int based data table (MyISAM) is 148M compared to 329M on the char(15) based table.
Are you convinced now? :) If so, now you know what you're going to do!
Until next time (whenever that will be),
David
No comments:
Post a Comment