答案MongoDB - can't use .explain() to get query info in C# code?或Is there an "Explain Query" for MongoDB Linq? 已经过时,并且在最新版本的C#驱动程序中不再存在类和方法。
答案 0 :(得分:0)
作为一种解决方法,我能够使用带有选项重载的find语法在要调整的查询上运行解释。
[Fact]
public async Task Metrics()
{
var options = new FindOptions
{
Modifiers = new BsonDocument("$explain", true)
};
var queryable = await MongoDb.Collection.Find("{ Paid: false}", options).As<StatsBsonDocument>().ToListAsync(); // StatsBsonDocument BsonDocument
//Console.WriteLine(queryable.First());
queryable.First().GetStats();
}
public class StatsBsonDocument
{
public dynamic queryPlanner { get; set; }
public ExecutionStats executionStats { get; set; }
public dynamic serverInfo { get; set; }
public void GetStats()
{
try
{
Console.WriteLine($"{queryPlanner.winningPlan.inputStage.stage}");
}
catch (Exception e)
{
Console.WriteLine($"executionSuccess {executionStats.executionSuccess}, stage {queryPlanner.winningPlan.stage},");
}
Console.WriteLine($"{executionStats.nReturned} returned documents, {executionStats.executionTimeMillis}ms execution");
Console.WriteLine($"{executionStats.totalKeysExamined} keys examined , {executionStats.totalDocsExamined} documents examined");
Console.WriteLine($"index vs returned {(decimal)executionStats.totalKeysExamined / executionStats.nReturned}, " +
$"scanned documents/returned {(decimal)executionStats.totalDocsExamined / executionStats.nReturned}");
Console.WriteLine();
// AND_SORTED stage or an AND_HASH stage. index intersection
}
}
public class QueryPlanner
{
public dynamic plannerVersion;
public string Namespace;
public dynamic indexFilterSet;
public dynamic parsedQuery;
public dynamic winningPlan;
public dynamic rejectedPlans;
}
public class ExecutionStats
{
public bool executionSuccess;
public int nReturned;
public int executionTimeMillis;
public int totalKeysExamined;
public int totalDocsExamined;
public dynamic executionStages;
public dynamic allPlansExecution;
}