redis 如何實(shí)現(xiàn)數(shù)據(jù)按 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), );
人生最曼妙的風(fēng)景,竟是內(nèi)心的淡定與從容!
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
I think Key-Value database is not suitable for applications filtering by conditions.
If512
這個(gè)條件是固定的,可以在添加文章的時(shí)候把大于512的元素加到一個(gè)集合里。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...
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.
Thank you all for your replies! :)
If redis is not suitable for such needs, please recommend a suitable nosql such as mongodb?
If the data changes frequently, such as adding, modifying, and deleting keys, you can use mongodb, which is easy and enjoyable~