【问题标题】:How to use Oracle's APPEND hint with JOOQ-rendered INSERT statements?如何将 Oracle 的 APPEND 提示与 JOOQ 呈现的 INSERT 语句一起使用?
【发布时间】:2016-05-17 12:28:32
【问题描述】:

我有一个 JOOQ 查询,如下所示:

dsl.insertInto(AUTHOR_ARCHIVE)
   .select(selectFrom(AUTHOR).where(AUTHOR.DECEASED.isTrue()));

我想利用Oracle's /*+ APPEND */ hint 来提高性能。

但是,JOOQ's documentation on oracle hints 不包含INSERT 语句的示例。如何使用 JOOQ 3.8 在上述查询中注入附加提示?

【问题讨论】:

    标签: java sql-insert jooq


    【解决方案1】:

    jOOQ 目前不支持 DML 的提示,但在路线图上:https://github.com/jOOQ/jOOQ/issues/2654

    与此同时,您的选择是:

    求助于plain SQL

    例如通过DSLContext.execute(String)。通常,您会通过Query.getSQL()Query.getBindValues() 从jOOQ Query 中提取SQL。例如:

    Query query = ctx.insertInto(...).values(...);
    ctx.execute(
        query.getSQL().replace("insert into", "insert /*+APPEND*/ into"), 
        query.getBindValues().toArray()
    );
    

    自己实现 DML 提示支持

    通过ExecuteListener 修补在renderEnd() 事件上生成的SQL,只要某些标志设置为true。您可以在Configuration.data() 中添加标志,例如:

    public class MyListener extends DefaultExecuteListener {
        @Override
        public void renderEnd(ExecuteContext ctx) {
            if (ctx.data().containsKey("insert hint")) {
                ctx.sql(ctx.sql().replace(
                    "insert into", 
                    "insert " + ctx.data().get("insert hint") + " into"
                );
            }
        }
    }
    

    然后:

    Configuration withHint = usualConfiguration.derive();
    withHint.data("insert hint", "/*+APPEND*/");
    
    DSL.using(withHint)
       .insertInto(...).values(...).execute();
    

    【讨论】:

      猜你喜欢
      • 2018-07-30
      • 1970-01-01
      • 2018-07-31
      • 2017-05-14
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多