-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" = '[email protected]' ) 
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

10
  • You ll have to create separate index for every column. Commented May 26, 2015 at 10:01
  • @usmanali, It did not change anything. Commented May 26, 2015 at 10:04
  • 1
    Please could you edit in that EXPLAIN ANALYZE output as text. Or perhaps paste it into explain.depesz.com Commented May 26, 2015 at 10:29
  • @IMSoP, done. I add link to explain.depesz.com Commented May 26, 2015 at 10:42
  • 3
    gain is minimum, since boolean index has low cardinality... unless you use it to "cluster" other column,based on the current boolean Commented May 26, 2015 at 12:11

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.