Store daily notification time in the PostgreSQL - database

I'd like to give the users the ability to select which time they'll get the notifications.
The should be able to select one or multiple hour values (0-24) and then get notified daily at selected hours.
Whats the best way to model it?
Was thinking about this solution: adding an ARRAY column within user table containing hours eg. [1, 6, 23] but dunno how fast it's gonna be during scanning the table each hour in order to find users to send notification to.

The array for all users would have to be read every hour just to find any occurrences of that hour. Seems a bit much.
A single row for each user for each hour then you only select the notifications that you actually need to send. Deletes/updates can easily be garbaged out of the DB.

Related

Storing and query time interval data in redis

I have to cache program schedule data based on zipcode. Each zipcode can have between 8-20k program schedule entries for a day. Each program schedule entry would look like this,
program_name,
start_time,
end_time,
channel_no,
..
..
There can be upto 10k zipcode entries.
Now, I want to cache this in such a way so that I can query at any instant to get currently running programs. For a particular zipcode, I want to query based on condition below,
start_time < current_time + 2 minutes AND end_time > current_time
So, I was thinking of couple of approaches here.
a) Use a redis list for each zipcode. List would contain all the program schedule entries. Load all the program schedule entries in memory and filter them based on query condition above.
b) Use 2 sorted sets for each zipcode. One set will use start_time as score for each program schedule entry. Another one with end_time as score. Once we have 2 sets, I could use the zrangebyscore for both sets by passing the current_time for the score param. And then do the intersection between the resulting sets.
I was wondering if there are better ways?
The List approach (a) is likely to be less performant since you'll need to get the entire list on every query.
Sorted Sets are more suitable for this purpose, but instead of using two you can probably get away with using just one by setting the score as start_time.length, do a ZRANGEBYSCORE and then filter the result on the fractional part.
Also, whether you're using two Sorted Sets or just one, consider using a Lua script to perform the query to avoid network traffic and to localize data processing.
I did solve this a bit differently a while back. Thought of coming back and adding my answer incase somebody runs into a similar design issue.
Problem was each of 10k zipcodes could have their own schedule because the channel numbers can be different based on zip code. So, the schedule entries for each of these zipcode is different. Here is what I did.
I load schedules for next hour for all channels in USA. There were
about 25k channel numbers. I do this once a hour by loading the schedules from redis into local memory.
I also store the zipcode <-> channel mapping within local memory.
When I need schedules for a particular zipcode, I get the list of channels for that zipcode and then get the schedule entries matching the channel numbers. Because, I do this in local memory the performance was pretty good!

How to save frequent received data in database?

Me and 10 students are doing a big project where we need to receive temperature data from hardware in form av nodes, that should be uploaded and stored on a server. As we are all engineers in embedded systems and having minor database knowledge, I am turning to you guys.
I want to receive data from the nodes lets say, every 30 seconds. The table that will store that data in the database would quickly become very long if you store: [nodeId, time, temp] in a table. Do you have any suggestions how to store the data in another way?
A solution could be to store it like mentioned for a period of time and then "compromize" it somehow to a matrix of some sort? I still want to be able to reach old data.
One row every 30 seconds is not a lot of data. It's 2880 rows per day per node. I once designed a database which had 32 million rows added per day, every day. I haven't looked at it for a while but I know it's currently got more than 21 billion rows in it.
The only thing to bear in mind is that you need to think about how you're going to query it, and make sure it has appropriate indexes.
Have fun!

Order By desc for 3 types of logged in users

I keep users session for 2 hours for statistics reasons, but 'real' user are one which did some action in the past half an hour.
I need to sort the users by their actions:
First will be the real users,
Second will be the users in the statistics state (hence didn't make
an action for more than half an hour, but their session is less than
2 hours),
and last are all the rest (their statistics state is over).
I'm using 2 columns: IsLogin (bit) and LastAction (DateTime) columns.
My logic is that sorting first by bit for logged in users and then sort by the timespan will be suffice.
I'm talking of around 200,000 users, 50% are online.
Every user can do that search so I need to do the search as quickly as possible.
Notice that when user is logging in - he/she is ordered first until the next one is logging in, then he/she become second and so on.
I'm using scrolling pagination of 20 per page (scrolling down retrieves the next 20).
The table has 23 columns.
Am I using the right columns?
Need I do something else?
Does selection by bit column and then order by timespan will be
faster than just ask if timespan is less than 30 minutes from now and
then order by?

Large number of entries: how to calculate quickly the total?

I am writing a rather large application that allows people to send text messages and emails. I will charge 7c per SMS and 2c per email sent. I will allow people to "recharge" their account. So, the end result is likely to be a database table with a few small entries like +100 and many, many entries like -0.02 and -0.07.
I need to check a person's balance immediately when they are trying to send an email or a message.
The obvious answer is to have cached "total" somewhere, and update it whenever something is added or taken out. However, as always in programming, there is more to it: what about monthly statements, where the balance needs to be carried forward from the previous month? My "intuitive" solution is to have two levels of cache: one for the current month, and one entry for each month (or billing period) with three entries:
The total added
The total taken out
The balance to that point
Are there better, established ways to deal with this problem?
Largely depends on the RDBMS.
If it were SQL Server, one solution is to create an Indexed view (or views) to automatically incrementally calculate and hold the aggregated values.
Another solution is to use triggers to aggregate whenever a row is inserted at the finest granularity of detail.

Adding dates into Calendar

I'd like to get your feedback on my current strategy for updating my office hours scheduling system. I'm rewriting it to avoid laborious entry by allowing employees to have a set schedule rather than individually inputting office hours. Is there an easier/normal way for doing it other than what I am planning below?
I am using a simple calendar setup that receives events in an array from an events table and then displays them in a calendar. I want to rework the way events are added into the events table.
Currently: Employees can select a day, and then add individual office hours (Monday 9:00, 10:00, 11:00, 12:00 , 1:00, 2:00, etc.)
Goal: Employees select a set schedule (ex. Mon-Wed-Fri, from 8:00-5:00PST), and the system automatically adds future dates into the events table for display (one month in advance).
Current plan: Add a schedules table with employee ID, and fields for Mon-Sunday. Daily a CRON job runs that checks what day of the week is 28 days from now. It then queries the schedules to find all employees who have hours scheduled on that day of the week. Adds events into the events table 4 weeks in advance.
Is this a satisfactory way of doing it? Thanks! I'm new in coding, so your feedback before spending a lot of time implementing is helpful!
If their schedules are consistent, it makes good sense for each employee to have a default schedule. And it makes sense to have a cron job update the calendar automatically.
But you might give some thought to what should happen when someone wants to schedule vacation time (or make any arbitrary change to their schedule) more than a month into the future. Same issue when the owner wants to see who's scheduled to work over the summer and finds a blank calendar. (The point is that some people will invariably need to know something that the calendar doesn't yet cover. You need to plan for that. )

Resources