从 SQLite 移植到 Redis [英] Porting from SQLite to Redis

查看:40
本文介绍了从 SQLite 移植到 Redis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 SQLite 用于我曾经存储 8-10 列的应用程序.我曾经根据任意数量的这些属性的组合来检索数据.现在我想移植到Redis.所以我正在为它构建一个测试应用.

I was using SQLite for an app in which I used to store 8-10 columns. I used to retrieve the data based upon a combination of any number of those attributes. Now I want to port to Redis. So I was building a test app for it.

但我无法考虑如何设计我的 redis 系统,以便我能够根据这些属性中的任何一个检索数据.大家有什么建议/经验吗?

But I am unable to think of how to design my redis system in such a way that I would be able to retrieve the data based upon any of those attributes. Any of you has any advice/experience?

推荐答案

我认为最好的建议是在将某些东西从 RDBMS 移植到 Redis 时避免坚持关系模型.除了模型之外,一个重要的区别是关注数据访问路径以及数据结构.

I think the best advice is to avoid sticking to the relational model when porting something from a RDBMS to Redis. And beyond the model, an important difference is to focus on data access paths as well as the data structures.

Redis 不包含查询语言(而是命令 la memcached),因此无法回复任意查询.如果数据的访问路径不是数据结构的一部分,则无法有效地检索数据.

Redis does not include a query language (but rather commands a la memcached), and cannot therefore reply to arbitrary queries. If an access path to the data is not part of the data structure, then the data cannot be efficiently retrieved.

在支持任意查询方面,Redis 并不是最好的 NoSQL 存储.例如,像 MongoDB 这样的东西会更好地为您服务.

Redis is not the best NoSQL store when it comes to supporting arbitrary queries. For instance, you would be better served by something like MongoDB.

现在,如果你真的想用 Redis 实现你的东西,你可以尝试使用类似于标记引擎的策略.您的记录可以存储在哈希对象中.对于您需要支持的任意查询的每一列部分,您使用集合构建反向索引.

Now, if you really want to implement your stuff with Redis, you could try to use a strategy similar to tagging engines. Your records can be stored in hash objects. For each column part of the arbitrary queries you need to support, you build reverse indexes using sets.

例如:

# Set up the records: one hash object per record
hmset user:1 name Bilbo type Hobbit job None
hmset user:2 name Frodo type Hobbit job None
hmset user:3 name Gandalf type Maiar job Wizard
hmset user:4 name Aragorn type Human job King
hmset user:5 name Boromir type Human job Warrior

# Set up the indexes: one set per value per field
sadd name:Bilbo 1
sadd name:Frodo 2
sadd name:Gandalf 3
sadd name:Aragorn 4
sadd name:Boromir 5
sadd type:Hobbit 1 2
sadd type:Maiar 3
sadd type:Human 4 5
sadd job:None 1 2
sadd job:Wizard 3
sadd job:King 4
sadd job:Warrior 5

# Perform a query: we want the humans who happen to be a king
# We just have to calculate the intersection of the corresponding sets
sinterstore tmp type:Human job:King
sort tmp by nosort get user:*->name get user:*->job get user:*->type
1) "Aragorn"
2) "King"
3) "Human"

通过结合并、交、差,可以实现更复杂的查询.对于非离散值或基于范围的查询,必须使用有序集 (zset)(并且可以与普通集组合).

By combining union, intersection, difference, more complex queries can be implemented. For non discrete values, or for range based queries, ordered sets (zset) have to be used (and can be combined with normal sets).

如果值具有足够的判别力,这种方法通常很快.请注意,您没有 RDBMS 的灵活性(没有正则表达式,没有前缀搜索,范围查询很难处理等......)

This method is usually quite fast if the values are discriminant enough. Please note you do not have the flexibility of a RDBMS though (no regular expressions, no prefixed search, range queries are a pain to deal with, etc ...)

这篇关于从 SQLite 移植到 Redis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆