问题标签 [compound-index]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
1376 浏览

performance - MongoDB通过许多参数过滤(复合索引与否)

我有产品目录,我想按许多参数过滤它:类别、价格、尺寸、颜色、重量等。

所以问题是关于索引。

我可以尝试对所有字段使用复合索引,并以与索引它们相同的顺序查询它们。但是,如果我只需要按尺寸和颜色过滤,然后只按价格和重量过滤呢?为每个可能的过滤查询创建复合索引将是多余的,因为可能有太多参数。

所以经过一番搜索,我发现了一个有趣的方法

建议使用“规范化属性”:

现在 mongo 记录将如下所示:

然后我应该按属性索引,然后我可以这样查询:

优点是:

  • 较小的索引
  • 更快的查找
  • 任意数量的参数
  • 容易成长

我不明白的是如何获得每个属性的这些数字?它们是否以某种方式计算(如 md5)?或者我应该创建一个不同的集合并将每个键值编号存储在那里?并首先从那里获取数字 - 每次我需要过滤“项目”集合时?

您如何看待这种方法?

更新: 如果我将使用连接字符串而不是数字怎么办?

0 投票
1 回答
74 浏览

mysql - mysql 复合索引是否也索引单个字段?

我有以下 MySQL/InnoDB 表。我在两列上添加了一个复合索引作为主键,我还添加了两个单列索引。使用复合索引后,我是否可以从单列索引中获得任何性能提升?还是我会更好地放弃它们?

如果有帮助,这个表的主要目的是作为一个前向索引,所以这个表是专门用 record_idINNER JOIN编辑到我的主记录表的 id 列的。

0 投票
4 回答
8614 浏览

c# - 在 C# 中创建复合索引

我想创建一个复合索引,其中一个键应该按升序排列,第二个键应该按降序排列。

我怎样才能做到这一点?

我有一个包含用户选择的属性名称的字符串。

不工作

0 投票
1 回答
82 浏览

mongodb - mongodb compound index over extending

I have a question regarding compound indexes that i cant seem to find, or maybe just have misunderstood.

Lets say i have created a compound index {a:1, b:1, c:1}. This should make according to http://docs.mongodb.org/manual/core/indexes/#compound-indexes

the following queries fast.

As i understand it the order of the query is very important, but is it only that explicit subset of {a:"a", b:"b",c:"c"} order that is important?

Lets say i do a query

or

Will these render useless for that specific compound index?

0 投票
2 回答
732 浏览

mongodb - MongoDB 复合索引的使用

假设我有以下两个键的文档:

1) 键 1
2) 键 2

如果我在他们两个上创建复合索引..

运行仅与 key1 相关的查询时,是否使用上面的索引?或者我还需要为 key1 创建特定索引?

谢谢

0 投票
2 回答
785 浏览

javascript - 确保 Collection._ensureIndex 以正确的顺序传递字段

mongo 索引文档说,对于复合索引,字段的顺序非常重要:索引只能支持使用字段的任何前缀的查询:http: //docs.mongodb.org/manual/core/indexes/

Meteor 目前在服务器端有对 MongoDBensureIndex的直通。Collection._ensureIndex

但是,当在 Javascript 中传递参数时Collection._ensureIndex,这些字段会变成 Javascript 对象。我们是否可以保证 Mongo 会以相同的顺序阅读它们?如果不是,那么设置此索引的正确方法是什么?

这可能与 JS 对象的存储方式有关。它们是关联数组,还是保持它们的属性有序?

0 投票
1 回答
145 浏览

ruby-on-rails-3 - How to get result faster with MongoDB queries and index

I am running a Rails App and using mongoid. I have created Account as a Mongoid::Document in my rails app.
There is large amount of records in account document, I have added compound index to fast the queries. but the performance of the application is poor.
I tried to do explain on query the, but the indexOnly attribute is showing as false value.

Can anyone please let me know what is the best way to create a compound index.
How can I check that the query I have written is using the index properly?

Here is my Query

first_record = Account.where(column_1: 5, column_2: "xxxx", column_3: "xxxxxx", column_4: "xxxxxxxxxxx", column_5: "xxxxxxxxx")

first_record.explain() => {"cursor"=>"BtreeCursor provider_5_params_idx", "isMultiKey"=>false, "n"=>10320, "nscannedObjects"=>10320, "nscanned"=>10320, "nscannedObjectsAllPlans"=>10320, "nscannedAllPlans"=>10320, "scanAndOrder"=>false, "indexOnly"=>false, "nYields"=>317, "nChunkSkips"=>0, "millis"=>222464, "indexBounds"=>{"column_1"=>[[5, 5]], "column_2"=>[["xxxx", "xxxx"]], "column_3"=>[["xxxxx", "xxxxx"]], "column_4"=>[["pxxxx", "xxxxxxx"]], "column_5"=>[["xxxxxx", "xxxxxx"]]}, "allPlans"=>[{"cursor"=>"BtreeCursor provider_5_params_idx", "n"=>10320, "nscannedObjects"=>10320, "nscanned"=>10320, "indexBounds"=>{"column_1"=>[[5, 5]], "column_2"=>[["xxxx", "xxxx"]], "column_3"=>[["xxxx", "xxxx"]], "column_4"=>[["xxxx", "xxxxx"]], "column_5"=>[["xxxxx", "xxxxx"]]}}], "oldPlan"=>{"cursor"=>"BtreeCursor provider_5_params_idx", "indexBounds"=>{"column_1"=>[[5, 5]], "column_2"=>[["xxxx", "xxxx"]], "column_3"=>[["xxxxx", "xxxxx"]], "column_4"=>[["xxxx", "xxxxx"]], "column_5"=>[["xxxxx", "xxxxx"]]}}, "server"=>"xxxxxxxxx"}

Also I have created index with the following way.
db.account.ensureIndex({column_1:1,column_2:1,column_3:1,column_4:1,column_5:1}, {name:"provider_5_params_idx",background:true});

0 投票
1 回答
354 浏览

mongodb - MongoDB稀疏索引和数组:索引的文档太多

我对 MongoDB 稀疏索引有疑问。我有一个包含很少文档(最大 6K)的集合(帖子),可以通过这种方式嵌入子文档:

字段“a”和“b”始终存在,但“meta.urls”可能不存在!

现在,我只插入了一个带有“meta.urls”值的文档,然后我做了

post stats 给了我一个“奇怪”的结果:索引大约是 97MB!这怎么可能?只有一个插入了“meta.urls”的文档,索引大小为 97MB ?

因此,我尝试以这种方式仅创建“meta.urls”索引:

我现在只有 1 个文档的“meta.urls_1”索引。但是如果我解释一个像这样的简单查询

我有另一个“奇怪”的结果:

为什么 Mongo 会扫描 5 个文档,而不仅仅是索引中的那个?

如果我在“meta.urls”上查询精确匹配,则单个稀疏索引将正常工作。

示例: db.post.find({"meta.urls": "url1"}).hint("meta.old_slugs_1") // 1 个文档

0 投票
1 回答
81 浏览

mongodb - 复合索引和单一索引

我要索引的文档中有两个字段。其中之一是接收时间,另一个是序列号。我希望用户能够单独查询序列号或同时查询序列号和接收时间。

在我看来,我有两个选择。

一个。

db.collection.ensureIndex({SerialNumber: 1, ReceiveTime: 1}) db.collection.ensureIndex({ReceiveTime: 1})

B.

db.collection.ensureIndex({ReceiveTime: 1, SerialNumber: 1}) db.collection.ensureIndex({SerialNumber: 1})

显然,与选项 B 相比,选项 A 是更好的选择(您希望稍后在索引中出现唯一性较低的字段)。为什么会这样?

但是,同时 MongoDB 文档指出,如果您的索引增加,则整个索引不需要适合 RAM。如果这是一个写入量很大的应用程序,那么 B 会是更好的选择吗?(复合索引大于单个索引,复合索引递增,而不是 A 不递增)

0 投票
1 回答
500 浏览

mysql - Mysql复合索引基数在所有字段上都不相同

我在 mysql 上创建了一个复合索引,这是我使用的命令:create index deliver_aid_sid_rcnt_idx on DELIVER_SM(AID,STATUSID,RETRY_CNT) USING BTREE; 奇怪的是我为每个字段得到了不同的基数值。这正常吗?我还创建了其他复合索引,并且每个字段的基数值是相同的。