Indexing queries with bit masks

From: Mike Christensen <mike(at)kitchenpc(dot)com>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Indexing queries with bit masks
Date: 2010-04-30 08:55:52
Message-ID: p2j7aa638e01004300155x1e1b721flf4c4024f9725b712@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I want a column in my Users table that will keep track of which types of
notifications the user wants to subscribe to. There's probably about 10
different types, so I don't want to have 10 boolean columns because this
seems kinda hacky and makes adding new types more work. So I'm thinking
about using a 32bit integer type and storing the data as a bitmask.

When a certain event happens, let's say event 4, I need to query for which
users to notify. So I'll be doing something like:

SELECT UserId FROM Users WHERE Subscriptions & 8;

(I haven't checked this syntax but I'm assuming that's how you do it)..

My question is say there's a million rows in the Users table. If I have an
index on Subscriptions, will this index be used in the above query? Is
there another good way to make this query super fast, or is my approach
totally dumb? I haven't implemented this yet so I'm open to new clever
ideas. Thanks!!

Mike

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Kenichiro Tanaka 2010-04-30 09:33:25 Re: Select with string that has a lone hyphen yields nothing
Previous Message Jorge Arevalo 2010-04-30 08:24:50 Re: Writing SRF