Re: Deleting 173000 records takes forever, blocks async queries for unrelated records

Поиск
Список
Период
Сортировка
От Michal Politowski
Тема Re: Deleting 173000 records takes forever, blocks async queries for unrelated records
Дата
Msg-id 20130201104136.GA27364@meep.pl
обсуждение исходный текст
Ответ на Deleting 173000 records takes forever, blocks async queries for unrelated records  (Alexander Farber <alexander.farber@gmail.com>)
Список pgsql-general
On Fri,  1 Feb 2013 10:38:26 +0100, Alexander Farber wrote:
> Hello,
>=20
> in a Facebook game running on
> PostgreSQL 8.4.13 and having so many players:
>=20
> #  select count(*) from pref_users;
>  count
> --------
>  223964
>=20
> I am trying to get rid of inactive users,
> who just visited the canvas page, but
> never played (I'm sure, Facebook has
> a clever-sounding name for them):
>=20
> # select count(*) from pref_users
> where id not in (select distinct id from pref_money);
>  count
> --------
>  173936
> (1 row)
>=20
> So I call:
>=20
> # delete from pref_users
> where id not in (select distinct id from pref_money);
>=20
> but that query lasts forever and
> what's more troubling me - it blocks
> the async queries of my game daemon
> (the Perl function pg_ready starts returning
> false all the time and my game accumulates
> thousands of yet-to-be-executed SQL queries).
>=20
> The good news is, that my quad server
> doesn't hang - I just see 1 postmaster
> process at 90-100% CPU but total load is 20%.
>=20
> Also my game daemon in Perl recovers
> and executes the thousands of queued
> up async queries, when I interrupt the
> above DELETE query with CTRL-C at
> the pgsql prompt - i.e. my game is not buggy.
>=20
> My question is how handle this?
>=20
> Why does deleting takes so long,
> is it because of CASCADES?
>=20
> And why does it make the pg_ready
> calls of my game daemon return false?
> The users I'm deleting aren't active,
> they shouldn't "intersect" with the
> async queries of my game daemon.
>=20
> Below are the both SQL tables involved,
> thank you for any insights.
>=20
> Regards
> Alex
>=20
> #  \d pref_money
>                         Table "public.pref_money"
>  Column |         Type          |                Modifiers
> --------+-----------------------+-----------------------------------------
>  id     | character varying(32) |
>  money  | integer               | not null
>  yw     | character(7)          | default to_char(now(), 'IYYY-IW'::text)
> Indexes:
>     "pref_money_money_index" btree (money DESC)
>     "pref_money_yw_index" btree (yw)
> Foreign-key constraints:
>     "pref_money_id_fkey" FOREIGN KEY (id) REFERENCES pref_users(id) ON
> DELETE CASCADE

I believe an index on pref_money.id could help.
Without it Postgres is, if I understand correctly, making 173936 table scan=
s on pref_money
to try (and fail) to find for each deleted row the referencing row in that =
table.
=20
> #  \d pref_users
[...]

--=20
Micha=B3 Politowski

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

Предыдущее
От: Albe Laurenz
Дата:
Сообщение: Re: Deleting 173000 records takes forever, blocks async queries for unrelated records
Следующее
От: Bèrto ëd Sèra
Дата:
Сообщение: Re: Deleting 173000 records takes forever, blocks async queries for unrelated records