redis 如何实现数据按 id 或者 timestamp 筛选?
比如,像 sql 这样
sql
select * from news_table where id > '512'; select * from news_table where id > '512' and cat_id = '2'; select * from news_table where pubdate > 'yesterday';
sql
-- news_table info CREATE TABLE `news_table` ( `id` int(10), `cat_id`, int(10), `title` text, `content` longtext, `pubdate` int(10), );
高洛峰2017-04-22 09:02:37
Please take a look at redis's zset, scop can achieve your requirements, but the kind of query you mentioned with an id greater than 100% is best to change your mind in redis, this is not its strong point
伊谢尔伦2017-04-22 09:02:37
I think Key-Value database is not suitable for applications filtering by conditions.
If512
这个条件是固定的,可以在添加文章的时候把大于512的元素加到一个集合里。yesterday
可以用EXPIRE
comes true.
If the filtering conditions cannot be determined during design, it will be necessary to traverse the Hash table/linked list, which will not be very efficient...
大家讲道理2017-04-22 09:02:37
What is 512? If it is a number with special meaning and often needs to be checked in this way, it is recommended that you store the set of id>512 in a separate map in redis, such as map_512, and just take out the entire map each time. If not, you can only take out the map every time and filter it locally. If this kind of query is very frequent and the filtering conditions are different, it is recommended to retrieve them all at once and then filter them uniformly.
PHP中文网2017-04-22 09:02:37
Thank you all for your replies! :)
If redis is not suitable for such needs, please recommend a suitable nosql such as mongodb?
ringa_lee2017-04-22 09:02:37
If the data changes frequently, such as adding, modifying, and deleting keys, you can use mongodb, which is easy and enjoyable~