Esempio n. 1
0
        public static QueryPlan ExplainQuery(this IManagedConnection runner, NpgsqlCommand cmd, Action <IConfigureExplainExpressions> configureExplain = null)
        {
            var serializer = new JsonNetSerializer();

            var config = new ConfigureExplainExpressions();

            configureExplain?.Invoke(config);

            cmd.CommandText = string.Concat($"explain ({config} format json) ", cmd.CommandText);
            return(runner.Execute(cmd, c =>
            {
                using (var reader = cmd.ExecuteReader())
                {
                    var queryPlans = reader.Read() ? serializer.FromJson <QueryPlanContainer[]>(reader.GetTextReader(0)) : null;
                    var planToReturn = queryPlans?[0].Plan;
                    if (planToReturn != null)
                    {
                        planToReturn.PlanningTime = queryPlans[0].PlanningTime;
                        planToReturn.ExecutionTime = queryPlans[0].ExecutionTime;
                        planToReturn.Command = cmd;
                    }
                    return planToReturn;
                }
            }));
        }
Esempio n. 2
0
        public static QueryPlan ExplainQuery(this IManagedConnection runner, NpgsqlCommand cmd)
        {
            var serializer = new JsonNetSerializer();

            cmd.CommandText = string.Concat("explain (format json) ", cmd.CommandText);
            return(runner.Execute(cmd, c =>
            {
                using (var reader = cmd.ExecuteReader())
                {
                    var queryPlans = reader.Read() ? serializer.FromJson <QueryPlanContainer[]>(reader.GetString(0)) : null;
                    return queryPlans?[0].Plan;
                }
            }));
        }