{"id":3120,"date":"2023-12-30T07:03:33","date_gmt":"2023-12-30T07:03:33","guid":{"rendered":"https:\/\/kedar.nitty-witty.com\/blog\/?p=3120"},"modified":"2024-01-27T17:21:42","modified_gmt":"2024-01-27T17:21:42","slug":"how-to-filter-mysql-slow-queries-using-pt-query-digest","status":"publish","type":"post","link":"https:\/\/kedar.nitty-witty.com\/blog\/how-to-filter-mysql-slow-queries-using-pt-query-digest","title":{"rendered":"How to filter MySQL slow queries using pt-query-digest"},"content":{"rendered":"\r\n<p>The pt-query-digest is my go-to tool in order to perform slow query analysis, improve query performance and thus overall MySQL performance. In this blog post, I&#8217;m going to highlight the filter option which allows you to parse slow queries on various conditions for custom analysis.<\/p>\r\n\r\n\r\n\r\n<p>Save Time: If you&#8217;re already aware of pt-query-digest, jump straight to <a href=\"#filterusingptquerydigest\"><strong>Filtering MySQL slow query logs<\/strong><\/a> section.<\/p>\r\n\r\n\r\n<div class=\"wp-block-image\">\r\n<figure class=\"aligncenter size-full is-resized\"><img decoding=\"async\" class=\"wp-image-3118 alignright\" src=\"https:\/\/kedar.nitty-witty.com\/blog\/wp-content\/uploads\/2023\/12\/mysql-slow-log-filter.png\" alt=\"\" width=\"369\" height=\"432\" srcset=\"https:\/\/kedar.nitty-witty.com\/blog\/wp-content\/uploads\/2023\/12\/mysql-slow-log-filter.png 674w, https:\/\/kedar.nitty-witty.com\/blog\/wp-content\/uploads\/2023\/12\/mysql-slow-log-filter-256x300.png 256w, https:\/\/kedar.nitty-witty.com\/blog\/wp-content\/uploads\/2023\/12\/mysql-slow-log-filter-390x457.png 390w\" sizes=\"(max-width: 369px) 100vw, 369px\" \/><\/figure>\r\n<\/div>\r\n\r\n\r\n<p>Filtering capacity of pt-query-digest allows us to concentrate on specific condition and scenarios only. For example by excluding specific users, DBAs can identify slow queries linked to individual users. It is also possible to generate digest to analyse MySQL slow queries for specific database or table for targeted query reviews. You may also want to exclude a specific set of queries from the analysis, if they&#8217;re affecting the overall statistics. This level of granularity is good for DBAs trying to concentrate on answering specific slow queries within the MySQL environment.<\/p>\r\n\r\n\r\n\r\n<p>I am going to keep it short, covering top 5 slow log filtering options in this post. I&#8217;m going to start with setup and basic execution command.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\">Get pt-query-digest<\/h3>\r\n\r\n\r\n\r\n<p>The query analysis tool, pt-query-digest comes with Percona Toolkit. You may consider installing it from the official <a title=\"\" href=\"https:\/\/docs.percona.com\/percona-toolkit\/installation.html.\" target=\"_blank\" rel=\"noopener\">installation page<\/a>. Though the easiest and my favourite way to get any Percona tool is,<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>wget percona.com\/get\/pt-query-digest &amp;&amp; chmod +x pt-query-digest<\/code><\/pre>\r\n\r\n\r\n\r\n<p>You should be good to go as long as you have Perl, DBI, DBD::mysql, and some core packages that ought to be installed in any reasonably new version of Perl.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\">Generating Slow Query Digest<\/h3>\r\n\r\n\r\n\r\n<p>Generating slow query digest is straight forward task.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>pt-query-digest slow.log &gt; digest.txt<\/code><\/pre>\r\n\r\n\r\n\r\n<p>Depending on the size of slow log I&#8217;d consider executing this in screen session. There are plenty of options you can use to parse the slow log though that&#8217;s not the main topic of this blog.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">Filtering MySQL slow query logs<\/h2>\r\n\r\n\r\n\r\n<p>Let&#8217;s explore top 5 use cases while parsing MySQL slow query log, focusing on the pt-query-digest&#8217;s filtering capabilities.<\/p>\r\n\r\n\r\n\r\n<p id=\"filterusingptquerydigest\"><strong>Filtering slow queries by user using pt-query-digest<\/strong><\/p>\r\n\r\n\r\n\r\n<p>To analyze slow queries excluding a specific user (e.g., &#8220;peter_the_sos&#8221;)<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>pt-query-digest --filter '$event-&gt;{user} ne \"peter_the_sos\"' slow.log<\/code><\/pre>\r\n\r\n\r\n\r\n<p>If you need to include queries only by a specific user (eg. abuelo_vago), you may consider using &#8220;eq&#8221; instead of &#8220;ne&#8221;!<\/p>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\"><strong>Generating slow query digest for specific user<\/strong><\/h4>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>pt-query-digest --filter '$event-&gt;{user} eq \"abuelo_vago\"' slow.log<\/code><\/pre>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\"><strong>Ignore specific query in pt-query-digest<\/strong><\/h4>\r\n\r\n\r\n\r\n<p>Let&#8217;s say if you have to ignore a specific query that&#8217;s repeating a lot in the digest, you may consider the following expression.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>pt-query-digest --filter '$event-&gt;{fingerprint} !~ m\/CREATE DATABASE lab\/i' slow.log<\/code><\/pre>\r\n\r\n\r\n\r\n<p>Did you note the &#8220;i&#8221;? That&#8217;s ignoring the case while comparing strings. Guess you got the idea before I tell you!<\/p>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\"><strong>Ignore queries coming from specific server using pt-query-digest<\/strong><\/h4>\r\n\r\n\r\n\r\n<p>If you want to disregard queries from a particular host (e.g., &#8220;localhost&#8221;) you will need to use the &#8220;host&#8221; filter as follows:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>pt-query-digest --filter '$event-&gt;{host} ne \"localhost\"' slow.log<\/code><\/pre>\r\n\r\n\r\n\r\n<p>Ofcourse you know now, using &#8220;eq&#8221; will allow you to parse slow queries for specific server.<\/p>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\"><strong>Filtering slow queries by database using pt-query-digest<\/strong><\/h4>\r\n\r\n\r\n\r\n<p>To focus on queries related to a specific database (e.g., &#8220;alok_the_pop&#8221;) and generate digest only based on that database<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>pt-query-digest --filter '$event-&gt;{db} eq \"alok_the_pop\"'<\/code><\/pre>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\"><strong>Filtering slow queries by table using pt-query-digest<\/strong><\/h4>\r\n\r\n\r\n\r\n<p>To analyze slow queries pertaining to a specific table (e.g., &#8220;specialk&#8221;) use &#8220;fingerprint&#8221; event filter.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>pt-query-digest --filter '$event-&gt;{fingerprint} =~ m\/specialk\/' slow.log<\/code><\/pre>\r\n\r\n\r\n\r\n<p>Note that these are just 5 options to highlight the capabilities of pt-query-digest tool for analysing MySQL queries. I invite you to read more options available here the <a title=\"\" href=\"https:\/\/docs.percona.com\/percona-toolkit\/pt-query-digest.html#cmdoption-pt-query-digest-filter\" target=\"_blank\" rel=\"noopener\">official documentation<\/a>. Note that I am not including any other options and only filter, consider adjusting the command as you please. Also note that you will have to redirect this to a specific file to store the digest thus generated.<\/p>\r\n","protected":false},"excerpt":{"rendered":"The pt-query-digest is my go-to tool in order to perform slow query analysis, improve query performance and thus overall MySQL performance. In this blog post, I&#8217;m going to highlight the&hellip;\n","protected":false},"author":1,"featured_media":3118,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8,870,377],"tags":[33,829,832,834,427,833,830,825,828,602,831,635,827,599],"class_list":{"0":"post-3120","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-mysql","8":"category-mysql-tools","9":"category-mysql-articles","10":"tag-database","11":"tag-database-administration","12":"tag-filtering-queries","13":"tag-how-to-filter-mysql-queries","14":"tag-mysql","15":"tag-mysql-performance","16":"tag-mysql-tools","17":"tag-parse-mysql-slow-log","18":"tag-performance-tuning","19":"tag-pt-query-digest","20":"tag-query-analysis","21":"tag-query-optimization","22":"tag-slow-queries","23":"tag-slow-query-log"},"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/3120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/comments?post=3120"}],"version-history":[{"count":6,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/3120\/revisions"}],"predecessor-version":[{"id":3127,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/3120\/revisions\/3127"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/media\/3118"}],"wp:attachment":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/media?parent=3120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/categories?post=3120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/tags?post=3120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}