【四】MongoDB索引管理

來源:互聯網
上載者:User

標籤:

一、索引介紹

在mongodb中,索引用來支援高效查詢。如果沒有索引,mongodb必須在整個集合中掃描每個文檔來尋找匹配的文檔。但是如果建立合適的索引,mongodb就可以通過索引來限制檢查的文檔數量。

索引是一種特殊的資料結構,它儲存著集合中小部分的資料集,這種資料結構很容易遍曆。索引儲存著指定的欄位或欄位集合,這些欄位都是根據欄位值排序的。排序的索引條目能夠支援高效的等值匹配和基於範圍的查詢操作,此外,mongodb通過排序索引還能夠返回排好序的結果集。

從根本上來說,mongodb的索引類似於其他關係型資料庫的索引,它被定義在集合層面並支援任何欄位或子域,它採用B-tree資料結構。

二、索引概念

1、索引類型

MongoDB提供多種不同類型的索引。對於某一文檔或者內嵌文檔,你能夠在任意欄位或者內嵌欄位上建立索引。一般而言,你應該建立通用的面向使用者的索引。通過這些索引,確保mongodb掃描最少最有可能匹配的文檔。在mongodb的shell中,你能通過調用createIndex()方法建立一個索引。

1)單欄位索引

對於集合中的文檔,mongodb完全支援在任何欄位上建立索引。預設地,任何集合的_id欄位上都有一個索引,並且應用和使用者還可以添加額外的索引來支援重要的查詢和操作。mongodb既支援單欄位索引也支援多個欄位的複合索引,這裡先介紹單欄位索引,下面請看舉例說明:

> db.friends.insert({"name" :"Alice","age":27}) #集合friends中的一個文檔WriteResult({ "nInserted" : 1 })> db.friends.createIndex({"name" :1}) #在文檔的name欄位上建索引{    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1}

db.collection.createIndex(keys,options)介紹:

Parameter Type Description
keys document

A document that contains the field and value pairs where the field is the index key and the value describes the type of index for that field. For an ascending index on a field, specify a value of 1; for descending index, specify a value of -1.

MongoDB supports several different index types including textgeospatial, and hashed indexes. See Index Types for more information.

options document Optional. A document that contains a set of options that controls the creation of the index. See Optionsfor details.
  • _id欄位上索引:當一個集合被建立時,預設的會在_id欄位上建立一個升序唯一索引,這個索引是不能被刪除的。考慮到_id欄位是一個集合的主鍵,所以對於集合中每個文檔都應該有一個唯一的_id欄位,在該欄位中,你能儲存任意的唯一的值。_id欄位預設的值是ObjectId,它是在插入文檔時被自動產生。在分區集合環境中,如果你沒有指定_id欄位為shard key,那麼你的應用程式必須確保_id欄位值得唯一性,否則會報錯。常用的做法是:通過自動產生ObjectId標準值解決。
  • 內嵌欄位索引:在內嵌文檔的任意欄位上,你也可以建立索引,就如同在文檔的一級欄位上建立一樣。不過,需要說明的是,在內嵌欄位上建立索引和在內嵌文檔上建立索引是有區別的,前者通過點號的方式訪問內嵌文檔中的欄位名。請看下面的例子:
> db.people.insert(... {...   name:"John Doe",...   address: {...      street: "Main",...      zipcode:"53511",...      state: "WI"... }... }... )WriteResult({ "nInserted" : 1 }) > db.people.createIndex({"address.zipcode":1})  #通過address.zipcode方法引用zipcode欄位,注意需要加上雙引號。
{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
  • 內嵌文檔索引:
> db.factories.insert(      { metro:               {                       city: "New York",                       state:"NY"                },           name: "Giant Factory"        })WriteResult({ "nInserted" : 1 })> db.factories.createIndex({metro:1}){    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1}

上面的metro欄位是內嵌文檔,包含內嵌欄位city和state,所以建立方法同一級欄位建立方法一樣。

下面的查詢能夠用到該索引:

db.factories.find( { metro: { city: "New York", state: "NY" } } )

{ "_id" : ObjectId("56189565d8624fafa91cbbc1"), "metro" : { "city" : "New York", "state" : "NY" }, "name" : "Giant Factory" }

在內嵌文檔進行等值匹配查詢時,需要注意欄位的順序,例如下面的查詢將匹配不到任何文檔:

> db.factories.find( { metro: { state: "NY", city: "New York" } } )> > 

2)複合索引

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

【四】MongoDB索引管理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.