当前位置:   article > 正文

MongoDB性能优化 - MongoDB从入门到删库

mongpdb indexbuildbatchsize

创建索引

在查询条件的字段或者排序条件的字段上创建索引,可以显著提高执行读效率。但是对于写比读多的,就尽量不要添加索引,因为索引越多,写的操作就会越慢。

  1. > db.testhint.insertMany([{"a":1,"b":2,"c":3},{"a":2,"b":3,"c":4}])
  2. ...
  3. > db.testhint.ensureIndex({"a":1,"b":1})
  4. ...
  5. > db.testhint.dropIndex({"a":1,"b":1})
  6. { "nIndexesWas" : 2, "ok" : 1 }
  7. # 静默方式创建,其他读写操作仍然可以同步进行,不会产生阻塞。
  8. > db.testhint.ensureIndex({"a":1,"b":1},{background:true})
  9. {
  10. "createdCollectionAutomatically" : false,
  11. "numIndexesBefore" : 1,
  12. "numIndexesAfter" : 2,
  13. "ok" : 1
  14. }
  15. # 创建唯一索引
  16. > db.testhint.ensureIndex({"c":1},{unique:true})
  17. {
  18. "createdCollectionAutomatically" : false,
  19. "numIndexesBefore" : 2,
  20. "numIndexesAfter" : 3,
  21. "ok" : 1
  22. }
  23. > db.testhint.getIndexes()
  24. [
  25. {
  26. "v" : 2,
  27. "key" : {
  28. "_id" : 1
  29. },
  30. "name" : "_id_",
  31. "ns" : "test.testhint"
  32. },
  33. {
  34. "v" : 2,
  35. "key" : {
  36. "a" : 1,
  37. "b" : 1
  38. },
  39. "name" : "a_1_b_1",
  40. "ns" : "test.testhint",
  41. "background" : true
  42. },
  43. {
  44. "v" : 2,
  45. "unique" : true,
  46. "key" : {
  47. "c" : 1
  48. },
  49. "name" : "c_1",
  50. "ns" : "test.testhint"
  51. }
  52. ]

重建索引,在collection大量删除的时候,磁盘空间并不会自动回收,长期这样会产生大量的磁盘碎片,影响磁盘读取性能,所以数据索引需要周期性地重建。

  1. > db.testhint.reIndex()
  2. {
  3. "nIndexesWas" : 3,
  4. "nIndexes" : 3,
  5. "indexes" : [
  6. {
  7. "v" : 2,
  8. "key" : {
  9. "_id" : 1
  10. },
  11. "name" : "_id_",
  12. "ns" : "test.testhint"
  13. },
  14. {
  15. "v" : 2,
  16. "key" : {
  17. "a" : 1,
  18. "b" : 1
  19. },
  20. "name" : "a_1_b_1",
  21. "ns" : "test.testhint",
  22. "background" : true
  23. },
  24. {
  25. "v" : 2,
  26. "unique" : true,
  27. "key" : {
  28. "c" : 1
  29. },
  30. "name" : "c_1",
  31. "ns" : "test.testhint"
  32. }
  33. ],
  34. "ok" : 1
  35. }
  36. # 全库级别的空间回收,可以提高回收的效率
  37. > db.repairDatabase()
  38. { "ok" : 1 }

限定返回结果条数

使用limit()函数可以限定返回结果集的记录条数,不仅可以减少数据库服务的资源消耗,还可以减少网络传输的数据量。

  1. > db.starbucks.find().sort({"_id":-1}).limit(3)
  2. { "_id" : "9722", "name" : "31st and Sixth Avenue", "street" : "875 Sixth Ave", "city" : "New York", "location" : { "type" : "Point", "coordinates" : [ -73.989251, 40.748026 ] }, "_class" : "example.springdata.mongodb.repo.Store" }
  3. { "_id" : "9467", "name" : "43rd & Ninth", "street" : "593 Ninth Ave", "city" : "New York", "location" : { "type" : "Point", "coordinates" : [ -73.992514, 40.758934 ] }, "_class" : "example.springdata.mongodb.repo.Store" }
  4. { "_id" : "9439", "name" : "84th & Third Ave", "street" : "1488 Third Avenue #A", "city" : "New York", "location" : { "type" : "Point", "coordinates" : [ -73.955133, 40.777492 ] }, "_class" : "example.springdata.mongodb.repo.Store" }

只查询用到的字段

只查询使用到的字段,而不是查询所有字段。这样比查询所有字段的效率要高很多。

  1. > db.starbucks.find({},{"name":1,"city":1}).sort({"_id":-1}).limit(3)
  2. { "_id" : "9722", "name" : "31st and Sixth Avenue", "city" : "New York" }
  3. { "_id" : "9467", "name" : "43rd & Ninth", "city" : "New York" }
  4. { "_id" : "9439", "name" : "84th & Third Ave", "city" : "New York" }

采用Capped Collection

capped集合比普通的集合读写效率要高。因为capped集合是高效率的集合类型。

  1. > db.createCollection("cappedLogCollection",{capped:true,size:10000,max:3})
  2. { "ok" : 1 }
  3. > db.cappedLogCollection.insert({"num":5})
  4. WriteResult({ "nInserted" : 1 })
  5. > db.cappedLogCollection.insert({"num":6})
  6. WriteResult({ "nInserted" : 1 })
  7. > db.cappedLogCollection.insert({"num":7})
  8. WriteResult({ "nInserted" : 1 })
  9. > db.cappedLogCollection.insert({"num":8})
  10. WriteResult({ "nInserted" : 1 })
  11. > db.cappedLogCollection.find()
  12. { "_id" : ObjectId("5c555e82ec962d89c701558a"), "num" : 6 }
  13. { "_id" : ObjectId("5c555e86ec962d89c701558b"), "num" : 7 }
  14. { "_id" : ObjectId("5c555e8aec962d89c701558c"), "num" : 8 }

capped集合的特点:

1、capped集合是一种固定大小的集合,使用之前必须先创建它,并且设置其大小。

  • size 是整个集合空间大小,单位为【KB】
  • max 是集合文档个数上线,单位是【个】

2、capped集合可以插入数据和修改数据,但是不能删除数据,只能使用drop()函数删除整合集合。
3、查询时,没有指定排序,会返回插入时的顺序数据。capped集合可以自动维护插入数据的顺序。
4、当插入的数据超过capped集合的限定大小,数据库会使用FIFO算法,用新数据覆盖最先插入的数据。

采用Server Side Code Execution命令集

Server Side Code Execution是由JavaScript编写,这个过程经过编译和优化后存储在system.js表中。类似存储过程,可以减少网络通信的开销,提高数据库的性能。但是,eval()已经被官方弃用。

  1. > db.system.js.save({"_id":"echo","value":function(x){return x;}})
  2. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  3. > db.eval("echo('test')")
  4. WARNING: db.eval is deprecated
  5. test

使用hint

MongoDB的查询优化器会自动优化查询,但是有些情况需要强制使用hint才可以提高工作效率,因为hint可以强制要求查询操作使用某个索引。

  • indexBounds:使用的索引
  1. > db.testhint.insertMany([{"a":1,"b":2,"c":3},{"a":2,"b":3,"c":4}])
  2. {
  3. "acknowledged" : true,
  4. "insertedIds" : [
  5. ObjectId("5c5572468e3fd7101d8d568a"),
  6. ObjectId("5c5572468e3fd7101d8d568b")
  7. ]
  8. }
  9. > db.testhint.ensureIndex({"a":1,"b":1})
  10. {
  11. "createdCollectionAutomatically" : false,
  12. "numIndexesBefore" : 1,
  13. "numIndexesAfter" : 2,
  14. "ok" : 1
  15. }
  16. > db.testhint.find({a:1}).explain()
  17. {
  18. "queryPlanner" : {
  19. ...
  20. },
  21. "winningPlan" : {
  22. "stage" : "FETCH",
  23. "inputStage" : {
  24. "stage" : "IXSCAN",
  25. ...
  26. "indexBounds" : {
  27. "a" : [
  28. "[1.0, 1.0]"
  29. ],
  30. "b" : [
  31. "[MinKey, MaxKey]"
  32. ]
  33. }
  34. }
  35. },
  36. "rejectedPlans" : [ ]
  37. },
  38. "serverInfo" : {
  39. ...
  40. },
  41. "ok" : 1
  42. }
  43. > db.testhint.find({a:1}).hint({"a":1,"b":1}).explain()
  44. {
  45. "queryPlanner" : {
  46. ...
  47. },
  48. "winningPlan" : {
  49. "stage" : "FETCH",
  50. "inputStage" : {
  51. "stage" : "IXSCAN",
  52. ...
  53. "indexBounds" : {
  54. "a" : [
  55. "[1.0, 1.0]"
  56. ],
  57. "b" : [
  58. "[MinKey, MaxKey]"
  59. ]
  60. }
  61. }
  62. },
  63. "rejectedPlans" : [ ]
  64. },
  65. "serverInfo" : {
  66. ...
  67. },
  68. "ok" : 1
  69. }

新版本MongoDB中,查询优化器已经经过优化,能达到更好的查询效果。是否走索引,可以先用explain()函数检查一下。

采用Profiler

采用Profiler功能会影响效率,使用system.profile来记录,而system.profile是一个Capped集合,所以,对性能影响并不严重。

  1. # 临时开启,默认记录100ms
  2. > db.getProfilingLevel()
  3. 0
  4. > db.setProfilingLevel(1,100)
  5. { "was" : 1, "slowms" : 100, "sampleRate" : 1, "ok" : 1 }
  6. # 查找执行时间大于5ms的profiler日志
  7. > db.system.profile.find({millis:{$gt:5}})
  8. # 查看最新产生的profiler记录
  9. > db.system.profile.find().sort({$natural:-1}).limit(1)

转载于:https://my.oschina.net/u/1404949/blog/3039478

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号