LoginSignup
1
2

More than 5 years have passed since last update.

pymongoでhintを指定する

Posted at

indexが増えてしまい、意図しないindexが先に当たって遅くなることがあったので、hintを与えてindexを指定する必要が出てきた。
データ構造を変えられないのでこれからお世話になるであろう為メモ
c⌒っ゚д゚)っφ メモメモ...

        spec = {
            'user_id': {'$in': user_id_list},
            'type': 'a',
            'status': 0,
            'at': {'$lte': datetime.datetime.utcnow()}
        }
        cursor = db.collection.find(spec)
        cursor.sort('at', -1).limit(500)
        cursor.hint([
            ('at', pymongo.DESCENDING),
            ('user_id', pymongo.ASCENDING),
            ('type', pymongo.ASCENDING),
            ('status', pymongo.ASCENDING),
            ('_id', pymongo.DESCENDING),
        ])
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2