Re: slow delete

Поиск
Список
Период
Сортировка
От Craig Ringer
Тема Re: slow delete
Дата
Msg-id 486DB22F.10106@postnewspapers.com.au
обсуждение исходный текст
Ответ на slow delete  (Jessica Richard <rjessil@yahoo.com>)
Список pgsql-performance
Jessica Richard wrote:
> I have a table with 29K rows total and I need to delete about 80K out of it.

I assume you meant 290K or something.

> I have a b-tree index on column cola (varchar(255) ) for my where clause
> to use.
>
> my "select count(*) from test where cola = 'abc' runs very fast,
>
> but my actual "delete from test where cola = 'abc';" takes forever,
> never can finish and I haven't figured why....

When you delete, the database server must:

- Check all foreign keys referencing the data being deleted
- Update all indexes on the data being deleted
- and actually flag the tuples as deleted by your transaction

All of which takes time. It's a much slower operation than a query that
just has to find out how many tuples match the search criteria like your
SELECT does.

How many indexes do you have on the table you're deleting from? How many
foreign key constraints are there to the table you're deleting from?

If you find that it just takes too long, you could drop the indexes and
foreign key constraints, do the delete, then recreate the indexes and
foreign key constraints. This can sometimes be faster, depending on just
what proportion of the table must be deleted.

Additionally, remember to VACUUM ANALYZE the table after that sort of
big change. AFAIK you shouldn't really have to if autovacuum is doing
its job, but it's not a bad idea anyway.

--
Craig Ringer

В списке pgsql-performance по дате отправления:

Предыдущее
От: idc danny
Дата:
Сообщение: Define all IP's in the world in pg_hba.conf
Следующее
От: Russell Smith
Дата:
Сообщение: Re: Define all IP's in the world in pg_hba.conf