非常に具体的な質問を申し訳ありません。同じテーブルを参照する列を含む行を削除すると、時間がかかりません
私はテーブルを持っています(下を見てください)、私はそれから多くのレコードを削除しようとすると、私のPostgreSQL 8.2.5は親子制約を行う時間の98%を費やします。 私はそれを速くするためにどのインデックスを追加すべきかを理解しようとしています。 私は、このテーブルのすべての列がparent_block_id
として0またはnullのいずれかを持っていると言う必要があります。それは基本的なものです。
別のインデックスを追加しようとしました:just(parent_block_id);どこにparent_block_id = 0; parent_block_idはNULLです。 WHERE parent_block_id!= 0.これらのどちらも重大なパフォーマンス上の利益をもたらしませんでした。
varshavka=> explain analyze delete from infoblocks where template_id = 112;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------
Seq Scan on infoblocks (cost=0.00..1234.29 rows=9 width=6) (actual time=13.271..40.888 rows=40000 loops=1)
Filter: (template_id = 112)
Trigger for constraint $1: time=4051.219 calls=40000
Trigger for constraint $2: time=1616.194 calls=40000
Trigger for constraint cs_ibrs: time=2810.144 calls=40000
Trigger for constraint cs_ibct: time=4026.305 calls=40000
Trigger for constraint cs_ibbs: time=3517.640 calls=40000
Trigger for constraint cs_ibreq: time=774344.010 calls=40000
Total runtime: 790760.168 ms
(9 rows)
varshavka=> \d infoblocks
Table "public.infoblocks"
Column | Type | Modifiers
-----------------+-----------------------------+------------------------------------------------------
id | integer | not null default nextval(('IB_SEQ'::text)::regclass)
parent_block_id | integer |
nm_id | integer | default 0
template_id | integer | not null
author_id | integer |
birthdate | timestamp without time zone | not null
Indexes:
"infoblocks_pkey" PRIMARY KEY, btree (id)
"zeroparent" btree (parent_block_id) WHERE parent_block_id <> 0
Foreign-key constraints:
"$2" FOREIGN KEY (nm_id) REFERENCES newsmakers(nm_id) ON DELETE RESTRICT
"$5" FOREIGN KEY (author_id) REFERENCES users(user_id) ON DELETE RESTRICT
"cs_ibreq" FOREIGN KEY (parent_block_id) REFERENCES infoblocks(id) ON DELETE CASCADE
ヒントありがとう、私は明日それを試してみます。 VACUUM ANALYZEに関して、私はそれをしました、それは〜10倍の絶対数で助けましたが、根底にあるO(N^2)をちょっと変えませんでした。 – alamar
あなたは絶対に正しいですが、DELETE RESTRICTは魔法のように破壊されました! – alamar