我应该在mongodb中使用稀疏索引作为布尔标志吗? [英] Should I use sparse index for boolean flags in mongodb?

查看:188
本文介绍了我应该在mongodb中使用稀疏索引作为布尔标志吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布尔标志:已完成。我应该

I have a boolean flag :finished. Should I

A: index({ finished: 1 })
B: index({ finished: 1 }, {sparse: true})
C: use flag :unfinished instead, to query by that
D: other?

Ruby mongoid语法。我的大多数记录都会有flag finished = true,而且大多数操作显然会取得未完成的记录。我不确定我是否理解何时使用稀疏和何时不使用。谢谢!

Ruby mongoid syntax. Most my records will have flag finished=true, and most operations fetch those unfinished, obviously. I'm not sure if I understand when to use sparse and when not to. Thanks!

推荐答案

稀疏标志有点奇怪。要了解何时使用它,您必须首先理解为什么稀疏存在。

The sparse flag is a little weird. To understand when to use it, you have to understand why "sparse" exists in the first place.

当您在一个字段上创建简单索引时,有一个条目对于每个文档,甚至是没有该字段的文档。

When you create a simple index on one field, there is an entry for each document, even documents that don't have that field.

例如,如果您的索引在 {rare_set_field:1} ,你将有一个主要用 null 填充的索引,因为在大多数情况下该字段不存在。这是浪费空间而且搜索效率低。

For example, if you have an index on {rarely_set_field : 1}, you will have an index that is filled mostly with null because that field doesn't exist in most cases. This is a waste of space and it's inefficient to search.

{sparse:true} 选项将摆脱 null 的值,因此在定义 {rare_set_field} 时,您将获得仅包含条目的索引。

The {sparse:true} option will get rid of the null values, so you get an index that only contain entries when {rarely_set_field} is defined.

回到你的情况。

你问的是使用布尔+稀疏。但稀疏并不会真正影响布尔,稀疏影响设置与未设置。

You are asking about using a boolean + sparse. But sparse doesn't really affect "boolean", sparse affect "is set vs. is not set".

在您的情况下,您尝试获取未完成。要利用稀疏,键不是布尔值,但未完成条目具有该键并且已完成 条目根本没有密钥。

In your case, you are trying to fetch unfinished. To leverage sparse the key is not the boolean value, but the fact that unfinished entries have that key and that "finished" entries have no key at all.

{ _id: 1, data: {...}, unfinished: true }
{ _id: 2, data: {...} } // this entry is finished

听起来像是在使用队列

您绝对可以利用上述信息来实现稀疏索引。但是,它实际上听起来像是在使用队列。 MongoDB可作为队列使用,这里有两个 示例

You can definitely leverage the information above to implement a sparse index. However, it actually sounds like you are using a Queue. MongoDB is serviceable as a Queue, here are two examples.

但是,如果你看一下队列,他们就不会像你那样做了。我个人使用MongoDB作为一些生产系统的队列,它运行得很好,但是测试你的预期负载,因为专用的队列会表现得更好。

However, if you look at the Queue, they are not doing it the way you are doing it. I'm personally using MongoDB as a Queue for some production systems and it runs pretty well, but test your expected load as a dedicated Queue will perform much better.

这篇关于我应该在mongodb中使用稀疏索引作为布尔标志吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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