【问题标题】:Create index for boolean columns为布尔列创建索引
【发布时间】:2015-08-07 22:24:55
【问题描述】:

我有一个有 5 个布尔列的表。

on_stock | paid | received_payment | on_the_way | received

如何为这个表创建索引?我应该这样做吗?我想优化这样的查询:

SELECT "order".* FROM "order" INNER JOIN "seller_users" ON "order"."seller_foreign_id" = "seller_users"."google_id" 
WHERE(((("order"."on_stock" <> true AND "order"."on_the_way" <> true ) AND "order"."paid" <> true ) AND "order"."received_payment" <> true ) AND "order"."received" <> true ) AND ("seller_users"."google_id" = 'lala@gmail.com' ) 
ORDER BY "order"."updated_at" DESC ;

当我尝试添加此索引时 - 没有任何反应。未使用此索引。

 add_index :order, [:on_stock, :on_the_way, :paid, :received_payment, :received], :name => "state_index"

如果我为每列添加单独的 index - 也不会发生任何事情。

EXPLAIN ANALYZE 输出: http://explain.depesz.com/s/FS2

【问题讨论】:

  • 您必须为每一列创建单独的index
  • @usmanali,它没有改变任何东西。
  • 请你 edit 在解释分析输出作为文本。或者将其粘贴到explain.depesz.com
  • @IMSoP,完成。我添加了指向 explain.depesz.com 的链接
  • 增益是最小的,因为布尔索引的基数很低......除非你用它来“聚集”其他列,基于当前的布尔值

标签: ruby-on-rails database postgresql indexing


【解决方案1】:

您的表共有 8 行,在这种情况下不需要索引。根据where 子句测试这 8 行中的每一行比在此处使用索引要快得多。

【讨论】:

    猜你喜欢
    • 2017-08-15
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    • 2020-07-29
    • 2017-04-25
    • 2013-06-19
    • 1970-01-01
    相关资源
    最近更新 更多