Comparison of deleted data DELETE statements and TRUNCATE statements for Oracle database

Source: Internet
Author: User

Comparison of deleted data DELETE statements and TRUNCATE statements for Oracle database

When the data in the table is not needed, you should delete the data and free up the occupied space, and delete the data in the table using the DELETE statement or the TRUNCATE statement, as described below.

One, DELETE statement

(1) Conditional deletion

syntax format:delete [from] table_name [where condition];

For example: Delete data from the users table with the UserID ' 001 ': Delete from users where userid= ' 001 ';

(2) Unconditionally delete entire table data

syntax format:DELETE table_name;

For example: Delete all data from the user table: delete users;

Second, truncate statement

Using the TRUNCATE statement is to delete all records in the table.

syntax Format: Truncate [TABLE] table_name;

(1) Delete all records do not keep record space

Truncate [TABLE] table_name [drop storage];

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

(2) Delete all records to keep the record occupied space

Truncate [TABLE] table_name [reuse storage];

For example: Delete all data from the users table and save space: Truncate table users reuse storage;

Comparison of two DELETE statements

Since the DELETE statement deletes the record, the record is deleted one at a time, and the TRUNCATE statement does not produce fallback information when it deletes the data, so if you need to delete large amounts of data, using delete consumes more system resources, and if you use truncate, it is much faster.

The following examples illustrate:

1, first establish the user table:

CREATE TABLE Users (UserID   varchar2, username varchar2 (+), Userpass varchar2 (30));

Copy Code

2. Then insert a piece of data

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

3. Insert tens of thousands of data using the Copy Insert method

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

I've inserted 4,194,304 data, using delete deletion takes time: 90.964 seconds, and then inserting twice times more data, but using truncate takes only 2.215 seconds, as shown in:

Comparison of deleted data DELETE statements and TRUNCATE statements for Oracle database

Related Article

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.