/// <summary> /// Note that you need to have a populated database to verify explain plans. You can make sure it's doing what you expect with the HandleExplainPlan /// callback. There's a few helper methods for that, such as EnsureNoSeqScan; see the source on github for examples. /// </summary> public static void ValidateAndExplainCommand(this CommandBuilder command, NpgsqlConnection connection, HandleExplainPlan handleExplain) => command.Explain(connection, handleExplain);
public static void Explain(this CommandBuilder command, NpgsqlConnection connection, HandleExplainPlan analyzeSqlExecutionPlan) { string content = ""; using (connection) using (var cmd = command.BuildFrom(connection, command.Parameters.ToDictionary(kvp => kvp.Key, kvp => (object)DBNull.Value))) { cmd.CommandText = "EXPLAIN (FORMAT JSON, VERBOSE) " + string.Join("; EXPLAIN", cmd.CommandText.Split(';')); foreach (NpgsqlParameter param in cmd.Parameters) { param.Direction = System.Data.ParameterDirection.Input; } connection.Open(); content = cmd.ExecuteScalar().ToString(); } analyzeSqlExecutionPlan(JToken.Parse(content)); }