How to track pageviews without thrashing the MySQL DB

后端 未结 3 530
别跟我提以往
别跟我提以往 2021-02-06 04:07

I am trying to track pageviews in MySQL DB using the following query:

\"UPDATE $table SET pageviews = pageviews + 1 WHERE page_id = 1\"

This is fine

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-06 04:44

    I'd consider gathering raw hits with the fastest writing engine you have available:

    INSERT INTO hits (page_id, hit_date) VALUES (:page_id, CURRENT_TIMESTAMP)
    

    ... and then running a periodical process, possibly a cron command line script, that would count and store the page count summary you need in an hourly or daily basis:

    INSERT INTO daily_stats (page_id, num_hits, day)
    SELECT page_id, SUM(hit_id)
    FROM hits
    WHERE hit_date='2012-11-29'
    GROUP BY page_id
    

    (Queries are mere examples, tweak to your needs)

    Another typical solution is good old log parsing, feeding a script like AWStats with your web server's logs.

    Clarification: My first suggestion is fairly similar to @fire's but I didn't get into storage details. The key point is to delay heavy processing and just the minimum amount of raw info in the fastest way.

提交回复
热议问题