Oracle Database Delete Data DELETE statement and TRUNCATE statement use comparison _oracle

Source: Internet
Author: User
Tags comparison
When the data in the table is not needed, you should delete the data and release the space occupied, and the data in the table can be deleted using the DELETE statement or the TRUNCATE statement, described separately below.

A, DELETE statement

(1) Conditional deletion

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

For example: Delete data from the users Table of UserID ' 001 ': 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;

Second, the TRUNCATE statement

Use the TRUNCATE statement to delete all records in a table.

Syntax format: Truncate [table] table_name;

(1) Delete all records do not keep the record occupied space

Truncate [TABLE] table_name [drop storage];

For example, deleting all data in 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 records occupied space

Truncate [TABLE] table_name [reuse storage];

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

comparison of two kinds of deletion statements

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

The following examples illustrate:

1, first set up user table:

CREATE TABLE Users
(
UserID VARCHAR2 (20),
Username VARCHAR2 (30),
Userpass VARCHAR2 (30)
); Copy Code

2, then insert a piece of data

Insert into users values (' 001 ', ' gavindream ', ' 123456 '); 3. Inserting tens of thousands of data using the Copy insertion method

Insert into the Users (userid,username,userpass) select * from users; I inserted 4,194,304 data, using delete Delete to take time: 90.964 seconds, The data is then inserted twice times, but using truncate takes time only for 2.215 seconds, as shown in the following illustration:



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.