Forum OpenACS Q&A: a simple way to profile database queries

add this at the beginning of db_exec in 10-database-procs.tcl:
nsv_set_or_incr db_debug_counter $statement_name
where nsv_set_or_incr is defined as:
proc_doc nsv_set_or_incr {set key {delta 1}} {
        if $key exists in $set, incr it, else set to $delta
} {
        if [catch {
                        nsv_incr $set $key $delta
        }] {
                nsv_set $set $key $delta
        }
}
Then getting a list of candidates for optimization is as simple as this page:
set l {}
foreach {key value} [nsv_array get db_debug_counter] {
    lappend l [list $key $value]
}
set l [lsort -integer -index 1 $l]

ns_return 200 text/html "
<ul><li>[join $l <li>]</ul>
"
db_exec could easily be extended to time the queries as well.
Collapse
Posted by Jade Rubick on
This doesn't seem like it would have much of a performance impact. Would this be a candidate for addition to the core code? It'd be nice to have an admin page to go to and see which queries have been executed the most.
Collapse
Posted by Caroline Meeks on
Can this be added to the existing Developer Support package?  I've never looked under the hood but it works great to time and display the queries on a page.  I find it extremely useful for optimizing pages.

I usually turn it off on production sites.  A lighter weight mode that you could leave on for a period of time on production and collect overall statistics would be cool.