Могу ли я выполнить анализ объяснения запроса с помощью JOOQ?

Могу ли я запустить explain analyze по запросу в JOOQ? как:

explain analyse select some, columns from some_table

но сделать это с помощью JOOQ в базе данных PostgreSQL?

Я нашел интерфейс org.jooq.Explain с методом DSLContext.explain​(Query query), но, кажется, просто использую EXPLAIN в запросе:

@Support({AURORA_MYSQL,AURORA_POSTGRES,H2,HSQLDB,MARIADB,MEMSQL,MYSQL,ORACLE,POSTGRES,SQLITE}) 
Explain explain​(Query query)
Run an EXPLAIN statement in the database to estimate the cardinality of the query.

Есть ли разумный способ запустить EXPLAIN ANALYZE в базе данных со стороны кода?


person dziki    schedule 17.07.2020    source источник


Ответы (2)


Да, вы можете запустить объяснить. Пример

SelectWhereStep<ModuldefRecord> where = dsl.selectFrom(MODULDEF);
Explain explain = dsl().explain(where);

System.out.println(explain);

Вывод выглядит так (для Oracle)

+------------------------------------------------------------------------------+
|PLAN_TABLE_OUTPUT                                                             |
+------------------------------------------------------------------------------+
|Plan hash value: 3871168833                                                   |
|                                                                              |
|------------------------------------------------------------------------------|
|| Id  | Operation         | Name     | Rows  | Bytes | Cost (%CPU)| Time     ||
|------------------------------------------------------------------------------|
||   0 | SELECT STATEMENT  |          | 61303 |    30M|  1305   (1)| 00:00:01 ||
||   1 |  TABLE ACCESS FULL| MODULDEF | 61303 |    30M|  1305   (1)| 00:00:01 ||
|------------------------------------------------------------------------------|
+------------------------------------------------------------------------------+

Объяснение также содержит строки и стоимость

    /**
     * The number of rows (cardinality) that is estimated to be returned by the query.
     * <p>
     * This returns {@link Double#NaN} if rows could not be estimated.
     */
    double rows();

    /**
     * The cost the database associated with the execution of the query.
     * <p>
     * This returns {@link Double#NaN} if cost could not be retrieved.
     */
    double cost();
person Simon Martinelli    schedule 17.07.2020

Это еще не поддерживается: https://github.com/jOOQ/jOOQ/issues/10424. Вместо этого используйте простой шаблон SQL:

ctx.fetch("explain analyze {0}", select);
person Lukas Eder    schedule 17.07.2020