Re: TRUNCATE TABLE

From: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>
To: "Alvaro Herrera" <alvherre(at)commandprompt(dot)com>
Cc: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "Adriaan van Os" <postgres(at)microbizz(dot)nl>, pgsql-performance(at)postgresql(dot)org
Subject: Re: TRUNCATE TABLE
Date: 2007-07-13 19:12:34
Message-ID: 162867790707131212g17cbc5a9q5d098dbf01c8e502@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Hello,

I tested speed difference between TRUNCATE TABLE and DROP TABLE
(tested on my notebook ext3 and Linux fedora 7):

CREATE OR REPLACE FUNCTION test01() RETURNS SETOF double precision
AS $$
DECLARE t1 timestamp with time zone;
BEGIN
CREATE TEMP TABLE foo(a integer);
FOR i IN 1..1000 LOOP
INSERT INTO foo SELECT 1 FROM generate_series(1,10000);
t1 := clock_timestamp();
TRUNCATE TABLE foo;
RETURN NEXT EXTRACT('ms' FROM clock_timestamp()-t1);
END LOOP;
RETURN;
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION test02() RETURNS SETOF double precision
AS $$
DECLARE t1 timestamp with time zone;
BEGIN
FOR i IN 1..1000 LOOP
EXECUTE 'CREATE TEMP TABLE foo(a integer);';
EXECUTE 'INSERT INTO foo SELECT 1 FROM generate_series(1,10000);';
t1 := clock_timestamp();
EXECUTE 'DROP TABLE foo;';
RETURN NEXT EXTRACT('ms' FROM clock_timestamp()-t1);
END LOOP;
RETURN;
END;
$$ LANGUAGE plpgsql;

vacuum pg_class; vacuum pg_type; vacuum pg_attribute;

postgres=# select count(*), min(t), max(t), avg(t), stddev_samp(t),
stddev_pop(t) from test01() t(t);
count | min | max | avg | stddev_samp | stddev_pop
-------+-------+---------+----------+------------------+------------------
1000 | 0.295 | 803.971 | 3.032483 | 30.0036729610037 | 29.9886673721876
(1 row)

Time: 33826,841 ms
postgres=# select count(*), min(t), max(t), avg(t), stddev_samp(t),
stddev_pop(t) from test02() t(t);
count | min | max | avg | stddev_samp | stddev_pop
-------+-------+--------+----------+------------------+-------------------
1000 | 0.418 | 20.792 | 0.619168 | 0.81550718804297 | 0.815099332459549
(1 row)

Time: 33568,818 ms

It's true, stddev_samp(TRUNCATE) >> stddev_samp(DROP)

Regards
Pavel Stehule

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Adriaan van Os 2007-07-13 19:32:09 Re: TRUNCATE TABLE
Previous Message smiley2211 2007-07-13 18:27:50 Re: Database Statistics???