Hello gentlemen,
I would ask your advice what is the best way to operate Log like records in Redis. Say I have a set of records to store:
A typical record contains follow fields:
- Unique ID (int);
- Some value (string);
- Status(int);
- Start Date (datetime);
- End Date (datetime)
so as we can see I'm going to save continuing events, each event has date of start and date of finish. At a first glance I have to use following schema:
Save all records as individual hash:( $foo means a value of a filed)
HMSET "prefix:records:$ID" ID $ID Value $Value Status $Status StartDate $StartDate EndDate $EndDate
To search by Status value save them in sets
SADD "prefix:Status:$Status" $ID
And add the ID into Start Dates' and End Dates' sorted sets:
ZADD "prefix:StarDate" $StartDate $ID
ZADD "prefix:EndDate" $EndDate $ID
So what is not problem in this case to find events by Status or ID, my problem is how can I search for event by date?
I need to find events which were happening at some date. The way I see is:
Get the list of event's IDs which start before the given date:
ZRANGEBYSCORE prefix:StartDate -inf "$date"
Save them to temporary set:
SADD tmp1 <IDs from the query above>
Get the list of event's IDs which finished after the given date and save them into another temporary set:
ZRANGEBYSCORE prefix:EndDate "$date" +inf
SADD tmp2 <IDs from the query >
Intersect both sets:
SINTER tmp1 tmp2 => $list
And get the records from prefix:records:$id
$list.each do |ID|
HGETALL prefix:records:$ID
end
Something like this, but I guess it will take long time to serach by this way, how can I change dataschema or search procedure to have Redis's profit?
I use 2.4.4, so no Lua available until it not in stable branch
Thank you in advance,
--
My best wishes.
Alexandr Ogurtsov.
Linux is very friendly it is just picky who its friends are