Skip to content
AI & Vectors

可扩展性工程

Building an enterprise-grade vector architecture.

向量的内容来源可以非常庞大。随着你的发展,你应该在几个二级数据库(有时称为“pods”)上运行你的向量工作负载,这样每个集合就可以独立扩展。

🌐 Content sources for vectors can be extremely large. As you grow you should run your Vector workloads across several secondary databases (sometimes called "pods"), which allows each collection to scale independently.

小型工作负载 #

🌐 Small workloads [#simple-workloads]

对于小型工作负载,你通常可以把数据存放在一个数据库里。

🌐 For small workloads, you can typically store your data in a single database.

如果你使用 Vecs 创建了 3 个不同的集合,你可以使用 views 将这些集合展示给你的网页或移动应用:

🌐 If you've used Vecs to create 3 different collections, you can expose collections to your web or mobile application using views:

下图显示了一个数据库,它包含了三个向量集合:docspostsimages。每个集合都通过视图向你的应用开放。

🌐 The diagram below shows a single database holding the three vector collections of docs, posts, and images. Each are exposed to your application through a view.

Architecture diagram: a single Supabase database holding three vector collections (docs, posts, and images), each exposed to the application through a view.

例如,对于三个集合,分别叫做 docspostsimages,我们可以像这样公开 schema 中的“docs”内容:

🌐 For example, with 3 collections, called docs, posts, and images, we could expose the "docs" inside the public schema like this:

1
create view public.docs as
2
select
3
id,
4
embedding,
5
metadata, # Expose the metadata as JSON
6
(metadata->>'url')::text as url # Extract the URL as a string
7
from vector

然后你就可以在应用中使用任何客户端库来访问你的集合了:

🌐 You can then use any of the client libraries to access your collections within your applications:

1
const { data, error } = await supabase
2
.from('docs')
3
.select('id, embedding, metadata')
4
.eq('url', '/hello-world')

企业工作负载 #

🌐 Enterprise workloads

当你进入生产阶段时,我们建议把你的集合拆分成不同的项目。这是因为这样可以让你的向量存储独立于生产数据进行扩展。向量通常增长比运营数据快,而且它们的资源需求也不同。在不同的数据库上运行它们可以避免单点故障。

🌐 As you move into production, we recommend splitting your collections into separate projects. This is because it allows your vector stores to scale independently of your production data. Vectors typically grow faster than operational data, and they have different resource requirements. Running them on separate databases removes the single-point-of-failure.

下图显示了一个主数据库,以及各自独立的次级“节点”数据库,每个节点都有自己的向量集合,这样它们就可以独立扩展。

🌐 The diagram below shows a primary database alongside separate secondary "pod" databases, each holding its own vector collection so they can scale independently.

Architecture diagram: a primary database alongside separate secondary 'pod' databases, each holding its own vector collection so collections can scale independently.

你可以根据需要使用任意数量的辅助数据库来管理你的集合。采用这种架构后,你在应用中访问集合有两种选择:

🌐 You can use as many secondary databases as you need to manage your collections. With this architecture, you have 2 options for accessing collections within your application:

  1. 直接用 Vecs 查询集合。
  2. 通过 Wrapper 访问你主数据库里的集合。

你可以同时使用这两者来适应你的使用场景。我们推荐尽可能使用选项 1,因为它提供了最大的可扩展性。

🌐 You can use both of these in tandem to suit your use-case. We recommend option 1 wherever possible, as it offers the most scalability.

使用 Vec 查询集合 #

🌐 Query collections using Vecs

Vecs 提供了查询集合的方法,可以使用余弦相似度函数或者通过元数据过滤

🌐 Vecs provides methods for querying collections, either using a cosine similarity function or with metadata filtering.

1
# cosine similarity
2
docs.query(query_vector=[0.4,0.5,0.6], limit=5)
3
4
# metadata filtering
5
docs.query(
6
query_vector=[0.4,0.5,0.6],
7
limit=5,
8
filters={"year": {"$eq": 2012}}, # metadata filters
9
)

使用封装器访问外部集合 #

🌐 Accessing external collections using Wrappers

Supabase 支持 外部数据封装器。这些封装器允许你将两个数据库连接在一起,这样你就可以通过网络对它们进行查询。

🌐 Supabase supports Foreign Data Wrappers. Wrappers allow you to connect two databases together so that you can query them over the network.

这涉及两个步骤:从主服务器连接到你的远程数据库,然后创建一个外部表。

🌐 This involves 2 steps: connecting to your remote database from the primary and creating a Foreign Table.

连接你的远程数据库 #

🌐 Connecting your remote database

在你的主数据库中,我们需要提供访问次级数据库的凭据:

🌐 Inside your Primary database we need to provide the credentials to access the secondary database:

1
create extension postgres_fdw;
2
3
create server docs_server
4
foreign data wrapper postgres_fdw
5
options (host 'db.xxx.supabase.co', port '5432', dbname 'postgres');
6
7
create user mapping for docs_user
8
server docs_server
9
options (user 'postgres', password 'password');

创建一个外部表 #

🌐 Create a foreign table

我们现在可以创建一个外部表来访问我们次要项目中的数据。

🌐 We can now create a foreign table to access the data in our secondary project.

1
create foreign table docs (
2
id text not null,
3
embedding extensions.vector(384),
4
metadata jsonb,
5
url text
6
)
7
server docs_server
8
options (schema_name 'public', table_name 'docs');

这看起来和我们上面的 View 示例非常相似,你可以继续使用客户端库通过外部表访问你的集合:

🌐 This looks very similar to our View example above, and you can continue to use the client libraries to access your collections through the foreign table:

1
const { data, error } = await supabase
2
.from('docs')
3
.select('id, embedding, metadata')
4
.eq('url', '/hello-world')

企业架构 #

🌐 Enterprise architecture

下面的图表提供了一个示例架构,允许你通过我们的客户端库或使用 Vecs 访问集合。你可以根据需要添加任意数量的辅助数据库(在这个例子中我们只展示了一个):

🌐 This diagram below provides an example architecture that allows you to access the collections either with our client libraries or using Vecs. You can add as many secondary databases as you need (in this example we only show one):

Enterprise architecture diagram: an application accessing vector collections in multiple secondary databases, either directly via Vecs or through the primary database using Foreign Data Wrappers.