redis 如何实现数据筛选
黄舟
黄舟 2017-04-22 09:00:37
0
6
1385

redis 如何实现数据按 id 或者 timestamp 筛选?
比如,像 sql 这样

sqlselect * 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),
);
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(6)
小葫芦

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这个条件是固定的,可以在添加文章的时候把大于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...

大家讲道理

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.

Ty80

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~

小葫芦

Create another index or change to mongodb

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template