Solved

MySQL Optimization/Options

Posted on 2011-09-14
4
217 Views
Last Modified: 2012-05-12
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
Comment
Question by:erzoolander
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
jet-black earned 500 total points
ID: 36539282
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
 
LVL 3

Expert Comment

by:Duboux
ID: 36539321
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
 
LVL 3

Expert Comment

by:Duboux
ID: 36539344
don't forget to index the combo-column in step 2 ;)
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 36540741
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now