0

i have requirement

  1. reward will give to user but reward have some condition a. reward can have limit hourly , daily , monthly

here is table maintaining user reward tracker

CREATE TABLE `user_reward_usage` (
   `id` bigint NOT NULL AUTO_INCREMENT,
   `user_id` bigint NOT NULL,
   `reward_id` int NOT NULL,
   `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
   `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON 
                                 UPDATE CURRENT_TIMESTAMP,
   `hourly_count` int DEFAULT NULL,
   `daily_count` int DEFAULT NULL,
   `monthly_count` int DEFAULT NULL,
    PRIMARY KEY (`id`),

)

any suggestion in above table or any flaws.

6
  • reward can have limit hourly , daily , monthly Only one of these 3 variants? use ENUM. May combine some options? use SET.
    – Akina
    Commented May 13 at 4:24
  • reward can have. 1. hourly limit 2. hourly and daily limit 3. hourly and daily and monthly limit 4. only daily limit 5. only monthly limit Commented May 13 at 5:31
  • 1
    If you need in a flag for each separate reward only then use SET. If you need to store the reward amount for each reward then your structure seems to be reasonable. If the combination of rewards amounts may be applied to a lot of users then they'd be normalized to separate table with referencing to it from this table.
    – Akina
    Commented May 13 at 5:44
  • You (or we) can find flaws after seeing the queries that you need.
    – Rick James
    Commented May 13 at 17:26
  • Can't user_id be the PRIMARY_KEY? (It feels like the table does not keep track of individual rewards, but only the totals to-date.)
    – Rick James
    Commented May 13 at 17:29

1 Answer 1

0

I think this might be denormalised. You probably already have a table to store users. Create a rewards table with reward_id, user_id and store the time user has been rewarded (rewarded_at or whatever you call it) there. All the usage can be then calculated using queries to the rewards table.

In general, if you feel like you need to store some counter, I would suggest to reconsider. Sure, for a small workload it won’t make a difference but if the traffic increases, this will be a point of serialisation for the access to your data. If you already use an in-memory store like Redis, you can consider storing usage data there, on top of the inserts to the rewards table.

3
  • but won't be problem for the table you are proposing let's say i have table user_reward_tracker but i need to have index on given table . when data grow won't be problem for query ? Commented May 14 at 21:17
  • Hey @RahulKumar, With no idea of the workload you run nor what business logic is behind the scenes. I still think it would become a problem in the long run regardless, there are other options. You can aggregate the data if needed. Keep the data more than a year/month/week/whatever you want old as counters and more recent as a list of entries. The whole point is to avoid updating the same row over and over again by multiple threads. <Continuation in the next comment> Commented May 16 at 8:30
  • There are also other options. You can rotate the data and remove the oldest entries. You can partition that table if needed. As you can see, there are many approaches and what should be picked, it all depends on a number of factors that we do not know here. Commented May 16 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.