Why does the index size become larger after a large amount of data is deleted?

delete the historical data in the table and retain only the data of the most recent quarter

before deletion

"count" : 1379345, -sharp 
"size" : 3823247286, -sharp 
"nindexes" : 4,
"totalIndexSize" : 86913024, -sharp 
"indexSizes" : {
    "_id_" : 12873728,
    "created_at_1" : 17534976,
    "storage_time_1" : 7397376,
    "guid_1" : 49106944
},

after deletion

"count" : 453978,
"size" : 2478837459,
"nindexes" : 4,
"totalIndexSize" : 89522176,
"indexSizes" : {
    "_id_" : 12873728,
    "created_at_1" : 18096128,
    "storage_time_1" : 7397376,
    "guid_1" : 51154944
},

before VS deletion
count: 1379345 VS 453978 diff: 925367
size: 3823247286 VS 2478837459 diff: 1344409827
totalIndexSize: 86913024 VS 89522176 diff:-2609152

Why does the index size become larger after so much data is deleted?

May.30,2021

in the WiredTiger engine, each index corresponds to a separate file on disk. After deleting a large amount of data, the corresponding index in this file is also deleted. But the space will not be released, just reuse as much as possible. Because if you want to free up this space, you must concentrate the free space at the end of the file and truncate it. No database will take the initiative to do this, which is inefficient and doesn't make much sense.
if you really want to do it, learn about the compact command, which is slow and blocked. Strongly discouraged.

Menu