Solved

MySQL Optimization/Options

Posted on 2011-09-14
4
218 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

23 Experts available now in Live!

Get 1:1 Help Now