After mongodb deletes the data, the sorting query becomes very slow

  mongodb, question

As shown in the figure:

clipboard.png

As you can see on the left, I have indexed the _dateline field. But sorting by _dateline becomes very slow, taking tens of seconds.

If sort is removed, it will be very fast.

There was no such situation before. This table had about 80 million pieces of data before, but now it has been deleted, leaving 20 million pieces of data, and the query has become slower.

I guess it is related to deletion. After deleting the data, do you still need to reconstruct the index or something?


I finally deleted the _dateline index before creating it. Now the speed has become very fast. But I don’t understand the principle.
After deleting the data, why is the index invalid?

It seems that you do not fully understand the working mode of index. Simply put, the correct index you need is:

db.log.createIndex({picker_id: 1, _dateline: -1})

And this index can replace{picker_id: 1}.
For indexes, please refer to official documents:Indexes.
The bug of low index performance occurred once at 3.0.x after deleting a large amount of data. I’m not sure what you are.