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) deleting all records does not reserve the space occupied by the records

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) Delete space occupied by reserved records of all 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)
); Copy the code

2. Insert a data record.

Insert into users values ('001', 'gavindream', '000000'); 3. insert tens of thousands of data records using the copy insertion method.

Insert into users (userid, username, userpass) select * from users; I have inserted 4194304 pieces of data. It takes 90.964 seconds to delete data using delete, the data is inserted twice, but the time spent using truncate is only 2.215 seconds, as shown in:



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.