Comparison between Delete statements and truncate statements for Oracle database data deletion

Source: Internet
Author: User

When the data in the table is not required, delete the data and release the occupied space. To delete the data in the table, use the delete statement or the truncate statement.

I. Delete statements

(1) conditional Deletion

Syntax format:Delete [from] table_name [Where condition];

For example, delete data whose userid is '001' in the users table: delete from users where userid = '001 ';

(2) unconditionally Delete the entire table data

Syntax format:Delete table_name;

For example, delete all data in the User table: delete users;

Ii. truncate statement

The truncate statement is used to delete all records in the table.

Syntax format:Truncate [Table] table_name;

(1) DeleteSpace occupied by records not retained

Truncate [Table] table_name [drop Storage];

For example, deleting all data in the users table does not save space: truncate table users drop storage; because the drop storage keyword is used by default, the drop storage can be omitted;

(2) DeleteSpace occupied by reserved records

Truncate [Table] table_name [reuse Storage];

For example, delete all data in the users table and save the occupied space: truncate table users reuse storage;

Iii. Comparison of the two deletion statements

The delete statement deletes records one by one, while the truncate statement does not generate rollback information when deleting data; therefore, if you need to delete a large amount of data, using Delete will occupy a large amount of system resources, while using truncate will be much faster.

The following is an example:

1. Create a user table first:

 
Create Table users (userid varchar2 (20), username varchar2 (30), userpass varchar2 (30 ));

CopyCode

2. Insert a data record.

Insert into users values ('001', 'gavindream', '20180101 ');

3. Insert tens of thousands of data records using the copy and insert method.

 
Insert into users (userid, username, userpass) Select * from users;

I have inserted 4194304 pieces of data. It takes 90.964 seconds to delete the data using delete, and then twice as long as the data is inserted. However, it takes only 2.215 seconds to use truncate, as shown in:

From online: http://www.cr173.com/html/15011_1.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.