{"id":478,"date":"2014-07-31T02:59:52","date_gmt":"2014-07-31T02:59:52","guid":{"rendered":"https:\/\/qbytes.cloud\/?p=478"},"modified":"2014-07-31T02:59:52","modified_gmt":"2014-07-31T02:59:52","slug":"optimize-only-fragmented-tables-in-mysql","status":"publish","type":"post","link":"https:\/\/www.qbytes.cloud\/index.php\/2014\/07\/31\/optimize-only-fragmented-tables-in-mysql\/","title":{"rendered":"Optimize only fragmented tables in MySQL"},"content":{"rendered":"<p>When you are using MySQL, you will (likely) have tables that can be fragmented. In MySQL terms this is called &#8220;OPTIMIZE&#8221;.<\/p>\n<p>You could simply OPTIMIZE every table in every database, but during an OPTIMIZE, the tables are locked, so writing is not possible.<\/p>\n<p>To minimize the time that MySQL will be locked (and results cannot be written), here is a script that checks fragmentation of every table of every database. Only if a table is fragmented, the table is OPTIMIZED.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n#!\/bin\/sh\n\necho -n &quot;MySQL username: &quot; ; read username\necho -n &quot;MySQL password: &quot; ; stty -echo ; read password ; stty echo ; echo\n\nmysql -u $username -p&quot;$password&quot; -NBe &quot;SHOW DATABASES;&quot; | grep -v &#039;lost+found&#039; | while read database ; do\nmysql -u $username -p&quot;$password&quot; -NBe &quot;SHOW TABLE STATUS;&quot; $database | while read name engine version rowformat rows avgrowlength datalength maxdatalength indexlength datafree autoincrement createtime updatetime checktime collation checksum createoptions comment ; do\n  if &#x5B; &quot;$datafree&quot; -gt 0 ] ; then\n   fragmentation=$(($datafree * 100 \/ $datalength))\n   echo &quot;$database.$name is $fragmentation% fragmented.&quot;\n   mysql -u &quot;$username&quot; -p&quot;$password&quot; -NBe &quot;OPTIMIZE TABLE $name;&quot; &quot;$database&quot;\n  fi\ndone\ndone\n<\/pre>\n<p>Another one:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n#!\/bin\/sh\n\n# --- Variabls\nMYSQLROOTUSR=&#039;root&#039;\nMYSQLROOTPW=&#039;password&#039;\nPERCENT=&#039;55&#039;\nROWCOUNT=&#039;300&#039;\n\nmysql -u $MYSQLROOTUSR -p&quot;$MYSQLROOTPW&quot; -NBe &quot;SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_ROWS, DATA_LENGTH, INDEX_LENGTH, DATA_FREE, (DATA_FREE*100\/DATA_LENGTH) as PRC FROM INFORMATION_SCHEMA.TABLES WHERE (DATA_FREE*100\/DATA_LENGTH) &gt;= $PERCENT AND TABLE_ROWS &gt;= $ROWCOUNT ORDER BY TABLE_SCHEMA, PRC DESC;&quot; | while read TABLE_SCHEMA TABLE_NAME TABLE_ROWS DATA_LENGTH INDEX_LENGTH DATA_FREE PRC; do\n\n        echo &quot;$TABLE_SCHEMA.$TABLE_NAME is $PRC% fragmented and has $TABLE_ROWS rows.&quot;\n        mysql -u &quot;$MYSQLROOTUSR&quot; -p&quot;$MYSQLROOTPW&quot; -NBe &quot;OPTIMIZE TABLE $TABLE_NAME;&quot; &quot;$TABLE_SCHEMA&quot;\ndone\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When you are using MySQL, you will (likely) have tables that can be fragmented. In MySQL terms this is called &#8220;OPTIMIZE&#8221;. You could simply OPTIMIZE every table in every database, but during an OPTIMIZE, the tables are locked, so writing is not possible. To minimize the time that MySQL will be locked (and results cannot &#8230; <a title=\"Optimize only fragmented tables in MySQL\" class=\"read-more\" href=\"https:\/\/www.qbytes.cloud\/index.php\/2014\/07\/31\/optimize-only-fragmented-tables-in-mysql\/\" aria-label=\"Read more about Optimize only fragmented tables in MySQL\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,70],"tags":[],"class_list":["post-478","post","type-post","status-publish","format-standard","hentry","category-administration","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/posts\/478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/comments?post=478"}],"version-history":[{"count":0,"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/posts\/478\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/media?parent=478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/categories?post=478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/tags?post=478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}