search

Home  >  Q&A  >  body text

redis 如何实现数据筛选

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),
);
黄舟黄舟2798 days ago1368

reply all(6)I'll reply

  • 高洛峰

    高洛峰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

    reply
    0
  • 伊谢尔伦

    伊谢尔伦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...

    reply
    0
  • 大家讲道理

    大家讲道理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.

    reply
    0
  • PHP中文网

    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?

    reply
    0
  • ringa_lee

    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~

    reply
    0
  • 高洛峰

    高洛峰2017-04-22 09:02:37

    Create another index or change to mongodb

    reply
    0
  • Cancelreply