为什么我的 hasura 查询不使用索引扫描?



我遵循Hasura关于如何优化我的查询的指南,该指南位于:

https://hasura.io/docs/latest/graphql/core/databases/postgres/queries/performance.html#data-验证pg索引

但我的查询仍然执行顺序扫描而不是索引扫描,我不明白为什么。

我使用了不可为null的标量变量,并创建了索引,但问题仍然存在。

索引:

CREATE INDEX shop_index ON "shop" (shop_origin);

查询:

query get_storefront_data ($shop_origin: String!) {
shop_by_pk(shop_origin: $shop_origin) {
app_subscription_type
currency_code
usage_count
}      
}

变量:

{
"shop_origin": "test.myshopify.com"
}

执行计划(由Hasura控制台中的"分析"按钮生成(:

Aggregate  (cost=1.05..1.07 rows=1 width=32)
->  Seq Scan on shop  (cost=0.00..1.04 rows=1 width=16)
Filter: (shop_origin = 'test.myshopify.com'::text)
SubPlan 1
->  Result  (cost=0.00..0.01 rows=1 width=32)

我做错了什么?如何让查询执行索引扫描而不是顺序扫描?

对于小表,顺序扫描是最有效的访问方法。使用实际数量的数据来执行有意义的性能测试。

最新更新