Answer to: Recommended data structures/algorithms for checking peoples' availability schedules
Score: 2
Doing search service side is a very bad idea. You would have to keep assignments synchronized with DB doing error-prone cache invalidation and potentially moving significant volumes of data around.
Do not do full scans service-side. Use DB to do searches:
CREATE TABLE workers
(
name VARCHAR NOT NULL PRIMARY KEY
)
CREATE TABLE assignments
(
worker VARCHAR NOT NULL REFERENCES workers(name),
during tsrange NOT NULL,
CONSTRAINT overlapping_times EXCLUDE USING GIST (
worker WITH =,
during WITH &&
)
)
INSERT INTO workers VALUES
('John'),
('Mary')
INSERT INTO assignments VALUES
('John', '[2021-01-01 00:00:01, 2021-01-01 00:09:01)'),
('John', '[2021-01-01 00:13:01, 2021-01-01 00:14:01)'),
('Mary', '[2021-01-01 00:05:01, 2021-01-01 00:16:01)')
Find busy workers:
SELECT worker from assignments WHERE during && tsrange('[2021-01-01 00:10:01, 2021-01-01 00:11:01)')
worker
Mary
Find available workers:
SELECT name FROM workers LEFT JOIN assignments
ON assignments.worker = workers.name AND during && tsrange('[2021-01-01 00:10:01, 2021-01-01 00:11:01)')
WHERE assignments.worker IS NULL
name
John
fiddle
I'm not sure which additional indexes would be optimal for this case, but btree-gist module surely has necessary operators to build them. There is a chance, that all indexes that might be needed are already created by CONSTRAINT directives.
View Question ↗
Question
Parent Entity
Score: 5 • Views: 595
Site: softwareengineering
Other Comments / Reviews
SaaS Metrics