Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MySQL Optimization/Options

Posted on 2011-09-14
4
Medium Priority
?
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
jet-black earned 2000 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 51

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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

705 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