【问题标题】:Why isn't my hasura query using an index scan?为什么我的 hasura 查询不使用索引扫描?
【发布时间】:2021-08-15 23:27:58
【问题描述】:

我正在遵循 Hasura 关于如何优化我的查询的指南:

https://hasura.io/docs/latest/graphql/core/databases/postgres/queries/performance.html#data-validation-pg-indexes

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

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

索引:

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)

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

【问题讨论】:

  • 如果表很小,数据库引擎本身会忽略索引。值得注意的是,这是一个数据库引擎问题,与 Hasura 无关。编辑:这并不完全正确,Postgres 可以决定不使用索引是否更有效,并且大小表都可能发生这种情况。

标签: postgresql indexing graphql query-optimization hasura


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-01-31
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    • 2021-05-23
    • 2013-11-28
    • 2015-08-09
    相关资源
    最近更新 更多