{"id":105,"date":"2011-08-31T19:59:27","date_gmt":"2011-08-31T16:59:27","guid":{"rendered":"http:\/\/meekconsulting.com\/?p=105"},"modified":"2011-08-31T20:04:10","modified_gmt":"2011-08-31T17:04:10","slug":"analyzing-mysql-slow-query-logs","status":"publish","type":"post","link":"https:\/\/meekconsulting.com\/?p=105","title":{"rendered":"Analyzing mysql slow query logs"},"content":{"rendered":"<p>The mysql servers we use are configured to log slow queries ( those that take over 5 seconds to run or queries that do not use any index).<\/p>\n<p>The output is put in \/var\/log\/mysqld.slow.log\u00a0 ( on the dbhost server! )<\/p>\n<p>I found a program today that is part of mysql that makes it easier to use this log file to pinpoint queries that might be slowing things down. \u00a0It is called <strong>mysqldumpslow<\/strong><\/p>\n<p>To run it just pass the name of the log file and it converts the variables in the queries in the log file to tokens so it can group similar queries together and tell you how often a particular query is run.\u00a0 It sorts the queries by how long they take to run, so you can focus on queries your apps are running that are taking a long time to run ( and making the user wait )<\/p>\n<p>Typically adding indexes or rewriting the query to be more efficient can greatly improve performance.<\/p>\n<p>For example I just ran it on our db1 log and immediately realized that the query we run to check a persons PKI DN certificate ( which is run for every page we display ) was doing the query with no index on pki_dn field\u00a0 \u2014 so it was sequentially scanning through the users table every time.\u00a0\u00a0 I created an index on pki_dn that should make that check faster.<\/p>\n<p>To use it:<\/p>\n<p>\u00a0<\/p>\n<blockquote><p>\/usr\/bin\/mysqldumpslow \/var\/log\/mysqld.slow.log<\/p><\/blockquote>\n<p>\u00a0<\/p>\n<p>NOTE: your mysql database must be configure to log slow queries.\u00a0 This is done with the following queries in \/etc\/my.cnf\u00a0 ( in the main [mysqld] section )<\/p>\n<blockquote><p>log-slow-queries = \/var\/log\/mysqld.slow.log<br \/>\nlog-long-format<br \/>\nlong_query_time = 5<br \/>\nlog-queries-not-using-indexes=1<\/p><\/blockquote>\n<p>\u00a0<\/p>\n<p>If the file doesn\u2019t already exist it won\u2019t create it. So be sure to make the file \/var\/log\/mysqld.slow.log and set permissions to be writable by mysql user and restart mysql like this:<\/p>\n<blockquote><p>> \/var\/log\/mysqld.slow.log<\/p>\n<p>chown mysql:mysql \/var\/log\/mysqld.slow.log<\/p>\n<p>chmod 770 \/var\/log\/mysqld.slow.log<\/p>\n<p>service mysqld restart<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>The mysql servers we use are configured to log slow queries ( those that take over 5 seconds to run or queries that do not use any index). The output is put in \/var\/log\/mysqld.slow.log\u00a0 ( on the dbhost server! ) &hellip; <a href=\"https:\/\/meekconsulting.com\/?p=105\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[3],"_links":{"self":[{"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/posts\/105"}],"collection":[{"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=105"}],"version-history":[{"count":3,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/posts\/105\/revisions"}],"predecessor-version":[{"id":107,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/posts\/105\/revisions\/107"}],"wp:attachment":[{"href":"https:\/\/meekconsulting.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}