【问题标题】:Can I run an "explain analyze" on a query using JOOQ?我可以使用 JOOQ 对查询运行“解释分析”吗?
【发布时间】:2020-11-07 04:53:25
【问题描述】:

我可以在 JOOQ 中的查询上运行 explain analyze 吗?喜欢:

explain analyse select some, columns from some_table

但是在 PostgreSQL 数据库上使用 JOOQ 吗?

我找到了一个接口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

【问题讨论】:

    标签: java postgresql jooq explain


    【解决方案1】:

    是的,您可以运行解释。示例

    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();
    

    【讨论】:

      【解决方案2】:

      尚不支持:https://github.com/jOOQ/jOOQ/issues/10424。请改用plain SQL templating

      ctx.fetch("explain analyze {0}", select);
      

      【讨论】:

        猜你喜欢
        • 2019-08-16
        • 2022-01-22
        • 2021-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-14
        • 2022-10-07
        相关资源
        最近更新 更多