开发者

Mongodb/Mongoid query slowness on an indexed attribute

开发者 https://www.devze.com 2023-04-12 07:06 出处:网络
I have a mongoid model like this: class Link include Mongoid::Document include Mongoid::Timestamps field :url, type: String

I have a mongoid model like this:

class Link
  include Mongoid::Document
  include Mongoid::Timestamps
  field :url, type: String

  index :url, background: true
end

Now I have 2 queries with profiling turned on:

Link.where(url: "http://stackoverflow.com/questions/ask").first  # =># <Link _id .....>

Executed < 1ms, no record for slowness

Link.where(url: "no url").first # =># nil

Executed = 35ms *PROFILER::* Sun Oct 9 23:36:20 [conn20] query ccc7.links ntoreturn:1 nscanned:16402开发者_运维百科 reslen:20 35ms

My question:

Clearly indexing is working fine, But why would a non-existent took mongodb such a long time to query? even scaning the entire mongo collection? isn't indexing taking care of this?


Executed = 35ms PROFILER:: Sun Oct 9 23:36:20 [conn20] query ccc7.links ntoreturn:1 nscanned:16402 reslen:20 35ms

Clearly there is an issue. Indeed, the number of nscanned docs should close (equal) to the number of results if you request only on the indexed field.

From the documentation :

nscanned Number of items (documents or index entries) examined. Items might be objects or index keys. If a "covered index" is involved, nscanned may be higher than nscannedObjects.

As suggested a explain on your request should give more information.

Can you also provide the result of :

db.link.getIndexes()


So, not completely clear here. Are all of your urls unique up to the "no url" option.

If so, and you indexed that column in ascending order, you are seeing the results of an alphabetical scan up to "n". Since all your urls start with h, you may see a delay.

Not 100% positive, but it is a possibility.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号