简体   繁体   中英

Database daily counter in MySQL

I want to create counters in MySQL database that stores the number of views per products in a specific day, month and year.

The problems is that I have around 2000 products. I thought about using the following schema:

 id (BIGINT)
 year (INT[4])
 month (TINYINT)
 day (TINYINT)
 product_id (INT)
 pageviews (BIGINT)

The problem with that solution is that if, in the worst case scenario, each product is viewed each day, I will have 2000 rows in my database each day. Multiply is by 36 days and I will have 72,000 each month.

I wanted to know if there is a better way to implement this. I thought that the daily data will be kept only in memory as Application variable (developing in .NET) and as ArrayList. IF I chose that direction, I will have less rows/data, 2000 rows each month.

I really want to keep the cumulative daily page-views. By the way, previews is just for illustration purposes, I will store different data, but that data will be updated very frequently.

If I use the daily column, it will be update very often, almost every 2-5 seconds. I intend to update MySQL async by calling an ASP.NET webservice from Javascript, passing the product_id and increase the counter +1. That's to prevent the application to wait for the update to occur.

I also want to know the estimate table size. IF I'm doing it right:

BIGINT = 8 bytes

Datetime = 10 bytes

INT - 4 bytes

^if I decide to go with a datatime column instead of year/month/day

30 bytes * 2000 = 60,000 bytes

60KB (approx.) * 30 days = 180KB month

180KB month * 12 months = 2160KB / year

Did I get it right?

Ok I'm not that great in Database design but I know that much... you should ask yourself for how long you want to save the daily hits on these 2K products?

If you want to hold them just for a week then you can calculate how much it'll take in Size (also if you want to hold them for a month).

If you want to save it for let's say a month then you could always make a table each month and save the data from the daily hits to that table and sum them up so you'll have only 2K rows in each table

Or you could use a "warehouse" table that will hold all the hits per let's say month and every month the data will be copied to that warehouse table after you sum it up for each month.

If you want these hits to be saved indefinitely then that is the way as far as I know (Again not a master in Database design).

Again this is what you should do if you want to save the hits up to a month.

Hope I helped you, Sagi.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM