I need to debug a Grails application with one really slow request. I have SQL logging but would like to see the amount of SQL-queries without counting them manually.
我需要使用一个非常慢的请求来调试Grails应用程序。我有SQL日志记录,但希望看到SQL查询的数量而不用手动计算它们。
debug 'org.hibernate.SQL'
trace 'org.hibernate.type'
For eaxmple to have following line after each request (where x is the amount of all queries made to SQL server):
对于eaxmple,在每个请求之后有以下行(其中x是对SQL Server进行的所有查询的数量):
[2012-10-04 13:41:45,049][LoggingFilters] INFO - Request finished in 8296 ms and made x SQL statements
[2012-10-04 13:41:45,049] [LoggingFilters] INFO - 请求在8296 ms内完成并生成x SQL语句
After some googling this doesn't seem to be possible with Grails so maybe MySQL could provide the information?
经过一些谷歌搜索,这似乎与Grails不可能,所以也许MySQL可以提供信息?
4 个解决方案
#1
9
You can do it by using Filters and Hibernate statistics. Create class ExampleFilters.groovy in conf folder. This is the content of the class:
您可以使用Filters和Hibernate统计信息来完成。在conf文件夹中创建类ExampleFilters.groovy。这是班级的内容:
import org.hibernate.stat.Statistics
class ExampleFilters {
def sessionFactory
def filters = {
// your filters here
logHibernateStats(controller: '*', action: '*') {
before = {
Statistics stats = sessionFactory.statistics;
if(!stats.statisticsEnabled) {stats.setStatisticsEnabled(true)}
}
afterView = {
Statistics stats = sessionFactory.getStatistics()
double queryCacheHitCount = stats.getQueryCacheHitCount();
double queryCacheMissCount = stats.getQueryCacheMissCount();
double queryCacheHitRatio = (queryCacheHitCount / ((queryCacheHitCount + queryCacheMissCount) ?: 1))
println """
######################## Hibernate Stats ##############################################
Transaction Count:${stats.transactionCount}
Flush Count:${stats.flushCount}
Total Collections Fetched:${stats.collectionFetchCount}
Total Collections Loaded:${stats.collectionLoadCount}
Total Entities Fetched:${stats.entityFetchCount}
Total Entities Loaded:${stats.entityFetchCount}
Total Queries:${stats.queryExecutionCount}
queryCacheHitCount:${queryCacheHitCount}
queryCacheMissCount:${queryCacheMissCount}
queryCacheHitRatio:${queryCacheHitRatio}
######################## Hibernate Stats ##############################################
"""
stats.clear()
}
}
}
}
For reading more about various Hibernate statistics read this article: http://www.javalobby.org/java/forums/t19807.html
有关各种Hibernate统计信息的更多信息,请阅读本文:http://www.javalobby.org/java/forums/t19807.html
Also note that there is a performance impact when using this, so it should really be used only in development environment.
另请注意,使用它时会对性能产生影响,因此它应该只在开发环境中使用。
#2
1
Have you considered some low tech solutions like running the output through wc -l
?
您是否考虑过一些低技术解决方案,例如通过wc -l运行输出?
#3
0
in grails use loggingSql
在grails中使用loggingSql
dataSource {
dbCreate = "update" // one of 'create', 'create-drop','update'
url = "jdbc:postgresql://localhost:5432/demodb"
loggingSql = true
}
you’ll notice that all SQL statements Grails utilize will be logged.
您会注意到将记录Grails使用的所有SQL语句。
#4
0
As Burt Beckwith said in his blog post "Stuff I Learned Consulting" http://burtbeckwith.com/blog/?p=1570
正如Burt Beckwith在他的博客文章“Stuff I Learned Consulting”中所说的那样http://burtbeckwith.com/blog/?p=1570
SQL Logging
There are two ways to view SQL output from queries; adding logSql = true in DataSource.groovy and configuring Log4j loggers. The Log4j approach is a lot more flexible since it doesn’t just dump to stdout, and can be routed to a file or other appender and conveniently enabled and disabled. But it turns out it’s easy to toggle logSql SQL console logging. Get a reference to the sessionFactory bean (e.g. using dependency injection with def sessionFactory) and turn it on with
有两种方法可以查看查询的SQL输出;在DataSource.groovy中添加logSql = true并配置Log4j记录器。 Log4j方法更加灵活,因为它不仅仅转储到stdout,而且可以路由到文件或其他appender,并且可以方便地启用和禁用。但事实证明,切换logSql SQL控制台日志记录很容易。获取对sessionFactory bean的引用(例如,使用def sessionFactory的依赖注入)并将其打开
sessionFactory.settings.sqlStatementLogger.logToStdout = true
and off with
sessionFactory.settings.sqlStatementLogger.logToStdout = false
#1
9
You can do it by using Filters and Hibernate statistics. Create class ExampleFilters.groovy in conf folder. This is the content of the class:
您可以使用Filters和Hibernate统计信息来完成。在conf文件夹中创建类ExampleFilters.groovy。这是班级的内容:
import org.hibernate.stat.Statistics
class ExampleFilters {
def sessionFactory
def filters = {
// your filters here
logHibernateStats(controller: '*', action: '*') {
before = {
Statistics stats = sessionFactory.statistics;
if(!stats.statisticsEnabled) {stats.setStatisticsEnabled(true)}
}
afterView = {
Statistics stats = sessionFactory.getStatistics()
double queryCacheHitCount = stats.getQueryCacheHitCount();
double queryCacheMissCount = stats.getQueryCacheMissCount();
double queryCacheHitRatio = (queryCacheHitCount / ((queryCacheHitCount + queryCacheMissCount) ?: 1))
println """
######################## Hibernate Stats ##############################################
Transaction Count:${stats.transactionCount}
Flush Count:${stats.flushCount}
Total Collections Fetched:${stats.collectionFetchCount}
Total Collections Loaded:${stats.collectionLoadCount}
Total Entities Fetched:${stats.entityFetchCount}
Total Entities Loaded:${stats.entityFetchCount}
Total Queries:${stats.queryExecutionCount}
queryCacheHitCount:${queryCacheHitCount}
queryCacheMissCount:${queryCacheMissCount}
queryCacheHitRatio:${queryCacheHitRatio}
######################## Hibernate Stats ##############################################
"""
stats.clear()
}
}
}
}
For reading more about various Hibernate statistics read this article: http://www.javalobby.org/java/forums/t19807.html
有关各种Hibernate统计信息的更多信息,请阅读本文:http://www.javalobby.org/java/forums/t19807.html
Also note that there is a performance impact when using this, so it should really be used only in development environment.
另请注意,使用它时会对性能产生影响,因此它应该只在开发环境中使用。
#2
1
Have you considered some low tech solutions like running the output through wc -l
?
您是否考虑过一些低技术解决方案,例如通过wc -l运行输出?
#3
0
in grails use loggingSql
在grails中使用loggingSql
dataSource {
dbCreate = "update" // one of 'create', 'create-drop','update'
url = "jdbc:postgresql://localhost:5432/demodb"
loggingSql = true
}
you’ll notice that all SQL statements Grails utilize will be logged.
您会注意到将记录Grails使用的所有SQL语句。
#4
0
As Burt Beckwith said in his blog post "Stuff I Learned Consulting" http://burtbeckwith.com/blog/?p=1570
正如Burt Beckwith在他的博客文章“Stuff I Learned Consulting”中所说的那样http://burtbeckwith.com/blog/?p=1570
SQL Logging
There are two ways to view SQL output from queries; adding logSql = true in DataSource.groovy and configuring Log4j loggers. The Log4j approach is a lot more flexible since it doesn’t just dump to stdout, and can be routed to a file or other appender and conveniently enabled and disabled. But it turns out it’s easy to toggle logSql SQL console logging. Get a reference to the sessionFactory bean (e.g. using dependency injection with def sessionFactory) and turn it on with
有两种方法可以查看查询的SQL输出;在DataSource.groovy中添加logSql = true并配置Log4j记录器。 Log4j方法更加灵活,因为它不仅仅转储到stdout,而且可以路由到文件或其他appender,并且可以方便地启用和禁用。但事实证明,切换logSql SQL控制台日志记录很容易。获取对sessionFactory bean的引用(例如,使用def sessionFactory的依赖注入)并将其打开
sessionFactory.settings.sqlStatementLogger.logToStdout = true
and off with
sessionFactory.settings.sqlStatementLogger.logToStdout = false