<p>I had to delete all the rows from a log table that contained about 5 million rows. My initial try was to issue the following command in query analyzer:</p>

<p>delete from client_log</p>

<p>which took a very long time.</p>

[{'answer_id': 64126, 'author': 'Rob Walker', 'author_id': 3631, 'author_profile': 'https://Stackoverflow.com/users/3631', 'pm_score': 7, 'selected': True, 'text': '<p>Check out <a href="https://msdn.microsoft.com/en-us/library/ms177570.aspx" rel="noreferrer">truncate table</a> which is a lot faster.</p>\n'}, {'answer_id': 64128, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 0, 'selected': False, 'text': '<p>Yes, well, deleting 5 million rows is probably going to take a long time. The only potentially faster way I can think of would be to drop the table, and re-create it. That only works, of course, if you want to delete ALL data in the table.</p>\n'}, {'answer_id': 64132, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p><I>truncate table client_log</I></p>\n\n<p>is your best bet, truncate kills all content in the table and indices and resets any seeds you've got too.</p>\n"}, {'answer_id': 64133, 'author': 'Martynnw', 'author_id': 5466, 'author_profile': 'https://Stackoverflow.com/users/5466', 'pm_score': 1, 'selected': False, 'text': "<p>On SQL Server you can use the <code>Truncate Table</code> command which is faster than a regular delete and also uses less resources. It will reset any identity fields back to the seed value as well.</p>\n\n<p>The drawbacks of truncate are that it can't be used on tables that are referenced by foreign keys and it won't fire any triggers. Also you won't be able to rollback the data if anything goes wrong.</p>\n"}, {'answer_id': 64135, 'author': 'Ron Skufca', 'author_id': 4096, 'author_profile': 'https://Stackoverflow.com/users/4096', 'pm_score': 5, 'selected': False, 'text': "<p>I discovered the <strong>TRUNCATE TABLE</strong> in the msdn transact-SQL reference. For all interested here are the remarks:</p>\n\n<p>TRUNCATE TABLE is functionally identical to DELETE statement with no WHERE clause: both remove all rows in the table. But TRUNCATE TABLE is faster and uses fewer system and transaction log resources than DELETE.</p>\n\n<p>The DELETE statement removes rows one at a time and records an entry in the transaction log for each deleted row. TRUNCATE TABLE removes the data by deallocating the data pages used to store the table's data, and only the page deallocations are recorded in the transaction log.</p>\n\n<p>TRUNCATE TABLE removes all rows from a table, but the table structure and its columns, constraints, indexes and so on remain. The counter used by an identity for new rows is reset to the seed for the column. If you want to retain the identity counter, use DELETE instead. If you want to remove table definition and its data, use the DROP TABLE statement.</p>\n\n<p>You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint; instead, use DELETE statement without a WHERE clause. Because TRUNCATE TABLE is not logged, it cannot activate a trigger.</p>\n\n<p>TRUNCATE TABLE may not be used on tables participating in an indexed view.</p>\n"}, {'answer_id': 64153, 'author': 'UnkwnTech', 'author_id': 115, 'author_profile': 'https://Stackoverflow.com/users/115', 'pm_score': 3, 'selected': False, 'text': '<p>For reference <a href="https://dev.mysql.com/doc/refman/8.0/en/truncate-table.html" rel="nofollow noreferrer">TRUNCATE TABLE</a> also works on MySQL</p>\n'}, {'answer_id': 64156, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 1, 'selected': False, 'text': '<p><code>truncate table</code> is <strong>not</strong> SQL-platform independent.  If you suspect that you <strong>might</strong> ever change database providers, you might be wary of using it.</p>\n'}, {'answer_id': 64263, 'author': 'Andy Lester', 'author_id': 8454, 'author_profile': 'https://Stackoverflow.com/users/8454', 'pm_score': 0, 'selected': False, 'text': '<p>The suggestion of "Drop and recreate the table" is probably not a good one because that goofs up your foreign keys.</p>\n\n<p>You ARE using foreign keys, right?</p>\n'}, {'answer_id': 64435, 'author': 'Brian D.', 'author_id': 8618, 'author_profile': 'https://Stackoverflow.com/users/8618', 'pm_score': 1, 'selected': False, 'text': '<p>Note that TRUNCATE will also reset any auto incrementing keys, if you are using those.</p>\n\n<p>If you do not wish to lose your auto incrementing keys, you can speed up the delete by deleting in sets (e.g., DELETE FROM table WHERE id > 1 AND id &lt; 10000). It will speed it up significantly and in some cases prevent data from being locked up.</p>\n'}, {'answer_id': 64909, 'author': 'Andy Frieders', 'author_id': 8785, 'author_profile': 'https://Stackoverflow.com/users/8785', 'pm_score': 0, 'selected': False, 'text': '<p>I am revising my earlier statement:</p>\n\n<blockquote>\n  <p>You should understand that by using\n  TRUNCATE the data will be cleared but\n  nothing will be logged to the\n  transaction log.  Writing to the log\n  is why DELETE will take forever on 5\n  million rows.  I use TRUNCATE often\n  during development, but you should be\n  wary about using it on a production\n  database because you will not be able\n  to roll back your changes.  You should\n  immediately make a full database\n  backup after doing a TRUNCATE to\n  establish a new basis for restoration.</p>\n</blockquote>\n\n<p>The above statement was intended to prompt you to be sure that you understand there is difference between the two.  Unfortunately, it is poorly written and makes unsupported statements as I have not actually done any testing myself between the two.  It is based on statements that I have heard from others.</p>\n\n<p>From <a href="http://msdn.microsoft.com/en-us/library/aa260621.aspx" rel="nofollow noreferrer">MSDN</a>:</p>\n\n<blockquote>\n  <p>The DELETE statement removes rows one\n  at a time and records an entry in the\n  transaction log for each deleted row.\n  TRUNCATE TABLE removes the data by\n  deallocating the data pages used to\n  store the table\'s data, and only the\n  page deallocations are recorded in the\n  transaction log.</p>\n</blockquote>\n\n<p>I just wanted to say that there is a fundamental difference between the two and because there is a difference, there will be applications where one or the other may be inappropriate.</p>\n'}, {'answer_id': 65215, 'author': 'squadette', 'author_id': 7754, 'author_profile': 'https://Stackoverflow.com/users/7754', 'pm_score': 4, 'selected': False, 'text': "<p>There is a common myth that TRUNCATE somehow skips transaction log.</p>\n\n<p>This is misunderstanding, and is clearly mentioned in MSDN.  </p>\n\n<p>This myth is invoked in several comments here.  Let's eradicate it together ;)</p>\n"}, {'answer_id': 65227, 'author': 'squadette', 'author_id': 7754, 'author_profile': 'https://Stackoverflow.com/users/7754', 'pm_score': 0, 'selected': False, 'text': '<p>If you cannot use TRUNCATE TABLE because of foreign keys and/or triggers, you can consider   to:</p>\n\n<ul>\n<li>drop all indexes;</li>\n<li>do the usual DELETE;</li>\n<li>re-create all indexes.</li>\n</ul>\n\n<p>This may speed up DELETE somewhat.</p>\n'}, {'answer_id': 65675, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>forget truncate and delete. maintain your table definitions (in case you want to recreate it) and just use drop table.</p>\n'}, {'answer_id': 65705, 'author': 'dar7yl', 'author_id': 9505, 'author_profile': 'https://Stackoverflow.com/users/9505', 'pm_score': 3, 'selected': False, 'text': '<p>I use the following method to zero out tables, with the added bonus that it leaves me with an archive copy of the table.</p>\n\n<pre><code>CREATE TABLE `new_table` LIKE `table`;\nRENAME TABLE `table` TO `old_table`, `new_table` TO `table`;\n</code></pre>\n'}, {'answer_id': 108984, 'author': 'Mark Stock', 'author_id': 19737, 'author_profile': 'https://Stackoverflow.com/users/19737', 'pm_score': -1, 'selected': False, 'text': "<pre><code>DELETE * FROM table_name;\n</code></pre>\n\n<p>Premature optimization may be dangerous.  Optimizing may mean doing something weird, but if it works you may want to take advantage of it.</p>\n\n<pre><code>SELECT DbVendor_SuperFastDeleteAllFunction(tablename, BOZO_BIT) FROM dummy;\n</code></pre>\n\n<p>For speed I think it depends on...</p>\n\n<ul>\n<li><p>The underlying database:   Oracle, Microsoft, MySQL, PostgreSQL, others, custom...</p></li>\n<li><p>The table, it's content, and related tables:</p></li>\n</ul>\n\n<p>There may be deletion rules.  Is there an existing procedure to delete all content in the table?  Can this be optimized for the specific underlying database engine?  How much do we care about breaking things / related data?  Performing a DELETE may be the 'safest' way assuming that other related tables do not depend on this table.  Are there other tables and queries that are related / depend on the data within this table?  If we don't care much about this table being around, using DROP might be a fast method, again depending on the underlying database.</p>\n\n<pre><code>DROP TABLE table_name;\n</code></pre>\n\n<p>How many rows are being deleted?  Is there other information that is quickly gleaned that will optimize the deletion?  For example, can we tell if the table is already empty?  Can we tell if there are hundreds, thousands, millions, billions of rows?</p>\n"}]
['https://Stackoverflow.com/questions/64117', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4096/']