Python Django hstore字段和索引

Python Django hstore字段和索引,python,django,postgresql,indexing,hstore,Python,Django,Postgresql,Indexing,Hstore,我将Django 1.8和postgresql与新的hstore字段一起使用。我想对我的hstore字段中的值应用索引。然而,在阅读postgres的文档时,我觉得btree索引不是最好的方法,而gin索引更合适,因为我的hstore字段中有许多值,所有这些值都指向同一条记录。使用pgAdmin,我注意到如果我将db_index=True添加到我的hstore字段,它将创建一个btree索引。 我的问题是: btree索引在这里真的没用吗?我应该不使用它吗 我使用原始sql(移植中植入的)创建了

我将Django 1.8和postgresql与新的hstore字段一起使用。我想对我的hstore字段中的值应用索引。然而,在阅读postgres的文档时,我觉得btree索引不是最好的方法,而gin索引更合适,因为我的hstore字段中有许多值,所有这些值都指向同一条记录。使用pgAdmin,我注意到如果我将db_index=True添加到我的hstore字段,它将创建一个btree索引。 我的问题是:

  • btree索引在这里真的没用吗?我应该不使用它吗
  • 我使用原始sql(移植中植入的)创建了一个gin索引,我想知道这是否足够,我的orm筛选/获取方法是否有效,或者我是否也必须覆盖这些方法
  • 原始SQL如下所示:

    SELECT * from transaction WHERE ("transaction"."hstorefield" -> 'service_code') = somevalue
    

    GIN很好,尽管它确实会大大降低插入和更新的速度

    CREATE INDEX "ix_hsfield" ON "transaction" USING GIN ("hstorefield");
    
    如果您只需要在一个hstore键上加快查询速度,那么btree是可能的,并且会稍微快一些(并且在使用大于/小于运算符的查询上工作得更好)

    如果该键包含数字数据,并且希望与where子句中的数字进行比较(小于/大于),请确保编写如下索引:

    CREATE INDEX "ix_hsfieldkey" ON "transaction" ((hs_data -> 'service_code')::numeric);
    
    -- the index won't be used unless the field used in the queries match the index:
    SELECT * from transaction WHERE ("transaction"."hstorefield" -> 'service_code')::numeric = somevalue
    

    我建议现在使用
    jsonb
    和postgresql9.4。我认为
    hstore
    越来越成为传统功能。至于索引,您需要显示ORM运行的实际查询。选择*from transaction,其中(“transaction”。“hstorefield”->“service_code”)=somevalue
    CREATE INDEX "ix_hsfieldkey" ON "transaction" ((hs_data -> 'service_code')::numeric);
    
    -- the index won't be used unless the field used in the queries match the index:
    SELECT * from transaction WHERE ("transaction"."hstorefield" -> 'service_code')::numeric = somevalue