Redis SELECT性能(Redis SELECT performance)

我正在使用带有多个数据库的redis(我通过SELECT命令切换)。

我将不同类型的信息存储到redis中,我需要以某种方式将其分开。 我不喜欢在密钥的前面添加信息类型,因此我创建了更多的数据库。

我想问一下这是一个正确的决定,关注绩效吗?

SELECT还会产生多少开销? 如果我需要从两个数据库中遍历一些相关数据,哪种方法更好(参见伪代码)?

for data in array {
  redis_select(0)
  k = redis_get(...)
  redis_select(1)
  k2 = redis_get(k)
}

要么

redis_select(0)
k = []
for data in array {
  k[x] = redis_get(...)
}

redis_select(1)
k2 = []
for data in array {
  k2[x] = redis_get(k[x])
}

I am using redis with multiple databases (which I switch by the SELECT command).

I am storing different types of information into redis and I needed to seperate it somehow. I didn't like to prefix the keys to distniguish the information type so I created more databases.

I would like to ask if it was a right decission, with concern for performance ?

Also how much overhead does SELECT cause ? If I need to traverse some related data from say two databases, which aproach is better (see pseudo code) ?

for data in array {
  redis_select(0)
  k = redis_get(...)
  redis_select(1)
  k2 = redis_get(k)
}

or

redis_select(0)
k = []
for data in array {
  k[x] = redis_get(...)
}

redis_select(1)
k2 = []
for data in array {
  k2[x] = redis_get(k[x])
}

原文:https://stackoverflow.com/questions/8805004
2022-10-06 18:10

满意答案

你需要括号:

(4).__str__()

问题是词法学家认为“4.” 将是一个浮点数。

此外,这样做:

x = 4
x.__str__()

So you think you can  dance  floating-point?

123 is just as much of an object as 3.14, the "problem" lies within the grammar rules of the language; the parser thinks we are about to define a float — not an int with a trailing method call.

We will get the expected behavior if we wrap the number in parenthesis, as in the below.

>>> (123).__str__()
'123'

Or if we simply add some whitespace after 123:

>>> 123 .__str__()
'123'


The reason it does not work for 123.__str__() is that the dot following the 123 is interpreted as the decimal-point of some partially declared floating-point.

>>> 123.__str__()
  File "", line 1
    123.__str__()
              ^
SyntaxError: invalid syntax

The parser tries to interpret __str__() as a sequence of digits, but obviously fails — and we get a SyntaxError basically saying that the parser stumbled upon something that it did not expect.



Elaboration

When looking at 123.__str__() the python parser could use either 3 characters and interpret these 3 characters as an integer, or it could use 4 characters and interpret these as the start of a floating-point.

123.__str__()
^^^ - int
123.__str__()
^^^^- start of floating-point

Just as a little child would like as much cake as possible on their plate, the parser is greedy and would like to swallow as much as it can all at once — even if this isn't always the best of ideas —as such the latter ("better") alternative is chosen.

When it later realizes that __str__() can in no way be interpreted as the decimals of a floating-point it is already too late; SyntaxError.

Note

 123 .__str__() # works fine

In the above snippet, 123  (note the space) must be interpreted as an integer since no number can contain spaces. This means that it is semantically equivalent to (123).__str__().

Note

 123..__str__() # works fine

The above also works because a number can contain at most one decimal-point, meaning that it is equivalent to (123.).__str__().



For the language-lawyers

This section contains the lexical definition of the relevant literals.

Lexical analysis - 2.4.5 Floating point literals

floatnumber   ::=  pointfloat | exponentfloat
pointfloat    ::=  [intpart] fraction | intpart "."
exponentfloat ::=  (intpart | pointfloat) exponent
intpart       ::=  digit+
fraction      ::=  "." digit+
exponent      ::=  ("e" | "E") ["+" | "-"] digit+

Lexical analysis - 2.4.4 Integer literals

integer        ::=  decimalinteger | octinteger | hexinteger | bininteger
decimalinteger ::=  nonzerodigit digit* | "0"+
nonzerodigit   ::=  "1"..."9"
digit          ::=  "0"..."9"
octinteger     ::=  "0" ("o" | "O") octdigit+
hexinteger     ::=  "0" ("x" | "X") hexdigit+
bininteger     ::=  "0" ("b" | "B") bindigit+
octdigit       ::=  "0"..."7"
hexdigit       ::=  digit | "a"..."f" | "A"..."F"
bindigit       ::=  "0" | "1"

相关问答

更多

Kotlin数组类型和类文字(Kotlin array types and class literals)

基本问题:您需要指定数组的类型。 这是使用Gson中的TypeToken完成的。 我希望这有帮助: val listType = object : TypeToken<Array<String>>() {}.type val json = """["1"]""" val yourClassList :Array<String> = Gson().fromJson(json, listType) print(yourClassList) 请注意,对于基元,它更简单: Gson().fromJso...

标量类型的复合文字(Compound literals for scalar types)

根据定义,复合文字不包含& address-of运算符。 来自N1570 6.5.2.5/p3 复合文字 : 后缀表达式由带括号的类型名称后跟括号括起的初始值设定项列表组成,是一个复合文字 。 现在,他们的声明: 标量类型和联合类型的复合文字也是允许的,但是复合文字等同于强制转换。 如果我正确阅读它是错误的。 复合文字和强制算子之间的根本区别在于前者是左值 ,如N1570 6.5.2.5/p4 (强调我的): 否则(当类型名称指定对象类型时),复合文字的类型是由类型名称指定的类型。 在任何一种情况...

访问文字上的属性适用于所有类型,但不是`int`;(Accessing attributes on literals work on all types, but not `int`; why? [duplicate])

你需要括号: (4).__str__() 问题是词法学家认为“4.” 将是一个浮点数。 此外,这样做: x = 4 x.__str__() So you think you can dance floating-point? 123 is just as much of an object as 3.14, the "problem" lies within the grammar rules of the language; the parser thinks we are about to...

Python,将类变量设置为int值不是int类型[duplicate](Python, set class variable to int value not int type [duplicate])

如此处所述。 以下方法仅适用于包含方法的类的实例(所谓的描述符类)出现在所有者类中时(描述符必须位于所有者的类字典或父类之一的类字典中)。 As stated here. The following methods only apply when an instance of the class containing the method (a so-called descriptor class) appears in an owner class (the descriptor must be...

使函数适用于所有数字类型(int,float,long)(make function work with all numeric types (int, float, long))

使用内联 : let inline sum a b = a + b 更新: 如果你有兴趣编写你自己的多态数值函数,你应该使用inline和LanguagePrimitives模块。 这里是从线程转换Haskell多态余弦函数到F#的多态余弦函数 : let inline cosine n (x: ^a) = let one: ^a = LanguagePrimitives.GenericOne Seq.initInfinite(fun i -> LanguagePrimitiv...

C#对Int32文字浮动的性能损失(C# Performance penalty for Int32 literals to floats)

答案是肯定的。 知道这一点足够聪明。 这是一个简单程序的拆卸IL( ILSpy提供 ),它具有这样一个任务。 正如您从ldc.r4指令中看到的那样 ,不会发生转换: .method private hidebysig static void Main ( string[] args ) cil managed { // Method begins at RVA 0x2050 // Code size 8 (0x8) .maxstack 1...

整数文字是否被认为是未签名的?(Are integer literals considered unsigned? [duplicate])

十进制整数文字是第一种类型,可以用int , long或long long 。 50是int类型。 可以使用u或U后缀指定无符号文字。 后缀为u或U十进制整数字是第一种类型,可以用unsigned int , unsigned long或unsigned long long 。 50U的类型为unsigned int 。 A decimal integer literal is of the first type where it can be represented in int, long o...

我应该总是使用适当的文字来表示数字类型吗?(Should I always use the appropriate literals for number types?)

您应该始终明确指出您打算使用的文字类型。 例如,当这种代码时,这将防止出现问题: float foo = 9.0f; float bar = foo / 2; 更改以下内容,截断结果: int foo = 9; float bar = foo / 2; 当您涉及重载和模板时,这也是函数参数的一个问题。 我知道gcc有-Wconversion但我不记得它涵盖的一切。 对于适合int整数值,我通常不会对那些long或unsigned值进行限定,因为通常存在更少的微小错误。 You should a...

VC ++中的文字类型(Literals types in VC++)

看来,就你的第一个结果而言,VC ++仍然遵循C89 / 90的规则,其中说(第6.1.3.2节): 整数常量的类型是相应列表中可以表示其值的第一个。 unsuffixed decimal: int , long int , unsigned long int ; [...] 因此,由于4294967295可以表示为unsigned long int ,因此它正在使用它。 在C ++ 98/03中,仍然允许这样做,但不再需要 - 您使用的值大于可以在long int表示的值,这会给出未定义的行为(...

整数文字(Integer literals)

如果不进行强制转换,则无法将具有较高容量的类型的值分配给较低容量的类型。 long可以存储更大的数字,因此无法分配给int 。 You cannot assign a value of a type with higher capacity to a type of lower capacity without casting. long can store larger numbers, so it can't be assigned to int.

相关文章

更多

Redis概述

什么是Redis Redis是Remote Dictionary Server的缩写, Redis是一 ...

Struts2标签select的使用

我想在进入系统首页的时候就对数据库进行查询,然后讲要查询的数据以下拉里表的形式放在页面上 Actio ...

redis sentinel(哨兵) 配置详解-redis集群管理

1. redis sentinel(哨兵) redis sentinel(哨兵)是对Redis系统的 ...

Solr Performance Factors(Solr 性能因素)

Schema Design Considerations(数据模型方面考虑) indexed fiel ...

Redis Cookbook

Two years since its initial release, Redis already ...

Select2在Bootstrap 3 Modal框中不能搜索的解决方法

在项目中用了Select2,基于Bootstrap 3的搜索下拉框。但奇怪的是,在modal-dial ...

redis 集群使用主从复制架构-redis集群管理

redis集群使用主从架构如下图,能有效解决集群中节点连接不上造成集群挂掉的情况 a) 在Redis ...

Redis配置文件详解

redis是一款开源的、高性能的键-值存储(key-value store),和memcached类似 ...

redis安装-redis集群管理

安装redis [root@master opt]# mkdir /opt/redis [root ...

对比下HBase, Memcached, MongoDB, Redis和Solr

转自:http://3834362.blog.51cto.com/3824362/1354092

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本