TRUNCATE
Description
TRUNCATE quickly removes all rows from a database table.
It has the same effect as an unqualified DELETE on each table, but TRUNCATE is faster because it does not actually scan the tables. This is most useful on large tables.
Precautions
- TRUNCATE TABLE has the same function as a DELETE statement with no WHERE clause, emptying a table.
- TRUNCATE TABLE uses less system and transaction log resources as compared with DELETE.
- DELETE deletes a row each time, and records the deletion of each row in the transaction log.
- TRUNCATE TABLE deletes all rows in a table by releasing the data page storing the table data, and records the releasing of the data page only in the transaction log.
- The differences between TRUNCATE, DELETE, and DROP are as follows:
- TRUNCATE TABLE deletes content and releases space, but does not delete definitions.
- DELETE TABLE deletes content, but does not delete definitions nor release space.
- DROP TABLE deletes content and definitions, and releases space.
Syntax
- Truncate the data in a table.
1 2
TRUNCATE [ TABLE ] [ ONLY ] {table_name [ * ]} [, ... ] [ CONTINUE IDENTITY ] [ CASCADE | RESTRICT ] [ PURGE ];
- Truncate the data in a partition.
1 2 3 4 5
ALTER TABLE [ IF EXISTS ] { [ ONLY ] table_name | table_name * | ONLY ( table_name ) } TRUNCATE PARTITION { partition_name | FOR ( partition_value [, ...] ) } [ UPDATE GLOBAL INDEX ];
Parameters
- ONLY
If ONLY is specified, only the specified table is cleared. If ONLY is not specified, the table and all its subtables (if any) are cleared.
- table_name
Specifies the name (optionally schema-qualified) of the target table.
Value range: an existing table name
- CONTINUE IDENTITY
Does not change the values of sequences. This is the default action.
- CASCADE | RESTRICT
- CASCADE: Clears all tables that are added to a group.
- RESTRICT (default): Refuses to truncate if any of the tables have foreign-key references from tables that are not listed in the statement (not supported in distributed scenarios).
- PURGE
Purges table data in the recycle bin by default.
- partition_name
Specifies the partition in the target partitioned table.
Value range: an existing table name
- partition_value
Specifies the value of the specified partition key.
The value specified by PARTITION FOR can uniquely identify a partition.
Value range: value range of the partition key for the partition to be renamed
When the PARTITION FOR clause is used, the entire partition where partition_value is located is cleared.
- UPDATE GLOBAL INDEX
Updates all global indexes in the partitioned table to ensure that correct data can be queried using global indexes.
If this parameter is not used, all global indexes in the partitioned table become invalid.
Examples
- Clear table data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
-- Create the reason table. gaussdb=# CREATE TABLE reason (r_reason_sk int,r_reason_id varchar(16),r_reason_desc varchar(100)); -- Insert multiple records into the table. gaussdb=# INSERT INTO reason values(1,'AAAAAAAABAAAAAAA','reason 1'), (5,'AAAAAAAABAAAAAAA','reason 2'), (15,'AAAAAAAABAAAAAAA','reason 3'), (25,'AAAAAAAABAAAAAAA','reason 4'), (35,'AAAAAAAABAAAAAAA','reason 5'), (45,'AAAAAAAACAAAAAAA','reason 6'); -- Check the table information. The table size is about 16 KB. gaussdb=# \d+ List of relations Schema | Name | Type | Owner | Size | Storage | Description --------+--------+-------+-------+-------+----------------------------------+------------- public | reason | table | omm | 16 kB | {orientation=row,compression=no} | (1 row) -- Run the DELETE statement without the WHERE condition to clear data in the table and check the table size. gaussdb=# DELETE FROM reason; gaussdb=# \d+ List of relations Schema | Name | Type | Owner | Size | Storage | Description --------+--------+-------+-------+-------+----------------------------------+------------- public | reason | table | omm | 16 kB | {orientation=row,compression=no} | (1 row) -- Run the TRUNCATE statement to clear the reason table and check the table size. gaussdb=# TRUNCATE TABLE reason; gaussdb=# \d+ List of relations Schema | Name | Type | Owner | Size | Storage | Description --------+--------+-------+-------+---------+----------------------------------+------------- public | reason | table | omm | 0 bytes | {orientation=row,compression=no} | (1 row) -- Drop the table. gaussdb=# DROP TABLE reason;
- Clear data of a partitioned table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
-- Create a partitioned table. gaussdb=# CREATE TABLE reason_p( r_reason_sk integer, r_reason_id character(16), r_reason_desc character(100) )PARTITION BY RANGE (r_reason_sk)( partition p_05_before values less than (05), partition p_15 values less than (15), partition p_25 values less than (25), partition p_35 values less than (35), partition p_45_after values less than (MAXVALUE) ); -- Insert data. gaussdb=# INSERT INTO reason_p values(1,'AAAAAAAABAAAAAAA','reason 1'), (5,'AAAAAAAABAAAAAAA','reason 2'), (15,'AAAAAAAABAAAAAAA','reason 3'), (25,'AAAAAAAABAAAAAAA','reason 4'), (35,'AAAAAAAABAAAAAAA','reason 5'), (45,'AAAAAAAACAAAAAAA','reason 6'); -- Clear the p_05_before partition. gaussdb=# ALTER TABLE reason_p TRUNCATE PARTITION p_05_before UPDATE GLOBAL INDEX; -- Clear the p_15 partition. gaussdb=# ALTER TABLE reason_p TRUNCATE PARTITION for (13) UPDATE GLOBAL INDEX; -- Clear the partitioned table. gaussdb=# TRUNCATE TABLE reason_p; -- Delete the reason_p table. gaussdb=# DROP TABLE reason_p;
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot