可以使用Postgresql的Explain来分析Grails GORM生成的查询

发布于 2024-12-13 16:16:30 字数 1142 浏览 2 评论 0原文

我发现并使用了一个闭包,可以暂时将 hibernate.SQL 日志记录转换为 Trace,以便我可以查看生成的确切查询。不过,我希望能够自动运行 PostgresQL 的解释,而不必单独提取查询进行分析。

日志关闭: (在这里找到:http://www.intelligrape.com/blog/2011/10/21/log-sql-in-grails-for-a-piece-of-code/

 public static def execute(Closure closure) {
    Logger sqlLogger = Logger.getLogger("org.hibernate.SQL");
    Logger transactionLogger = Logger.getLogger("org.hibernate.transaction");
    Level currentLevel = sqlLogger.level
    Level transLevel = transactionLogger.level
    sqlLogger.setLevel(Level.TRACE)
    transactionLogger.setLevel(Level.TRACE)
    def result = closure.call()
    sqlLogger.setLevel(currentLevel)
    transactionLogger.setLevel(transLevel)
    result
}

用法:

def result
    execute{
        result=Dog.createCriteria().list{
            eq("breed","Greyhound")
        }
    }

我想要的东西可以使用以类似的方式。

我可以用 Criteria 或 Hibernate.Restrictions 的子类来做这件事吗?

或者我在文档中缺少关于如何修改从 GORM 发送到数据库的 SQL 语句的内容吗?

感谢您提供任何信息。

I found and have used a closure that temporarily turns hibernate.SQL logging to Trace to allow me to see the exact queries that are generated. However I would like to be able to have PostgresQL's explain run automatically instead of having to pull out queries individually for analysis.

logging closure:
(found here: http://www.intelligrape.com/blog/2011/10/21/log-sql-in-grails-for-a-piece-of-code/)

 public static def execute(Closure closure) {
    Logger sqlLogger = Logger.getLogger("org.hibernate.SQL");
    Logger transactionLogger = Logger.getLogger("org.hibernate.transaction");
    Level currentLevel = sqlLogger.level
    Level transLevel = transactionLogger.level
    sqlLogger.setLevel(Level.TRACE)
    transactionLogger.setLevel(Level.TRACE)
    def result = closure.call()
    sqlLogger.setLevel(currentLevel)
    transactionLogger.setLevel(transLevel)
    result
}

usage:

def result
    execute{
        result=Dog.createCriteria().list{
            eq("breed","Greyhound")
        }
    }

I would like something that can be used in a similar way.

Is this something I could do with a sub-class of Criteria or Hibernate.Restrictions ?

Or is there something I'm missing in the docs on how to modify the SQL statement that is sent to the DB from GORM?

Thanks for any info.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他是夢罘是命 2024-12-20 16:16:30

假设您不需要所有查询,而只需要运行时间较长的查询,您可能会发现 PostgreSQL 的“自动解释”插件很有用。

http://www.postgresql.org/docs/9.1/static/auto -解释.html

Assuming you don't want all queries, but only the longer-running ones you might find the "auto explain" add-on for PostgreSQL useful.

http://www.postgresql.org/docs/9.1/static/auto-explain.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文