Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

MySQL Optimization/Options

Hi -

We have a tracking system on our website that's begun to become a bit cumbersome on performance.  Essentially - when someone hits a certain type of page - there's a sequence that:

1. Checks the IP address of the person hitting the page
2: Checks to see if that IP address has hit the page before
3: If the IP address is new/unique - logs it into the database for that page
4: If the IP address is not unique - adds to a counter for that IP address

The system works - but we're now at about 750k records and growing...and it's slowing down (obviously).

I know enough about SQL to make that process work - but need some guidance on how to optimize it.  How exactly do you deal with large recordsets like that without a degradation in performance?  Any suggestions/solutions that have worked for you?
0
erzoolander
Asked:
erzoolander
  • 2
1 Solution
 
jet-blackCommented:
Hi erzoolander,

Firstly, add INDEX to ip_address column if it doesn't exists already. You should add indexes to all columns that you use in WHERE statements in your queries.
http://dev.mysql.com/doc/refman/5.0/en/create-index.html

I suggest you to use stored procedure for the check the ip address. It is very simple but it is very effective for performance. Also do the insert operations in the same stored procedure.
http://www.mysqltutorial.org/mysql-stored-procedure-tutorial.aspx

You can also try saving the page id to user cookie when he/she enters the page. For the next views for the same pages, you can firstly check if the page id exists in the user's cookie. If it doesn't exists, check it with mysql.
0
 
DubouxCommented:
Table indexes speed up the uhh... speed :P
But u can't use these, if you don't have them exist only once in a table.

I guess the biggest work for MySql is the searching.

You could use Indexes in 3 ways (or more):

1. Use table with unique ip's


Create a table where all the IP's are only mentioned once.
Index this column as key.
What happens when a visitor comes over:
1. script checks the new table for the IP.
2a. if it exists it will run a insert-on-duplicate-update query on the table u're already using.
2b. if it doesn't exist, it inserts a record in the table u're already using. Plus it inserts the IP into the new table.

2. add new unique column from the merging of 2 colums


add a new column to ur table and there fill the following data "IP - page".
IP's may exist multiple times, pages too.. but the combination of both shouldn't. ;)
So when a visitor comes over:
1. script just runs a insert-on-duplicate-update query on your table, checking the IP+page combo column.

3. break table in parts


Duplicate the table 9x (for example)
in the 1st table keep every record where IP starts with a 1
in the 2nd table .... where IP starts with a 2
in the..... etc..

Allthough the 2nd option is the quickest fix, the 1st one may give u the best performance.
(well, actually the 3rd idea will, however u break the parts, but that won't be necesarry and is more for huge tables (> milions of records)
0
 
DubouxCommented:
don't forget to index the combo-column in step 2 ;)
0
 
Steve BinkCommented:
Another optimization idea: break the IP into its distinct octets:

CREATE TABLE ipHistory (octet1 UNSIGNED TINYINT,octet2 UNSIGNED TINYINT,octet3 UNSIGNED TINYINT,octet4 UNSIGNED TINYINT);

Open in new window


When you store the IPs, don't store them as text.  Instead, break them into their constituent parts.  Index the ipHistory table on (octet1,octet2,octet3,octet4).  This prevents you from having to use a text-based index (like varchar), and should provide some very nice speed improvements.  

Of course, this does require some significant changes to your routine, but I think you'll find it is worth it.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now