V
V
Vadim2014-02-11 02:43:44
Database design
Vadim, 2014-02-11 02:43:44

How to design a database for distribution?

I will try to describe everything with an example:


There are 10-100-... users.
Each user can create a schedule for a specific activity on the site.
(Send an email about the status of the account to E-mail every hour)
In other words, set up a newsletter to your email as he pleases.
At least every minute. (Hardly)
Let's take for example:
10 users who want to receive emails every 10 minutes.
Namely:
00:00
00:10
00:20
00:30...

Letters may vary.
And they are stored in the "mails" table (id user_id text send_status ...)
Approached the main question:
How to organize the database structure responsible for the mailing schedule for each specific user?
Regular table: user_id, time[00:00 .. 23:59] in 1 minute increments?
time - countdown. 60*24 = 1480 per day.
The script will be run by Cron every minute.
And send everything that matches.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2014-02-11
@Rsa97

I would do this
user_id, weekDays, date, startTime, interval, endTime, nextTime

SELECT * FROM `table` 
    WHERE `nextTime` < `endTime` 
        AND ((`weekDays` & (1 << WEEKDAY(NOW())) OR `date` = CURDATE())
        AND `nextTime` < {$currentTime};
UPDATE `table` 
    SET `nextTime` = ADDTIME(`nextTime`, `interval`)
    WHERE `nextTime` < `endTime`
        AND `nextTime` < {$currentTime};
$currentTime is needed in order not to miss events between SELECT and UPDATE queries.
Reset nextTime at midnight
UPDATE `table` 
    SET `nextTime` = `startTime`

V
Vadim, 2014-02-11
@Subotinn

Theoretically, the user can create 7 schedules for each day of the week.
This means:
7(days) * 24(hours) * 60(minutes) = 10080 records per user.
This is maximum.
Let's say a person still creates for 7 days. and for 24 hours.
But it is unlikely that he will need letters more often than 1 time in 5-10 minutes.
So there will be not 60, but 6-12 letters. Let's take 10.
So 7*24*10 = 1680.
Again, let's assume that there are 1000 users.
1000 * 1680 = 1.68M records consisting of 3-4 integer fields.
This is given that each of the 1000 users made a personal schedule for each day of the week, which is unlikely.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question