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

查看:455
本文介绍了从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不包括查询语言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天全站免登陆