Reputation: 1304
I have the stock daily return data from market, and want to put them to redis, current data format is [stock_id, date, profit], my most operation is to search a stock list from an interval days, e.g. stock_id_list: (203, 512, 532), date: from '20050101' to '20151231'
If query data from mysql, it's quite slow.
I want you to help me design a redis data structure to store these data, and can query them quickly
Upvotes: 1
Views: 746
Reputation: 4677
You can use the sortedset. use the date as value and make others into a json as the member. When you query, use the zrangebyscore. in redis ZADD doc:
The score values should be the string representation of a double precision floating point number. +inf and -inf values are valid values as well.
so you should convert the date to a number, use the timestamp or the days count form a early timepoint or change the date to a number E.g. 2017-08-11 to 20170811
One thing to notice: score could be repeated, but member cannot in sorted set,
If a specified member is already a member of the sorted set, the score is updated and the element reinserted at the right position to ensure the correct ordering.
so you should add a field make your stock data unique, such as uuid or the timestamp you insert it into redis.
Upvotes: 2