Generative Sketching
for Relational Joins


Brian Tsan
Ph.D., Database Systems  ·  btsan@ucmerced.edu
About me

Research interests


Generative Models → Create Sketches → Faster Databases

Studied generative models to create sketches for better database query optimization:

  • Tsan et al., “Approximate Sketches,” SIGMOD 2024 — transformers.
  • Tsan et al., “Sketched Sum-Product Networks for Joins,” VLDB 2027 — sum-product networks.
Overview

What we'll cover


  1. What is sketching?join size · inner products · embedding distributions
  2. Tug-of-War Sketch
  3. Generative Sketching — my work
  4. Results: 8.6× faster than PostgreSQL on JOB
01
Section

What is sketching?

Sublinear summaries, and three examples.

1 · Definition

What is sketching?


The usage of “sketch” is somewhat colloquial. It refers to data structures that summarize data in sublinear space — usually in a single pass. Sometimes it refers to random projection, called linear sketching.

Example applications
  • Join sizes — used in relational database systems (our focus)
  • Inner products
  • Embedding distributions
1 · Definition

Join size


How many rows match?

Table 1
Table 2
1 · Definition

Nested-loop join


Compare every Table 1 row against every Table 2 row; a line marks a match.

Table 1
Table 2

\( |T_1 \Join T_2| = 5 \)

1 · Definition

The join result


Each match merges into one result row.

T1 ⋈ T2
T1 ⋈ T2
1 · Definition

Join size sketching


Project each table through the same random values per key — 4 rows down to 2.

⟨
Table 1
⋅
4×2
,
Table 2
⋅
4×2
⟩

This inner product approximates the join size \( |T_1 \Join T_2| \).

1 · Definition

Inner product sketching


Project each N-dimensional vector through the same sketching matrix.

⟨
⋮
N
⋅
⋮
N×2
,
⋮
N
⋅
⋮
N×2
⟩

\( \langle S^\top x_1,\, S^\top x_2 \rangle \approx \langle x_1, x_2 \rangle \)

1 · Definition

Embedding distribution


A context \( x \) and a target \( y \) are encoded into embeddings \( s_x, s_y \); a predictor takes \( s_x \) and a condition \( z \) and predicts \( s_y \).
Having to predict is what makes the encoder learn richer, information-preserving embeddings.

I-JEPA: a context block is encoded by a context encoder and target blocks by a target encoder; a predictor maps the context embedding plus a positional condition to each target block's embedding, trained against the target embeddings with an L2 loss.
Assran et al., “Self-Supervised Learning from Images with a Joint-Embedding Predictive Architecture,” CVPR 2023.
github.com/facebookresearch/ijepa
1 · Definition

Sketched Isotropic Gaussian Regularization


The CIFAR-10 embedding distribution as it trains.

A random 1-D projection of the latents (left), and the density of that sketch (right)
1 · Definition

Results from LeJEPA


Plain video (left), attention map demonstrating LeJEPA's emergent salient object tracking behavior (right).

A video of a paved path, beside an attention map that stays locked on the moving salient object. A video of a dog running across grass, beside an attention map tracking the dog.
Balestriero and LeCun, “LeJEPA: Provable and Scalable Self-Supervised Learning Without the Heuristics,” arXiv 2025.
github.com/galilai-group/lejepa
02
Section

Tug-of-War Sketch

Alon, Matias, and Szegedy, “The space complexity of approximating the frequency moments,” STOC 1996.

2 · Preliminary foundations

Frequency estimation


Every item pulls the counter one way or the other.

Stream
+ − + + − +
0 1 0 1 2 1 2

Each item pulls the counter one way. \( Z = \sum_i s(x_i) = 2 \)

2 · Preliminary foundations

Estimating each count


Multiply the counter by that colour's own sign.

Stream
− −
2
+ + + +
(+1) ⋅ 2 = 2
true 3
(−1) ⋅ 2 = −2
true 2
(+1) ⋅ 2 = 2
true 1

Was it correct?

2 · Preliminary foundations

Averaging many games


Re-draw the signs each round; every round is an independent estimate.

Stream
0
\( \xi:\ [N] \to \{-1, +1\} \)
\( \mathbb{E}\left[\xi(i)\,\xi(j)\right] = \begin{cases} 1, & i = j \\ 0, & i \neq j \end{cases} \)
avg —
true 3
avg —
true 2
avg —
true 1
round 0
2 · Preliminary foundations

Join size estimation


Two streams, one counter each — sketched with the same signs ξ.

Stream 1
+ − + + − +
0 1 0 1 2 1 2
Sketch 1
4
estimate
0 1 2 1 2
Sketch 2
Stream 2
+ + − +
True join size
(3 × 2) + (2 × 1) + (1 × 1) = 9
2 · Preliminary foundations

Count sketch


Width splits collisions into buckets; depth adds independent rows, combined by median.

Stream 1
Sketch 1
4
estimate
Sketch 2
Stream 2
width 1
depth 1
(3 × 2) + (2 × 1) + (1 × 1) = 9
2 · Preliminary foundations

A live stream


The stream keeps growing; each arrival lands in one bucket per row.

Stream 1
⋮
Sketch 1
4
estimate
Sketch 2
Stream 2
⋮
width 1
depth 1
03
Section

Generative Sketching

Learning a joint distribution to generate sketches.

3 · Generative sketching

The problem with databases


Tables usually have more than just one column.

Orders
key
key
vector · 4
3 · Generative sketching

The problem with databases


Tables usually have more than just one column.

Orders
key
date
date
key
matrix · 4×4
3 · Generative sketching

The problem with databases


Tables usually have more than just one column.

Orders
key
date
price
date
price
key
tensor · 4×4×4
3 · Generative sketching

Training the model


Every row is an example: the other columns in, the join key out.

Orders
key
date
price
model
→
output
−
target · loss
3 · Generative sketching

Training the model


Every row is an example: the other columns in, the join key out.

Orders
key
date
price
model
→
output
−
target · loss
3 · Generative sketching

Training the model


Every row is an example: the other columns in, the join key out.

Orders
key
date
price
model
→
output
−
target · loss
3 · Generative sketching

Training the model


Every row is an example: the other columns in, the join key out.

Orders
key
date
price
model
→
output
−
target · loss
3 · Generative sketching

Learning the key embeddings


Softmax over one embedding per key: the dictionary is an output layer of the model, trained with it.

model
→
output
⋅
key dictionary · d×N
=
P(key | row)
−
target · loss
3 · Generative sketching

Generating the sketch


The same product the loss trained — one dot product against the key embeddings gives the whole sketch.

model
→
output
⋅
key dictionary · d×N
=
sketch
3 · Generative sketching

Independent sketches


Each sketch has its own embedding dictionary.

model
→
output
⋅
dictionary 3
dictionary 2
dictionary 1
=
sketch 1
sketch 2
sketch 3
04
Section

8.6× faster than PostgreSQL

113 Join Order Benchmark queries against PostgreSQL 18.4.

4 · Results

The Join Order Benchmark


keyword 134K 3 cols
info_type 113 2 cols
company_name 235K 7 cols
movie_keyword 4.5M 3 cols
movie_info 14.8M 5 cols
movie_info_idx 1.4M 5 cols
person_info 3.0M 5 cols
aka_name 901K 8 cols
company_type 4 2 cols
movie_companies 2.6M 5 cols
title 2.5M 12 cols
cast_info 36.2M 7 cols
name 4.2M 9 cols
link_type 18 2 cols
movie_link 30K 4 cols
role_type 12 2 cols
char_name 3.1M 7 cols
comp_cast_type 4 2 cols
complete_cast 135K 4 cols
kind_type 7 2 cols
aka_title 361K 12 cols

21 tables · 108 columns · 74,190,187 rows · 23 join edges

4 · Results

Total query execution time


total
avg / query
PostgreSQLversion 18.4
118.8 min
63 s
Fast-AGMSour approximation
13.8 min
7.3 s

8.6×faster than PostgreSQL

113 Join Order Benchmark queries · IMDb, 74,190,187 rows
Tsan et al., “Sketched Sum-Product Networks for Joins,” VLDB 2027.

Wrap-up

Conclusion


  1. Sketching has various applications — join size, inner products, embedding distributions, etc.
  2. Generative models enable sketching for multivariate data.
  3. This talk: 10% content, 90% slide animations.
Bonus slide

Vector–matrix–vector product


One sketch per factor: \( x \) and \( y \) by their own hashes, \( A \) by the circular convolution of both — bucket \( (h_1(i)+h_2(j)) \bmod w \), sign \( \xi_1(i)\xi_2(j) \).

x⊤Ay = —
Sx
SA
⋆ circular cross-correlation
Sx⋆SA
⋅ dot
Sy
—avg — over 0 drawstrue —
width 1