Tracking SQL queries

Events happening in the community are now at Drupal community events on www.drupal.org.
jacobbednarz's picture

After spending quite a while trying to diagnose the origin of some slow SQL queries, I looked around for some solutions to better identify the query and lessen the time spent for the initial investigation.

One of the options I found was sending through a comment with the SQL which included information such as the class or function. I've wrote an article about doing it here (https://jacobbednarz.github.io/posts/tracking-sql-queries/) but I was wondering if you had any feedback on how you go about tracking what methods or url's are sending what SQL queries within Drupal.

Comments

Would it be possible to hook

beardedgeek's picture

Would it be possible to hook this into every query? Obviosuly not permanently, but on a switch.

I did have a look around at

jacobbednarz's picture

I did have a look around at trying to add these globally however I didn't come up with anything that really worked.

I tried using hook_views_query_alter() however it doesn't look like it allows you to tack on the comment via the provided $query object. Additionally, I looked into using hook_query_alter() but that also looks like it requires the queries to be explicitly tagged to be accessible via that hook.

Hopefully someone else may know of some hooks that could work - even partially.

Just after posting the above

jacobbednarz's picture

Just after posting the above message, I decided to go through another portion of the site and I managed to get the following working and showing up in the query as intended.

function hook_query_alter($query) {
  $query->comment('my sample comment');
}

In doing this, I'm not sure how widespread 'tagging' of queries is so I think the mileage on this one will vary based on your setup. Getting the parent (or caller) from this position may be a little awkward too if you were trying to push anything dynamic in as it will only have reference of the hook it is being executed from.

Devel Module

mikeytown2's picture

I use the devel module for tracking queires. You can even make your own logging object to track other things; I do that inside of the Asynchronous Prefetch Database Query Cache module since it's not a PDO connection. Inside of http://cgit.drupalcode.org/apdqc/tree/apdqc.log.inc the findCallerfunction might be of interest here.

Inside of

jacobbednarz's picture

Inside of http://cgit.drupalcode.org/apdqc/tree/apdqc.log.inc the findCallerfunction might be of interest here.

Thanks! Will give it a whirl and see if I can push this any further.

High performance

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: