0Pricing
Vector Databases: Pinecone, Weaviate & pgvector · レッスン

pgvectorで距離指標を選ぶ

pgvectorのコサイン距離、L2距離、内積の演算子を理解し、Embeddingに適したものを選ぶ方法を学びます。

「pgvectorで距離指標を選ぶ」はCoddyKit上の無料Vector Databases: Pinecone, Weaviate & pgvectorレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはVector Databases: Pinecone, Weaviate & pgvector学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Vector Databases: Pinecone, Weaviate & pgvectorコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Why Distance Matters

Similarity search ranks vectors by distance. The metric you choose must match how your embedding model was trained, or results will be subtly wrong.

The Three Operators

pgvector provides three distance operators:

  • <-> Euclidean (L2) distance
  • <=> Cosine distance
  • <#> Negative inner product
SELECT '[1,2,3]'::vector <=> '[1,2,4]'::vector AS cosine_distance;

Cosine Distance

Cosine measures angle, ignoring magnitude. It is the default for most text embedding models like OpenAI and Sentence Transformers.

SELECT id, embedding <=> '[0.1,0.2,0.3]'::vector AS dist
FROM items
ORDER BY dist
LIMIT 5;

Euclidean (L2) Distance

L2 measures straight-line distance and is sensitive to magnitude. Use it when vector length carries meaning, such as raw feature vectors.

SELECT id, embedding <-> '[0.1,0.2,0.3]'::vector AS dist
FROM items
ORDER BY dist
LIMIT 5;

Inner Product

The inner product operator <#> returns the negative dot product (so smaller is more similar). It is fastest and ideal for normalized vectors.

SELECT id, (embedding <#> '[0.1,0.2,0.3]'::vector) * -1 AS similarity
FROM items
ORDER BY embedding <#> '[0.1,0.2,0.3]'::vector
LIMIT 5;

Match Metric to Model

Always check your embedding model's documentation:

  • OpenAI text-embedding-3: cosine
  • Normalized vectors: inner product (equivalent to cosine, faster)
  • Geometric/spatial features: L2

Normalization Trick

If your vectors are unit-normalized, cosine and inner product give the same ranking. Inner product is cheaper, so normalize once at insert time.

-- Normalize before insert in app code, then use <#>
UPDATE items SET embedding = l2_normalize(embedding);

Metric in Indexes

Indexes are metric-specific. You must declare the operator class so the index matches your query operator.

CREATE INDEX ON items
USING hnsw (embedding vector_cosine_ops);

Mismatched Operators

If your index uses vector_l2_ops but you query with <=>, Postgres ignores the index and does a slow sequential scan. Keep them aligned.

Verifying Index Usage

Use EXPLAIN to confirm the planner uses your vector index for the chosen metric.

EXPLAIN ANALYZE
SELECT id FROM items
ORDER BY embedding <=> '[0.1,0.2,0.3]'::vector
LIMIT 5;

Summary of Choices

Rule of thumb:

  • Text/semantic search: cosine
  • Pre-normalized vectors: inner product
  • Raw numeric features: L2

Quick Check

Pick the right metric.

Recap

You learned pgvector's three distance operators, when to use cosine, L2, and inner product, and why your index operator class must match your query operator for fast searches.

よくある質問

「pgvectorで距離指標を選ぶ」レッスンは無料ですか?

はい。「pgvectorで距離指標を選ぶ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Vector Databases: Pinecone, Weaviate & pgvectorコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Vector Databases: Pinecone, Weaviate & pgvectorコースには全4レッスンが含まれています。

「pgvectorで距離指標を選ぶ」で何を学びますか?

pgvectorのコサイン距離、L2距離、内積の演算子を理解し、Embeddingに適したものを選ぶ方法を学びます。 ブラウザで直接実行するハンズオンコードでVector Databases: Pinecone, Weaviate & pgvectorを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Vector Databases: Pinecone, Weaviate & pgvectorを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのVector Databases: Pinecone, Weaviate & pgvectorは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「pgvectorで距離指標を選ぶ」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このVector Databases: Pinecone, Weaviate & pgvectorレッスンでコードを書いて実行できますか?

はい。すべてのVector Databases: Pinecone, Weaviate & pgvectorレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. pgvector拡張機能のセットアップ
  2. PostgreSQLでのベクトル保存
  3. 類似度クエリの実行
  4. pgvectorで距離指標を選ぶ
← Vector Databases: Pinecone, Weaviate & pgvectorに戻る