mongodb university week4

1、index Creation,background

如果在foreground运行index,会阻塞其他writer,如果background运行,会比较慢,但不会阻塞其他writer,可以并发写入。

但是在产品级别的应用中,你可以同时建立replica set,在其中一个set中运行EnsureIndex foreground ,在其他的set中允许访问,然后再同步。

Tips:

  A mongod instance can only build one background index at a time per database.

  Although the database server will continue to take requests, a background index creation still blocks the mongo shell that you are using to create the index.

  Creating an index in the background takes longer than creating it in the foreground

2、 解释Index背后 mongodb的操作

db.foo.find({c:1}).explain()

该函数返回find对应于背后的数据库操作。

3、当同时有几种index可以使用时,mongodb会同时运行,当有一个index查找到结果时,会都停止。

4、db.collections.totalIndexSize() 返回index的大小bytes

5、对index的选择,进行筛选

6、hint指定选用哪个index,

优先选用c index

db.foo.find({a:100,b:100}).hint({c:1})

.sort()或者.hint()中选用的index,如果为sparse稀疏Index,则会略去该index中值为null的记录;如果使用hint({$natural:1}),则会使用BasicCusor,遍历所有文件。

foo.find(query).sort().hint([])

7、geospatial index

ensureIndex({location:'2D',type:1})

find({location:{$near:[50,50]}}) 

地理坐标查询:

存储顺序:longitude,latitude

db.runCommand({geoNear:'stores',near:[50,50] ,spherical : true, maxDistance:1})

spherical标示曲面坐标为真,maxDistance标示One radian指最大长度为地球半径,约1/6个地球周长,即六分之一的数据。

mongodb优化

loggine slow queries

当查询时间很长时,该次查询会被记录在log文件中。

profiler有三个层次:0,1,2;

0为不记录;1为记录时间长的查询;2为记录所有查询,多用在dubug程序

 在启动mongod时:mongod --dbpath... --profile 1 --slowms 2 

 db.system.profile.find({millis:{$gt:1000}}).sort({ts:-1})

 Mongotop:

 Sharding Key

原文地址:https://www.cnblogs.com/bigbigtree/p/3488110.html