
#MYSQL TRUNCATE TABLE HOW TO#
Now you should know how to use the TRUNCATE TABLE statement to delete all data from a big table fast and understand the differences between the TRUNCATE TABLE and DELETE statements.The TABLE keyword is optional.
#MYSQL TRUNCATE TABLE CODE#
Third, to remove all data from the big_table, you use the following statement: TRUNCATE TABLE big_table Code language: SQL (Structured Query Language) ( sql )Īs you can see, how fast the TRUNCATE TABLE statement is. CALL load_big_table_data( 10000) Code language: SQL (Structured Query Language) ( sql ) The following statement calls the load_big_table_data stored procedure to insert 10,000 rows into the big_table table. WHILE counter < num DO INSERT INTO big_table(val)ĮND$$ Code language: SQL (Structured Query Language) ( sql ) DELIMITER $$ĬREATE PROCEDURE load_big_table_data( IN num int) For example, the following stored procedure in MySQL loads data into the big_table table with the number of rows specified by the num parameter. Note that if you use a database system that supports stored procedure, you can put this statement inside a loop. ( RAND( 100000)) Code language: SQL (Structured Query Language) ( sql ) Second, execute the following statement as many times as you want to insert sample data into the big_table table: INSERT INTO big_table (val) ) Code language: SQL (Structured Query Language) ( sql ) Let’s take a look at an example of truncating a table.įirst, create a new table named big_table as follows: CREATE TABLE big_table (

Logically the TRUNCATE TABLE statement and the DELETE statement without the WHERE clause gives the same effect that removes all data from a table.

If you are using the one that does not, you must issue multiple TRUNCATE TABLE statements to truncate multiple tables. Not all database systems support this form of the TRUNCATE TABLE statement. Code language: SQL (Structured Query Language) ( sql ) To truncate multiple tables at a time, you can use a list of comma-separated table names after the TRUNCATE TABLE clause as follows: TRUNCATE TABLE table_name1, table_name2. By doing this, the RDBMS can reduce the resources for logging and the number of locks that need to acquire. When you issue the TRUNCATE TABLE statement, the database system deletes all rows from the table by deallocating the data pages allocated by the table. Some database systems such as MySQL and PostgreSQL allow you to skip the TABLE keyword so the TRUNCATE TABLE statement is as simple as follows: TRUNCATE table_name Code language: SQL (Structured Query Language) ( sql )

In this syntax, you specify the table_name that you want to delete data after the TRUNCATE TABLE clause. To delete all rows from a big table fast, you use the following TRUNCATE TABLE statement: TRUNCATE TABLE table_name Code language: SQL (Structured Query Language) ( sql )

For a big table that has few million rows, the DELETE statement is slow and not efficient. To delete all data from a table, you use the DELETE statement without a WHERE clause.
