在Mong中创建索引

2024-06-12 07:45:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我的数据集格式如下:

{
  u'geometry': {
    u'type': u'Point',
    u'coordinates': [157.0625, -24.36573]
  },
  u'_id': ObjectId('5ad70f71f2119236741ffad6'),
  u'type': u'Feature',
  u'properties': {
    u'STATUS': u'8',
    u'TIMESTAMP': u'2013-12-31 17:03:00.000',
    u'MMSI': u'636015036',
    u'SPEED': u'98'
  }
}

我创建了一个查询,返回按MMSI分组并排序的船舶的位置(坐标)。Total:具有相同MMSI的记录数,COORDINATES:在数组中收集的相同MMSI的坐标,_id:船的MMSI。你知道吗

查询(pymongo):

getPositionsOfshipsgrouped = db.samplecol.aggregate([
  { "$match": { "properties.MMSI": { "$exists": "true" } } },
  {
    "$project": {
      "properties.MMSI": "$properties.MMSI",
      "geometry.coordinates": "$geometry.coordinates"
    }
  },
  {
    "$group": {
      "_id": "$properties.MMSI",
      "total": { "$sum": 1 },
      "COORDINATES": { "$push": "$geometry.coordinates" }
    }
  },
  { "$match": { "total": { "$gte": 0 } } },
  { "$sort": { "total": -1 } },
  { "$limit": 15 }
])

结果:

{
  u'total': 10,
  u'_id': u'503551000',
  u'COORDINATES': [
    [141.8705, -12.67311],
    [158.1707, -0.9142034],
    [157.1707, -0.9142034],
    [157.1707, -0.9142034],
    [157.1707, -0.9142034],
    [157.1707, -0.9142034],
    [159.1707, -0.8142034],
    [158.2707, -0.8142034],
    [159.1707, -0.8142034],
    [159.1707, -0.8142034]
  ]
}

{
  u'total': 2,
  u'_id': u'416243700',
  u'COORDINATES': [
    [159.1707, -0.8142034],
    [159.0707, -0.7142034]
  ]
}

我认为在这里创建索引是个好主意。 首先,我在几何体中创建一个地理空间索引:

db.samplecol.ensure_index([("geometry", "2dsphere")])

然后我创建单键索引,如:

db.samplecol.ensure_index([
  ("properties.MMSI", 1),
  ("geometry.coordinates", 1),
  ("properties.TIMESTAMP", 1)
])

在创建索引时,我使用$project中的字段(在上面的查询中)。你知道吗

所以我的问题是,这些索引创建是否能确保更好的性能,以及我使用的字段是否正确。 我有一个小的数据集,在响应时间方面看不到更好的性能。你知道吗


Tags: 数据projectiddbtypematchpropertiestimestamp