A
A
Alexey Abramenko2017-01-27 07:50:30
MySQL
Alexey Abramenko, 2017-01-27 07:50:30

Proper storage of statistics, and work with it with high attendance?

Good afternoon dear!
Not so long ago, I implemented modest statistics for the account of news views for the site. I use MySQL for storage.
As a result, I got a pretty simple table:

CREATE TABLE `developers_stat` (
  `news_id` int(20) DEFAULT NULL,
  `date` date DEFAULT NULL,
  `views` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

As a result, we have: the number of news views for each day of any month and year.
news_id | date | views
4285 | 2016-12-29 | 8258
4285 | 2016-12-10 | 10698
4285 | 2016-12-11 | 10989
4285 | 2017-01-02 | 11735
4285 | 2017-01-07 | 11253

Next, I display statistics in the admin panel.
Total, Last 30 days, This week, Yesterday, Today.
5266c7f1423845e0bd55016bb44a945c.png
Now there are 564,627 lines in the database and this is only for 3 months.
When updating views:
$readcount_stat = $db->super_query( "SELECT count(*) as count FROM developers_stat WHERE news_id='{$row['id']}' AND date='{$today}'" );

if( !$readcount_stat['count'] ) {
    $db->query( "INSERT INTO developers_stat (news_id, date, views) VALUES ('{$row['id']}', '{$today}','1')" );
}else{
    $db->query( "UPDATE developers_stat SET views=views+1 WHERE news_id='{$row['id']}' AND date='{$today}'" );
}

Site traffic is about 70,000, and with each view, a query is made to the database (SELECT count (*) and UPDATE), and so on for each view. What is the load on the server.
How would you recommend storing such statistics more competently?
So far, I see only one option, which is to store general statistics in a separate table.
news_id | total_views
4283 | 301894
4284 | 380036
4285 | 250481

And in the current table, leave only for the current month,
news_id | date | views
4283 | 2017-01-02 | 11735
4283 | 2017-01-03 | 12248
4284 | 2017-01-04 | 11804

after which run the script through cron, and transfer the values ​​​​to the general table and add them with those values. And reset the current one, and start the month anew.
But this will reduce the amount of data, but not the load itself.
I will be glad to your advice and criticism.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2017-01-27
@AlekseyKota

Yandex Clickhouse
+ archive / aggregation for old periods (last year before last, etc.)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question