How Do I Delete Table Data Efficiently?
Yes. TRUNCATE is more efficient than DELETE for deleting massive data.
For details, see TRUNCATE.
Function
TRUNCATE quickly removes all rows from a table. It has the same effect as an unqualified DELETE but since it does not actually scan the table it is faster. This is most effective on large tables.
Functions
- TRUNCATE TABLE works like a DELETE statement with no WHERE clause, that is, emptying a table.
- TRUNCATE TABLE uses less system and transaction log resources.
- DELETE deletes a row each time, and records each deletion in the transaction log.
- TRUNCATE TABLE deletes all rows in a table by releasing the data page, and only records each releasing of the data page in the transaction log.
- TRUNCATE, DELETE, and DROP are different in that:
- TRUNCATE TABLE deletes content, releases space, but does not delete definitions.
- DELETE TABLE deletes content, but does not delete definitions or release space.
- DROP TABLE deletes content and definitions, and releases space.
Examples
- Create a table.
1
CREATE TABLE tpcds.reason_t1 AS TABLE tpcds.reason;
Truncate the table.
1
TRUNCATE TABLE tpcds.reason_t1;
Delete the table.
1
DROP TABLE tpcds.reason_t1;
- Create a partitioned table.
1 2 3 4 5 6 7 8 9 10 11 12 13
CREATE TABLE tpcds.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.
1
INSERT INTO tpcds.reason_p SELECT * FROM tpcds.reason;
Truncate the p_05_before partition.
1
ALTER TABLE tpcds.reason_p TRUNCATE PARTITION p_05_before;
Truncate the partition p_15 where 13 is located.
1
ALTER TABLE tpcds.reason_p TRUNCATE PARTITION for (13);
Truncate the partitioned table.
1
TRUNCATE TABLE tpcds.reason_p;
Delete the table.
1
DROP TABLE tpcds.reason_p;
Database Usage FAQs
- How Do I Change Distribution Columns?
- How Do I View and Set the Database Character Encoding?
- What Do I Do If Date Type Is Automatically Converted to the Timestamp Type During Table Creation?
- Do I Need to Run VACUUM FULL and ANALYZE on Common Tables Periodically?
- Do I Need to Set a Distribution Key After Setting a Primary Key?
- Is GaussDB(DWS) Compatible with PostgreSQL Stored Procedures?
- What Are Partitioned Tables, Partitions, and Partition Keys?
- How Can I Export the Table Structure?
- How Do I Delete Table Data Efficiently?
- How Do I View Foreign Table Information?
- If No Distribution Column Is Specified, How Will Data Be Stored?
- How Do I Replace the Null Result with 0?
- How Do I Check Whether a Table Is Row-Stored or Column-Stored?
- How Do I Query the Information About GaussDB(DWS) Column-Store Tables?
- Why Sometimes the GaussDB(DWS) Query Indexes Become Invalid?
- How Do I Use a User-Defined Function to Rewrite the CRC32() Function?
- What Are the Schemas Starting with pg_toast_temp* or pg_temp*?
- Solutions to Inconsistent GaussDB(DWS) Query Results
- Which System Catalogs That the VACUUM FULL Operation Cannot Be Performed on?
- In Which Scenarios Would a Statement Be "idle in transaction"?
- How Does GaussDB(DWS) Implement Row-to-Column and Column-to-Row Conversion?
- What Are the Differences Between Unique Constraints and Unique Indexes?
- What Are the Differences Between Functions and Stored Procedures?
- How Do I Delete Duplicate Table Data?
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.
Chatbotmore