Closed mirror222 closed 4 months ago
Hi @mirror222
I guess that LiteDB first uses the index to perform the conditional query, and then sorts (FileSort)?
That's it. You can see explain plan in your query using EXPLAIN (in sql) or using GetPlan()
var queryPlan = _dbThreads.Query().Where(x => x.LabelIds.Contains("NOTICE")).GetPlan();
Print(queryPlan);
void Print(BsonDocument doc)
{
var sb = new StringBuilder();
var sw = new StringWriter(sb);
var w = new JsonWriter(sw);
w.Pretty = true;
w.Serialize(doc);
Console.WriteLine(sb);
}
{
"collection": "CThreads",
"snaphost": "read",
"pipe": "queryPipe",
"index":
{
"name": "label_index",
"expr": "$.LabelIds[*]",
"order": 1,
"mode": "INDEX SEEK(label_index = \"NOTICE\")",
"cost": 10
},
"lookup":
{
"loader": "document",
"fields": "$"
},
"select":
{
"expr": "$",
"all": false
}
}
Where ONLY : 26 ms
{
"collection": "CThreads",
"snaphost": "read",
"pipe": "queryPipe",
"index":
{
"name": "date_index",
"expr": "$.InternalDate",
"order": -1,
"mode": "FULL INDEX SCAN(date_index)",
"cost": 100
},
"lookup":
{
"loader": "document",
"fields": "$"
},
"select":
{
"expr": "$",
"all": false
}
}
OrderBy ONLY: 5 ms
{
"collection": "CThreads",
"snaphost": "read",
"pipe": "queryPipe",
"index":
{
"name": "label_index",
"expr": "$.LabelIds[*]",
"order": 1,
"mode": "INDEX SEEK(label_index = \"NOTICE\")",
"cost": 10
},
"lookup":
{
"loader": "document",
"fields": "$"
},
"orderBy":
{
"expr": "$.InternalDate",
"order": -1
},
"select":
{
"expr": "$",
"all": false
}
}
Both Where and OrderBy : 1239 ms
LiteDB 5.0.18 Net 8.0/6.0
I have a large table with over 500,000 records, and I have created indexes on the LabelIds and InternalDate fields. I am now encountering a performance issue, as shown in the following code:
The execution results are shown in the following figure:
I guess that LiteDB first uses the index to perform the conditional query, and then sorts (FileSort)?