1

I am using DocumentDb database(total size ~13TB) This is the schema of the database, all the keys are single indexed

[
    {
      {key : {"customerId" : 1}},
      {key : {"typeOfProduct" : 1}},
      {key : {"date": 1}},
    }
]

1st query: db.collectionName.find({"customerId" : <SAMPLE-CUSTOMER>}).count() returns 500 documents

2nd query: db.collectionName.find({"customerId" : <SAMPLE-CUSTOMER>, "typeOfProduct" : <TYPE>}).count() returns 200 documents

3nd query db.collectionName.find({"customerId" : <SAMPLE-CUSTOMER>, "date" : { "$gte" : NumberLong(1584055385383), "$lte" : NumberLong(1584141785383)}}).count() query just keeps on running infinitely

In the 2nd query, My understanding is that DocumentDb first gets all the documents for matching customerId which is 500, and then iteratively searches for matching typeOfProduct through each document.

In the 3rd query, it is supposed to be working in a similar manner, but the query keeps on running infinitely.

Can someone explain why this is happening? Why is it so slow with the Date? Is it because of the size of the database or am I writing the query wrong?

4

1 回答 1

0

我不确定您是否有一个三字段索引 (1) 或三个单字段索引 (2)。

  1. 第一个和第二个查询都使用该索引,因为它们与它进行前缀匹配。一般来说,第三个查询可以轻松使用索引,跳过第二个字段。您可以使用explain().
  2. Mongo 将如何选择索引并不明显,但知道您的数据结构,您可以建议一个,使用hint(index)({customerId: 1}在您的情况下)。要检查默认情况下哪个用于此查询,请使用explain().
于 2020-03-15T23:04:55.390 回答