借助 AI 函数加速,您可以在运行使用 AI 函数的查询时优化性能。它提供的性能与基于数组的 函数相同或更好, 而无需您编写复杂的查询。如需详细了解 AI 函数,请参阅 AI 函数 概览。
AI 函数加速支持以下 AI 函数:
ai.if:加速仅适用于函数位于WHERE子句中的SELECT查询。它支持每次扫描使用一个ai.if函数,并且适用于顺序扫描、索引扫描和位图堆扫描。如果查询包含多个表扫描(例如,针对不同的表、关系或通用表表达式 (CTE)),则您可以为每次扫描加速一个ai.if函数。ai.rank:加速仅适用于函数位于ORDER BY子句中的SELECT查询。它支持每次扫描使用一个ai.rank函数。如果查询包含多个表扫描(例如,针对不同的表、关系或 CTE),则您可以为每次扫描加速一个ai.rank函数。
启用 AI 函数加速
如需启用 AI 函数加速,请将
google_ml_integration.enable_ai_function_acceleration 标志设置为 on。此标志默认处于停用状态,用于控制是否允许查询执行器将符合条件的 AI 操作卸载到 AI 函数应用节点以进行加速。如果未设置或设置为
off,则所有查询操作都由标准 PostgreSQL 执行器执行。
您可以在会话级或实例级配置此标志。
如需在实例级启用 AI 函数加速,请使用 gcloud
alloydb instances update 命令:
gcloud alloydb instances update INSTANCE_ID \
--database-flags google_ml_integration.enable_ai_function_acceleration=on \
--region=REGION_ID \
--cluster=CLUSTER_ID \
--project=PROJECT_ID替换以下内容:
INSTANCE_ID:您要更新的实例的 ID。REGION_ID:集群所在的区域的 ID。如需了解详情,请参阅支持的区域。CLUSTER_ID:集群的 ID。PROJECT_ID:项目的 ID。
如需详细了解如何设置实例级标志,请参阅配置 数据库标志。
验证 AI 函数加速
如需验证 AI 函数加速是否已启用,您可以使用 EXPLAIN 语句来分析查询。
验证 ai.if 查询的 AI 函数加速
以下示例展示了如何为当前会话启用 AI 函数加速,并使用 ai.if 函数执行查询:
SET google_ml_integration.enable_ai_function_acceleration = on;
EXPLAIN (COSTS OFF) SELECT r.name
FROM restaurant_reviews r
WHERE ai.if('Is the following a positive review? Review: '||r.review) AND r.city = 'Los Angeles'
GROUP BY r.name
HAVING count(*) > 20
ORDER BY r.name;
当您使用 EXPLAIN 语句分析查询时,查询计划会显示一个 AI Function Apply 节点:
QUERY PLAN
----------------------------------------------------------------------------------------
GroupAggregate
Group Key: name
Filter: (count(*) > 20)
-> Sort
Sort Key: name
-> AI Function Apply
Filter: ai.if((('Is the following a positive review? Review: '::text || review)), NULL::character varying)
-> Index Scan using idx_restaurant_reviews_city on restaurant_reviews r
Index Cond: ((city)::text = 'Los Angeles'::text)
验证 ai.rank 查询的 AI 函数加速
以下示例展示了如何为当前会话启用 AI 函数加速,并使用 ai.rank 函数执行查询:
SET google_ml_integration.enable_ai_function_acceleration = on;
EXPLAIN (COSTS OFF) SELECT r.name, r.review
FROM restaurant_reviews r
WHERE r.city = 'Los Angeles'
ORDER BY ai.rank('Rank these reviews based on how much they emphasize high-quality ingredients. Review: ' || r.review) DESC
LIMIT 50;
当您使用 EXPLAIN 语句分析查询时,查询计划会显示一个 AI Function Apply 节点:
QUERY PLAN
----------------------------------------------------------------------------------------
Limit
-> Sort
Sort Key: (ai.rank(('Rank these reviews based on how much they emphasize high-quality ingredients. Review: ' || r.review'), NULL)) DESC
-> AI Function Apply
-> Bitmap Heap Scan on restaurant_reviews r
Recheck Cond: (city = 'Los Angeles')
-> Bitmap Index Scan on idx_restaurant_reviews_city
Index Cond: (city = 'Los Angeles')
查询计划中的 AIFunctionApply 节点表示查询使用了 AI 函数加速。如果此节点不存在,则查询使用了标准 PostgreSQL
执行。
限制
- 仅支持函数的基本实参。对于
ai.if和ai.rank,prompt实参必须是列引用或与列引用连接的字符串字面量。任何其他实参都必须是简单常量。受支持的prompt实参的示例包括:ai.if(r.review)ai.if('Is this true? : ' || r.review)ai.rank(r.review)ai.rank('Rate this review: ' || r.review)
- 仅支持
WHERE子句中包含ai.if或ORDER BY子句中包含ai.rank的SELECT查询。 - 每次表扫描仅支持一个 AI 函数。
- 仅支持默认 Gemini 模型。如需了解详情,请参阅 Gemini 模型。如需使用其他模型,您可以将
google_ml_integration.default_llm_model标志设置为您要使用的模型 ID。 - 此功能仅在支持
gemini-2.5-flash-lite模型的区域或您使用google_ml_integration.default_llm_model标志设置的 LLM 模型的区域提供。如需了解详情,请参阅支持的区域。
后续步骤
- 使用 AI 函数执行智能 SQL 查询。
- 使用优化后的函数加速 AI 查询。
- 使用基于数组的函数执行过滤