{"id":1716,"date":"2026-06-05T10:14:34","date_gmt":"2026-06-05T08:14:34","guid":{"rendered":"https:\/\/www.mcgill.org.za\/stuff\/?p=1716"},"modified":"2026-06-06T20:41:16","modified_gmt":"2026-06-06T18:41:16","slug":"run-a-mysql-maintenance-command-until-no-rows-are-changed","status":"publish","type":"post","link":"https:\/\/www.mcgill.org.za\/stuff\/archives\/1716","title":{"rendered":"run a mysql maintenance command until no rows are changed"},"content":{"rendered":"\n<p>TL;DR:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET @quit=IF(ROW_COUNT()=0,'QUIT','SELECT 0 quit'); PREPARE quit FROM @quit; EXECUTE quit;<\/code><\/pre>\n\n\n\n<p>Detail: Sometimes you want to delete records older than 45 days from a table:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DELETE FROM livetable WHERE acctstarttime &lt; now()-interval 45 day;<\/code><\/pre>\n\n\n\n<p>But you don&#8217;t want to make a huge long-running transaction of it, so you do it in chunks of <code>LIMIT 1000<\/code>:<\/p>\n\n\n\n<pre id=\"block-eba9f210-df50-4b7d-95d6-da5cf0f77203\" class=\"wp-block-code\"><code>DELETE FROM livetable WHERE acctstarttime &lt; now()-interval 45 day LIMIT 1000;<\/code><\/pre>\n\n\n\n<p>But you want to do more than 1 chunk, so you make a repetitive script:<\/p>\n\n\n\n<pre id=\"block-eba9f210-df50-4b7d-95d6-da5cf0f77203\" class=\"wp-block-code\"><code>DELETE FROM livetable WHERE acctstarttime &lt; now()-interval 45 day;\nDELETE FROM livetable WHERE acctstarttime &lt; now()-interval 45 day;\nDELETE FROM livetable WHERE acctstarttime &lt; now()-interval 45 day;\nDELETE FROM livetable WHERE acctstarttime &lt; now()-interval 45 day;\nDELETE FROM livetable WHERE acctstarttime &lt; now()-interval 45 day;<\/code><\/pre>\n\n\n\n<p>But 5 times is likely wrong: we need to <strong>quit<\/strong> when the number of rows affected is <strong>zero<\/strong>, ie. <code>ROW_COUNT()=0<\/code> : but MySQL doesn&#8217;t have conditional execution in the comand line interface.<\/p>\n\n\n\n<p>We can fudge it: here&#8217;s the recipe, to run from the shell:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yes \"\nDELETE FROM livetable WHERE acctstarttime &lt; now()-interval 45 day LIMIT 1000;\nSET @quit=IF(ROW_COUNT()=0,'QUIT','SELECT 0 quit'); PREPARE quit FROM @quit; EXECUTE quit;\n\" | mysql livedatabase<\/code><\/pre>\n\n\n\n<p>The <code>yes<\/code> command prints out the string over and over, and the line that says <code>EXECUTE<\/code> runs a string which makes an error (<code>QUIT<\/code> is not a mysql command), and the error causes <code>mysql<\/code> to quit.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>TL;DR: Detail: Sometimes you want to delete records older than 45 days from a table: But you don&#8217;t want to make a huge long-running transaction of it, so you do it in chunks of LIMIT 1000: But you want to &hellip; <a href=\"https:\/\/www.mcgill.org.za\/stuff\/archives\/1716\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":null,"sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[20,190],"class_list":["post-1716","post","type-post","status-publish","format-standard","hentry","category-stuff","tag-mysql","tag-stuff"],"_links":{"self":[{"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/posts\/1716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/comments?post=1716"}],"version-history":[{"count":4,"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/posts\/1716\/revisions"}],"predecessor-version":[{"id":1720,"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/posts\/1716\/revisions\/1720"}],"wp:attachment":[{"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/media?parent=1716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/categories?post=1716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/tags?post=1716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}