Reputation: 19713
I've got a web app that tracks page views. The app checks the IP address of the end-user and maps it to a Country. Everything works here so far.
However, I am realizing that this table (called views
) is getting populated very fast, since it tracks every page view. On some pages reaching 500k, 1M, etc. Which could potentially store over millions of records on this table within a short time.
I thought about aggregating the results every 24 hours into a new table (called country_views
) and then deleting the records in views
.
So for example. My views
table could look like:
page_id | country_id | created_at | updated_at
======= | ========== |=========== | ==========
1 | 1 | 2011-11... | 2011-11...
1 | 2 | 2011-11... | 2011-11...
1 | 1 | 2011-11... | 2011-11...
1 | 1 | 2011-11... | 2011-11...
2 | 4 | 2011-11... | 2011-11...
2 | 3 | 2011-11... | 2011-11...
After 24 hours. I count the results from this table and put them into a new table called country_views
. Which could look like:
page_id | country_id | total | created_at | updated_at
======= | ========== | ===== | ========== | ==========
1 | 1 | 3 | 2011-11... | 2011-11...
1 | 2 | 1 | 2011-11... | 2011-11...
2 | 4 | 1 | 2011-11... | 2011-11...
2 | 3 | 1 | 2011-11... | 2011-11...
On the same web app. I plan on showing this data to certain users as a pie chart to display which countries are viewing certain pages.
Is this a sensible approach? Is there a better way to go about this?
Upvotes: 0
Views: 50
Reputation: 5779
Why don't you just keep a count in views?
You only want to count which page was viewed by which country so just add a count column and increment it if the record already exists otherwise add a new row :)
Upvotes: 1
Reputation: 11478
I think this approach is fine, especially since it could be easily programatically undone.
Upvotes: 0