**** **** EDIT'IN'クエリを最適化するにはどうすればよいですか?
14msは多くのことを思えないかもしれない、しかし、あなたは「PostgresSQLの説明」には以下を参照することができますよう、PostgreSQLの80,000行に配列スキャンを行っています。このスキャンを避け、いくつかのインデックス検索を行う方法がなければなりません。
**** EDITのEND ****
私は、スキーマレスなアイデアで遊んでいると私は、次の3つのテーブルを持っている:
テーブル10万件のランダムエントリが移入されています。 index_username_email(key)
上の非ユニークインデックスを持つ
entities(_primary_key SERIAL PRIMARY KEY, _id CHAR(32) UNIQUE,
data BYTEA)
index_username_profile_names(_id CHARE(32) PRIMARY KEY,
key VARCHAR UNIQUE)
index_username_email(_id CHAR(32) PRIMARY KEY, key VARCHAR)
私のSQLクエリは次のとおりです。
SELECT data FROM entities WHERE
_id IN (SELECT _id FROM index_users_email WHERE key = 'test')
OR
_id in (SELECT _id FROM index_users_profile_name WHERE key = 'test')
「テストは」「インデックスのどちらかではない終了をするが、これは百日咳約14msを消費しとります私がPostgreSQLかMySQLを使用していても、それは私が間違っているものでなければなりません。
どのように私はそれを最適化することができますか、または何を間違っているのですか?
ありがとうございます!
Postgresは説明:1つの潜在的な答えはunion
とjoin
あるよう
Seq Scan on entities (cost=16.88..4776.15 rows=80414 width=163) (actual time=15.169..15.169 rows=0 loops=1)
Filter: ((hashed SubPlan 1) OR (hashed SubPlan 2))
Rows Removed by Filter: 107218
SubPlan 1
-> Index Scan using index_users_email_key_idx1 on index_users_email (cost=0.42..8.44 rows=1 width=33) (actual time=0.039..0.039 rows=0 loops=1)
Index Cond: ((key)::text = 'test'::text)
SubPlan 2
-> Index Scan using index_users_profile_name_key_idx1 on index_users_profile_name (cost=0.42..8.44 rows=1 width=33) (actual time=0.071..0.071 rows=0 loops=1)
Index Cond: ((key)::text = 'test'::text)
Planning time: 0.202 ms
Execution time: 15.216 ms
14 *ミリ秒* "なんと" ではありません。 –
1ms未満でなければなりません:D –
それ以下はありません8 ms – Drew