Mysql json based trending tags implementation

后端 未结 2 1589
借酒劲吻你
借酒劲吻你 2021-01-18 14:29

I am trying to identifying the trending tags (based on maximum hits) on time series using mysql json feature. Below is my table

CREATE TABLE TAG_COUNTER (
           


        
2条回答
  •  南笙
    南笙 (楼主)
    2021-01-18 14:37

    I don't see a good reason, why you use JSON here. It's also not clear, why you believe that a "nosql schema" within MySQL would do anything better.

    What you probably need is something like this:

    CREATE TABLE TAG_COUNTER (
        account       varchar(36) NOT NULL,
        time_id       INT NOT NULL,
        tag_name      varchar(50) NOT NULL,
        counter       INT UNSIGNED NOT NULL,
        PRIMARY KEY   (account, time_id, tag_name)
    );
    

    This will simplify your queries. The INSERT statement would look like:

    INSERT INTO TAG_COUNTER
      (account, time_id, tag_name, counter)
    VALUES
      ('google', 2018061023, 'tag1', 1),
      ('google', 2018061023, 'tag2', 1)
    ON DUPLICATE KEY UPDATE counter = counter + VALUES(counter);
    

    The SELECT statement might be something like this

    SELECT
        SUBSTRING(time_id, 1, 6) AS month,
        tag_name,
        SUM(counter) AS counter_agg
    FROM TAG_COUNTER
    GROUP BY month, tag_name
    ORDER BY month, counter_agg DESC;
    

    Note that I did't try to optimize the table/schema for data size and performance. That would be a different question. But you must see, that the queries are much simpler now.

提交回复
热议问题