IP Address Database Storage

Register an Account
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 04-09-2007, 05:41 PM
MicroBerto's Avatar
Casual Pligger
 
Join Date: Oct 2006
Posts: 44
Hi all,

We have seen some issues with some database tables (mainly pageviews) getting too large and out of control. Some fixes have been made and helped, but I'd like to go further and discuss IP Address Storage.

Currently, Pligg uses VARCHARs to store IP address. This means that, at worst, each IP address is going to store 15 bytes of data (123.567.901.345 if you count numbers and dots). This adds up to a LOT of wasted space when we log every single profile/story/out pageview.

What is often done instead of this is to use INT UNSIGNED, and use built-in functions to do conversions to integer for us. This takes up 4 bytes -- significant savings.

The functions:
  1. INET_ATON()

    Let's say we get an IP Address: $ip_address = $_SERVER['REMOTE_ADDR'];

    When submitting this into the database, what you need to do is this:

    PHP Code:
    $query "INSERT INTO votes (vote_ip) VALUES (INET_ATON('$ip_address'))"
    And then the string IP gets formatted into UINT
  2. INET_NTOA()

    Now we need to get it out.

    PHP Code:
    $query "SELECT INET_NTOA(pv_user_ip) FROM pageviews WHERE pv_type = 'profile' DESC LIMIT 1"// (or whatever...)
    $result $db->query($query); // or however you do your queries 
    And now $result will have the text format back to you.

Files that would need to be changed:
  1. live2.php
  2. out.php
  3. register.php
  4. possibly story.php and user.php (they don't seem to be writing to the DB, just checking an IP)
  5. install/installtables.php
  6. install/upgrade.php (this might take some functions to run through all stored IP addresses, get them into an array, change the table, reformat the IP, and store them back in)
  7. libs/votes.php
  8. libs/pageview.php

References:
  1. MySQL Misc Functions
  2. Arjen's Journal - Storing IP Addresses

At a savings of up to 11 bytes per every vote and every time someone clicks on something, I think this is worth the work.

Let me know what you think, I'm willing to help with most of these.

Cheers,
berto
Reply With Quote
  #2 (permalink)  
Old 04-09-2007, 06:39 PM
Pligg Developer/Admin
Pligg Version: 0
Pligg Template: none
 
Join Date: Jun 2006
Location: Muncie, Indiana
Posts: 3,215
Thanks for this. You made it easy for the devs to make the changes needed. Now if all the threads were like this :P
Reply With Quote
  #3 (permalink)  
Old 04-09-2007, 06:46 PM
Casual Pligger
 
Join Date: Mar 2007
Posts: 64
Also, turn the MySQL cache off for the pageview table. This is very important from a performance standpoint.
Reply With Quote
  #4 (permalink)  
Old 04-10-2007, 12:31 AM
dollars5's Avatar
Pligg Donor
 
Join Date: Dec 2006
Location: India
Posts: 1,960
Thanks m8, saving 4bytes per record is a great thing as this is a table that will get growing real fast. I hope this has already made its way to the SVN code.
Reply With Quote
  #5 (permalink)  
Old 02-13-2008, 12:27 AM
dennis's Avatar
Pligg Donor
Pligg Version: 9.8.2
Pligg Template: 9.8.2
 
Join Date: Dec 2007
Location: Silicon Valley
Posts: 12
Send a message via Yahoo to dennis
Is there any plan to migrate the dB tables to use unsigned int(4)? I've prep'd a regEx that will convert the dB -- so that part is covered..
Reply With Quote
  #6 (permalink)  
Old 03-08-2008, 11:36 AM
netwb's Avatar
Casual Pligger/Coder
Pligg Version: 1.0
Pligg Template: wistie
 
Join Date: Dec 2006
Location: Bruxelles
Posts: 86
Quote:
Originally Posted by MicroBerto View Post
Hi all,

We have seen some issues with some database tables (mainly pageviews) getting too large and out of control. Some fixes have been made and helped, but I'd like to go further and discuss IP Address Storage.

Currently, Pligg uses VARCHARs to store IP address. This means that, at worst, each IP address is going to store 15 bytes of data (123.567.901.345 if you count numbers and dots). This adds up to a LOT of wasted space when we log every single profile/story/out pageview.

What is often done instead of this is to use INT UNSIGNED, and use built-in functions to do conversions to integer for us. This takes up 4 bytes -- significant savings.

The functions:
  1. INET_ATON()

    Let's say we get an IP Address: $ip_address = $_SERVER['REMOTE_ADDR'];

    When submitting this into the database, what you need to do is this:

    PHP Code:
    $query "INSERT INTO votes (vote_ip) VALUES (INET_ATON('$ip_address'))"
    And then the string IP gets formatted into UINT
  2. INET_NTOA()

    Now we need to get it out.

    PHP Code:
    $query "SELECT INET_NTOA(pv_user_ip) FROM pageviews WHERE pv_type = 'profile' DESC LIMIT 1"// (or whatever...)
    $result $db->query($query); // or however you do your queries 
    And now $result will have the text format back to you.

Files that would need to be changed:
  1. live2.php
  2. out.php
  3. register.php
  4. possibly story.php and user.php (they don't seem to be writing to the DB, just checking an IP)
  5. install/installtables.php
  6. install/upgrade.php (this might take some functions to run through all stored IP addresses, get them into an array, change the table, reformat the IP, and store them back in)
  7. libs/votes.php
  8. libs/pageview.php

References:
  1. MySQL Misc Functions
  2. Arjen's Journal - Storing IP Addresses

At a savings of up to 11 bytes per every vote and every time someone clicks on something, I think this is worth the work.

Let me know what you think, I'm willing to help with most of these.

Cheers,
berto
THANKS FOR THE solution but the THE IP adress is enable of the database to the register users? yes of no?
thanks
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Warning: Error establishing mySQL database connection. - Errors on all pages - HELP! comptalk Questions and Comments 3 04-21-2009 12:44 PM
Friendly Database Unavailable Error Message LeoNel Questions and Comments 2 09-09-2007 10:19 PM
database during install phalcon Questions and Comments 2 07-24-2007 11:17 AM
Database argh2xxx Questions and Comments 4 06-26-2007 11:04 PM
Database name, username, password = what?!?! starguy Questions and Comments 4 06-10-2007 04:29 AM


Pligg Modules and Pligg Templates from Pligg Pro Find support on the Pligg CMS Forum - 24 hours a day! Make a donation to support Pligg CMS development