zoukankan      html  css  js  c++  java
  • mongodb(三)

    索引详讲
    索引管理
    空间索引

    for(var i = 0 ; i<200000 ;i++){
        db.books.insert({number:i,name:i+"book"})
    }

    1.创建简单索引

    数据准备index.js
    1.1.先检验一下查询性能
    var start = new Date()
    db.books.find({number:65871})
    var end = new Date()
    end - start
    1.2.为number 创建索引,1表示正序,-1表示倒序
    db.books.ensureIndex({number:1})
    1.3.再执行第一部的代码可以看出有数量级的性能提升


    2.索引使用需要注意的地方
    2.1.创建索引的时候注意1是正序创建索引-1是倒序创建索引
    2.2.索引的创建在提高查询性能的同事会影响插入的性能
    对于经常查询少插入的文档可以考虑用索引
    2.3.符合索引要注意索引的先后顺序
    2.4.每个键全建立索引不一定就能提高性能呢
    索引不是万能的
    2.5.在做排序工作的时候如果是超大数据量也可以考虑加上索引
    用来提高排序的性能

    3.索引的名称
    3.1用VUE查看索引名称


    3.2创建索引同时指定索引的名字
    db.books.ensureIndex({name:-1},{name:”bookname”})

    4.唯一索引
    4.1如何解决文档books不能插入重复的数值
    建立唯一索引
    db.books.ensureIndex({name:-1},{unique:true})
    试验
    db.books .insert({name:”1book”})


    5.踢出重复值
    5.1如果建议唯一索引之前已经有重复数值如何处理
    db.books.ensureIndex({name:-1},{unique:true,dropDups:true})
    6.Hint
    6.1如何强制查询使用指定的索引呢?
    db.books.find({name:"1book",number:1}).hint({name:-1})
    指定索引必须是已经创建了的索引

    7.Expain
    7.1如何详细查看本次查询使用那个索引和查询数据的状态信息
    db.books.find({name:"1book"}).explain()


    “cursor” : “BtreeCursor name_-1“ 使用索引
    “nscanned” : 1 查到几个文档
    “millis” : 0 查询时间0是很不错的性能

    1.system.indexes
    1.1在shell查看数据库已经建立的索引
    db.system.indexes.find()
    db.system.namespaces.find()
    2.后台执行
    2.1执行创建索引的过程会暂时锁表问题如何解决?
    为了不影响查询我们可以叫索引的创建过程在后台
    db.books.ensureIndex({name:-1},{background:true})
    3.删除索引
    3.1批量和精确删除索引
    db.runCommand({dropIndexes : ”books” , index:”name_-1”})
    db.runCommand({dropIndexes : ”books” , index:”*”})

    1.mongoDB提供强大的空间索引可以查询出一定范围的地理坐标.看例子
    准备数据map.json

    var map = [{
      "gis" : {
        "x" : 185,
        "y" : 150
      }
    },{
      "gis" : {
        "x" : 70,
        "y" : 180
      }
    },{
      "gis" : {
        "x" : 75,
        "y" : 180
      }
    },{
      "gis" : {
        "x" : 185,
        "y" : 185
      }
    },{
      "gis" : {
        "x" : 65,
        "y" : 185
      }
    },{
      "gis" : {
        "x" : 50,
        "y" : 50
      }
    },{
      "gis" : {
        "x" : 50,
        "y" : 50
      }
    },{
      "gis" : {
        "x" : 60,
        "y" : 55
      }
    },{
      "gis" : {
        "x" : 65,
        "y" : 80
      }
    },{
      "gis" : {
        "x" : 55,
        "y" : 80
      }
    },{
      "gis" : {
        "x" : 0,
        "y" : 0
      }
    },{
      "gis" : {
        "x" : 0,
        "y" : 200
      }
    },{
      "gis" : {
        "x" : 200,
        "y" : 0
      }
    },{
      "gis" : {
        "x" : 200,
        "y" : 200
      }
    }]
    for(var i = 0;i<map.length;i++){
        db.map.insert(map[i])
    }
    View Code

    1.查询出距离点(70,180)最近的3个点
    添加2D索引
    db.map.ensureIndex({"gis":"2d"},{min:-1,max:201})
    默认会建立一个[-180,180]之间的2D索引
    查询点(70,180)最近的3个点
    db.map.find({"gis":{$near:[70,180]}},{gis:1,_id:0}).limit(3)
    2.查询以点(50,50)和点(190,190)为对角线的正方形中的所有的点
    db.map.find({gis:{"$within":{$box:[[50,50],[190,190]]}}},{_id:0,gis:1})
    3.查询出以圆心为(56,80)半径为50规则下的圆心面积中的点
    db.map.find({gis:{$within:{$center:[[56,80],50]}}},{_id:0,gis:1})

  • 相关阅读:
    MySQL++:(转)mybatis 常用 jdbcType数据类型
    CF1556F Sports Betting (状压枚举子集DP)
    ICPC Greater New York Region 2020 F
    post方式实现导出/下载文件
    自定义一个v-if
    在vue项目中引用element-ui时 让el-input 获取焦点的方法
    element-select当下拉框数据过多使用懒加载
    vue强制刷新组件更新数据的方式
    .net core efcore dbfirst(sqlserver,mysql,oracle,postgresql)
    camunda安装配置mysql以及整合springboot
  • 原文地址:https://www.cnblogs.com/ecollab/p/7526550.html
Copyright © 2011-2022 走看看