Traditional database indexes answer “Which rows match this condition?”
Vector indexes answer “Which rows are most similar to this query?”
For example, previously we asked:
select * from hw3_movies where movidId = 1;
or
select * from hw3_movies where title like '%Toy Story%';
These are exact predicates. But what if the user asks: “Find the movies most similar to Toy Story?” A B-Tree index can’t really do that.
The Central Question: How can we represent and query similarity?
Vectors and Embeddings
A vector in two dimensions is a pair such as (0.95, 0.92). In real applications, embeddings can have hundreds or thousands of dimensions.
A model computes embeddings from text, images, audio, users, or other objects; a vector database stores and searches those embeddings. It does not typically create them.
Movie Genre x y
Toy Story animation 0.95 0.92
Finding Nemo animation 0.88 0.85
The Incredibles animation 0.84 0.80
The Dark Knight action 0.15 0.12
Die Hard action 0.12 0.25
Mission: Impossible action 0.20 0.18
The Notebook romance 0.65 0.10
Pride & Prejudice romance 0.70 0.15
So if our query is q = (0.90, 0.90), which movies would you expect to be near the query?
In this case, “meaning” resides in how the vector was constructed and evaluated.
Distance
For two vectors $x$ and $q$ we can define Euclidean Distance (L2)
\[ d_2(q,x)=\sqrt{\sum_{i=1}^{d}(q_i-x_i)^2}. \]
So, for q(0.9, 0.9):
\[ d_2(q,\text{Toy Story})=\sqrt{(0.90-0.95)^2+(0.90-0.92)^2} =\sqrt{0.0029}\approx 0.05385. \]
\[ d_2(q,\text{Finding Nemo})=\sqrt{(0.90-0.88)^2+(0.90-0.85)^2} =\sqrt{0.0029}\approx 0.05385. \]
There are other ways to measure similarity:
- Dot product: \(q\cdot x=\sum_iq_ix_i\). Larger is more similar (depending on the embedding model).
- Cosine similarity: \(\operatorname{cos}(q,x)=\frac{q\cdot x}{\lVert q\rVert\lVert x\rVert}\). Larger is more similar; it is insensitive to multiplying a vector by a positive scalar.
Lets’ Build some Embeddings:
We need to start with some code:
python3.11 -m venv .venv
source .venv/bin/activate
pip install sentence-transformers
python mysql_movie_vectors.py build
python mysql_movie_vectors.py stats
python mysql_movie_vectors.py search 'toys come to life'
Vector Algebra
king − man + woman = queen
Paris − France + Italy = Rome
python mysql_movie_vectors.py algebra "doctor - hospital + university"