-1

I have table that has 5 boolean columns.

on_stock | paid | received_payment | on_the_way | received

How to create an index for this table? Should I do it? I want to optimize a query like this:

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 ;

When I try add this index - nothing happens. This index is not used.

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

If I add separate index for each columns - nothing happens too.

EXPLAIN ANALYZE output: http://explain.depesz.com/s/FS2

ViT-Vetal-
  • 2,431
  • 3
  • 19
  • 35

1 Answers1

1

Your table has a total of 8 rows, an index is not needed in this case. It is way faster to test each of this 8 rows against the where clause than to use an index here.

Clément Prévost
  • 8,000
  • 2
  • 36
  • 51