efcore icon indicating copy to clipboard operation
efcore copied to clipboard

ToQueryPlan to... get the query plan...

Open roji opened this issue 5 years ago • 12 comments

Very cool idea suggested by @lauxjpn: we now have ToQueryString, we could also get the query plan from the database.

SQL Server:

SET SHOWPLAN_ALL ON;
<query>
SET SHOWPLAN_ALL OFF;

PostgreSQL / SQLite

EXPLAIN <query>

roji avatar Oct 28 '20 20:10 roji

When this has been implemented, this basically screams for an extension, that allows users to specify execution duration limits, that will log the query together with the query plan, once those limits get hit.

lauxjpn avatar Oct 29 '20 11:10 lauxjpn

We may as well just find out what's wrong with the queries and fix it too :rofl:

Seriously, this is a really nice direction.

roji avatar Oct 29 '20 13:10 roji

This has challenges that ToQueryString doesn't have--most notably that it requires a connection to the database to run the "explain" query. So this probably needs a bit more thought, but integrating this in some way with EF Core would be very useful.

ajcvickers avatar Oct 30 '20 21:10 ajcvickers

Also not all users have showplan rights with SQL Server..

ErikEJ avatar Oct 31 '20 06:10 ErikEJ

I just wrote an extension method along these lines

static class SqliteExtensions
{
    public static String ToQueryPlan(this IQueryable queryable)
    {
          using var command = queryable.CreateDbCommand();
          command.CommandText = "explain query plan " + command.CommandText;
          using var reader = command.ExecuteReader();
          reader.Read();
          return reader.GetString(3);
    }
    
    
    public static async Task<String> ToQueryPlanAsync(this IQueryable queryable, CancellationToken ct = default)
    {
        await using var command = queryable.CreateDbCommand();
        command.CommandText = "explain query plan " + command.CommandText;
        await using var reader = await command.ExecuteReaderAsync(ct);
        await reader.ReadAsync(ct);
        return reader.GetString(3);
    }
}

NickStrupat avatar Nov 10 '21 01:11 NickStrupat

It's not a method but I built a VS debugger visualizer to view query plans inside Visual Studio: EFCoreVisualizer

Requires VS 17.9 which was released yesterday.

SqlPlan2

Giorgi avatar Feb 14 '24 11:02 Giorgi

@Giorgi this is super nicer! Obviously the visualization is also much better than any text-based dump we could integrate.

Did you write the visualization yourself, or could you leverage any existing SQL Server component to do that for you?

roji avatar Feb 14 '24 12:02 roji

All the credit for visualization goes to html-query-plan and pev2, I wrote glue code only :smiley:

Giorgi avatar Feb 14 '24 12:02 Giorgi

@Giorgi you should get it added to the tools list in the docs

ErikEJ avatar Feb 14 '24 12:02 ErikEJ

Super cool!

roji avatar Feb 14 '24 12:02 roji

@lauxjpn Does mysql have a graphical query plan format?

ErikEJ avatar Feb 14 '24 12:02 ErikEJ

@ErikEJ mysql-workbench has some code for displaying a graphical query plan.

Giorgi avatar Feb 14 '24 12:02 Giorgi

@ErikEJ mysql-workbench has some code for displaying a graphical query plan.

Seems to be done by the wb.query.analysis python plugin.

10.8.2 EXPLAIN Output Format describes the format that EXPLAIN outputs.

lauxjpn avatar Mar 08 '24 09:03 lauxjpn