Questions tagged [truncate]

An operation that removes all rows in a table without causing any triggered action

An operation that removes all rows in a table without causing any triggered action.

In Oracle and MySQL(depending on the storage engine) the operation is not transactional and cannot be rolled back.

In PostgreSQL and SQL Server the statement must be committed or rolled back.

In SQL Server, TRUNCATE TABLE removes all rows from a table or specified partitions of a table, without logging the individual row deletions. TRUNCATE TABLE is similar to the DELETE statement with no WHERE clause; however, TRUNCATE TABLE is typically faster and uses fewer system and transaction log resources.

The ISO SQL Standard classifies TRUNCATE TABLE as data manipulation language (DML) but DBMS software vendors commonly classify it as DDL for their own implementation-specific purposes.

References:

TRUNCATE TABLE (Transact-SQL)

ISO/IEC 9075-2 14.10

115 questions
107
votes
7 answers

Why use both TRUNCATE and DROP?

In the system I work on there are a lot of stored procedures and SQL scripts that make use of temporary tables. After using these tables it's good practice to drop them. Many of my colleagues (almost all of whom are much more experienced than I am)…
user606723
  • 1,526
  • 4
  • 14
  • 16
45
votes
1 answer

PostgreSQL - Truncate a table on cascade and reset all hierarchic sequences with 1

Is there any way to reset all the sequences of tables, when truncate a table on cascade. I already read this post How to reset sequence in postgres and fill id column with new data? ALTER SEQUENCE seq RESTART WITH 1; UPDATE t SET…
Youcef LAIDANI
  • 553
  • 1
  • 4
  • 10
26
votes
4 answers

Truncated 200GB table but disk space not released

I have only 2GB left, so I need to remove this history table. This table now is empty but the database disk space not released. And the database file is 320GB.
26
votes
5 answers

What permissions are necessary for truncating a table?

I have a SQL account with the following permissions on a database: The db_executor role you see this account being a member of was created by this script: CREATE ROLE [db_executor] AUTHORIZATION [dbo] GO GRANT EXECUTE TO [db_executor] GO When I…
Mansfield
  • 1,061
  • 8
  • 20
  • 33
25
votes
2 answers

Postgres suggests "Truncate table ... at the same time"

When running: TRUNCATE TABLE YYYYY RESTART IDENTITY I am seeing this error message: ERROR: cannot truncate a table referenced in a foreign key constraint DETAIL: Table "XXXXX" references "YYYYY". HINT: Truncate table "XXXXX" at the same time, or…
davetapley
  • 953
  • 4
  • 9
  • 16
21
votes
2 answers

Truncate a table with 17 billion rows in an AG

I need to truncate a table with 17 billion rows, the table is in a database that is part of an AG. What will be the effect of this operation on the AG latency and the size of log backups? Is there a recommended way of doing this?
20
votes
1 answer

Why does DELETE leave a lingering effect on performance?

At the end is a test script for comparing the performance between a @table variable and a #temp table. I think I've set it up correctly - the performance timings are taken outside of the DELETE/TRUNCATE commands. The results that I am getting are…
孔夫子
  • 4,330
  • 3
  • 30
  • 50
18
votes
1 answer

Safe way to truncate SQL Server Error Log

We are running out of space. What is the safe way to clear the error log?
aron
  • 595
  • 2
  • 6
  • 8
16
votes
3 answers

Why is truncate DDL?

I have an interview question, which was asked during my interview. I answered the question, but interviewer was not so convinced with my answer. So, anyone please correct me with my understanding ? Q. Why Truncate is DDL Where as Delete is DML ?…
Ravi
  • 1,565
  • 4
  • 15
  • 20
16
votes
1 answer

PostgreSQL: Disk space not released after TRUNCATE

I haveTRUNCATEd a huge (~120Gb) table called files: TRUNCATE files; VACUUM FULL files; The table size is 0, but no disk space was released. Any ideas how to reclaim my lost disk space? UPDATE: The disk space was released after ~12 hours, without…
Adam Matan
  • 12,079
  • 30
  • 82
  • 96
13
votes
1 answer

Why does truncating a temp table at the end of the stored procedure that creates it free tempdb space faster?

SQL Server caches temp tables created within stored procedures and merely renames them when the procedure ends and is subsequently executed. My question has to do with when the tempdb space is released. I've read that the table is truncated at the…
10
votes
1 answer

TRANSACTION ISOLATION LEVEL SNAPSHOT vs. TRUNCATE?

I'm hoping someone can shed some light on this behavior that I was not expecting regarding SNAPSHOT isolation vs. TRUNCATE. Database: Allow Snapshot Isolation = True; Is Read Committed Snapshot On = False. Procedure1 (Replaces content of table foo…
Mark Freeman
  • 2,293
  • 5
  • 32
  • 54
10
votes
7 answers

TRUNCATE TABLE statement sometimes hangs

Why does the TRUNCATE TABLE statement hang sometimes? What are the reasons for this type of issue? I am migrating from MySQL to MariaDB. This problem doesn't happen with MySQL, only with MariaDB. The hanging statement is simply: TRUNCATE TABLE…
Haseena
  • 571
  • 3
  • 7
  • 16
10
votes
4 answers

Truncate with where clause

Can I use truncate command with a where clause? I need to remove specific rows from several tables. How can I delete specific data from the entire database? SELECT DimEmployee.[FirstName], DimEmployee.[LastName], [SalesOrderNumber], [ShipDateKey] …
l.lijith
  • 918
  • 4
  • 9
  • 27
9
votes
2 answers

What can cause TRUNCATE TABLE to take a really long time?

I'm running MySQL5.5 with Master/Slave replication (1 master, 2 slaves). I have a process that runs once a week and truncate a specific table. The table is not large and only has a few thousands records. For some reason, the TRUNCATE TABLE command…
Ran
  • 1,573
  • 9
  • 21
  • 35
1
2 3 4 5 6 7 8