Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all rows in table

Normaly i would do a delete * from XXX but on this table thats very slow, it normaly has about 500k to 1m rows in it ( one is a varbinary(MAX) if that mathers ).

Basicly im wondering if there is a quick way to emty the table of all content, its actualy quicker to drop and recreate it then to delete the content via the delete sql statement

The reason i dont want to recreate the table is because its heavly used and delete/recreate i assume will destroy indexs and stats gathered by sql server

Im also hoping there is a way to do this because there is a "clever" way to get row count via sys.sysindexes , so im hoping there is a equaly clever way to delete content

like image 801
EKS Avatar asked Jul 10 '09 23:07

EKS


People also ask

How do you delete rows in a table?

Note: In Excel, select a row or column that you want to delete, right-click and select Delete , and choose the option you want. Or select a cell, and then select Home > Insert or Home > Delete, and then choose an option.

Which command is used to delete all rows from a table?

The truncate command removes all rows of a table.

What is the clause to delete all rows from the table *?

The DELETE statement specifies a table and usually contains a WHERE clause that designates the row or rows that are to be removed from the table. If the WHERE clause is left out, all rows are deleted.


1 Answers

Truncate table is faster than delete * from XXX. Delete is slow because it works one row at a time. There are a few situations where truncate doesn't work, which you can read about on MSDN.

like image 78
Scott Avatar answered Oct 07 '22 10:10

Scott