{"id":2500,"date":"2015-07-28T12:14:36","date_gmt":"2015-07-28T12:14:36","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2500"},"modified":"2015-07-28T12:14:36","modified_gmt":"2015-07-28T12:14:36","slug":"rethinkdb-truncate-table","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/rethinkdb-truncate-table\/","title":{"rendered":"RethinkDB: Truncate table"},"content":{"rendered":"<p>Like most relational databases, RethinkDB has several different commands to delete records from a database.<\/p>\n<p>To test the behavior, I made several copies of an existing table I had, which took ~1 minute per:<\/p>\n<pre lang=\"Javascript\">\nr.db('performance')\n .tableCreate('query_timings2')\n\nr.db('performance')\n .table('query_timings2')\n .insert(r.db('performance')\n .table('query_timings'))\n<\/pre>\n<p>Currently, the SQL to ReQL comparision guide lists this as the equivalent of &#8220;truncate&#8221;:<\/p>\n<pre lang=\"Javascript\">\nr.db('performance')\n .table('query_timings')\n .delete()\n<\/pre>\n<p>In my database, this took about the same amount of time as creating the original table. I believe this runs in a transaction, as when I abort the query the number of rows remains the same.<\/p>\n<p>In many relational databases, &#8220;truncate&#8221; has a specific meaning; in Oracle it deletes the underlying filesystem from disk immediately and outside a transaction. In Postgres it runs within a transaction but skips any effort required to verify rows, which appears to match what an unqualified delete does in RethinkDB.<\/p>\n<p>If you would like to replicate the Oracle behavior, dropping and re-creating a table seems to be much faster (I only know this experimentally, so I don&#8217;t know if this runs outside a transaction)<\/p>\n<pre lang=\"Javascript\">\nr.db('performance')\n .tableDrop('query_timings') -- 45 ms\n\nr.db('performance')\n .tableCreate('query_timings')\n<\/pre>\n<p>RethinkDB has two other neat feature that I&#8217;ve not seen in other databases; the option to do a &#8220;hard&#8221; vs &#8220;soft&#8221; delete, and whether to return the deleted rows:<\/p>\n<pre lang=\"Javascript\">\nr.db('performance')\n .table('query_timings2')\n .delete({durability=\"soft\", returnChanges=false})\n<\/pre>\n<p>In my test, the &#8220;soft&#8221; delete took about half the time of the &#8220;hard&#8221; delete (basically it is not waiting for the OS to confirm changes were written to disk).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Like most relational databases, RethinkDB has several different commands to delete records from a database. To test the behavior, I made several copies of an existing table I had, which took ~1 minute per: r.db(&#8216;performance&#8217;) .tableCreate(&#8216;query_timings2&#8217;) r.db(&#8216;performance&#8217;) .table(&#8216;query_timings2&#8217;) .insert(r.db(&#8216;performance&#8217;) .table(&#8216;query_timings&#8217;)) Currently, the SQL to ReQL comparision guide lists this as the equivalent of &#8220;truncate&#8221;: r.db(&#8216;performance&#8217;) &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/rethinkdb-truncate-table\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;RethinkDB: Truncate table&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"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":[4],"tags":[160,462,466,523],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2500"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=2500"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2500\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}