2

For example, I store interests for Joe as chemistry and classical-music (as two separate values) , and you can query the table like where interests in ('chemistry', 'biochem') and it will use the index to efficiently find people such as Joe.

Does PostgreSQL have such a datatype that can index multiple values to allow advanced queries? What are the performance advantages for queriues? And what are the important caveats?

5
  • 2
    Why don't you normalize your design? Storing comma separated values is almost never a good idea. Commented Sep 25, 2012 at 9:39
  • I understand its bad in traditional DBs which dont index multiple values. But I just felt DBs must offer this feature. But I did update this question about caveats thanks to you :-) Commented Sep 25, 2012 at 10:05
  • 1
    The question is misleading/poorly worded. You're asking if it can index the values in a text field containing comma-separated values. Commented Sep 25, 2012 at 14:22
  • I think the caveat is that it's not a good design. If you want to use a RDBMS, then I second other people recommendation to create a proper, relational schema to store the data. People have been working for decades to create very efficient ways to do this kind of querying (via a separate "interests" table). PostgreSQL is quite good at it. Otherwise, I suggest you evaluate different storage engines. Commented Sep 25, 2012 at 19:46
  • @DondiMichaelStroma, corrected the poor wording Commented Sep 26, 2012 at 7:00

2 Answers 2

3

If you use an array datatype, it can be indexed and the index can be used, check this topic: Can PostgreSQL index array columns?

A better datamodel might be a better idea, but that's up to you.

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

Comments

1

You could use the hstore datatype for this. You would only store keys, not values. A hstore column can be indexed and testing for the presence of a key is really fast.

Something like:

create table person 
(
   person_id integer not null primary key,
   name text not null,
   interests hstore
);

insert into person (person_id, name, interests)
values 
(1, 'Joe', 'chemistry => NULL, biochem => null'::hstore);

Then to find a person with a specific interest you can use:

select *
from person
where interests ? 'chemistry'
   or interests ? 'biochem';

It's a bit of a hack because the hstore datatype is intended to store key/value pairs and in this case you only need the keys.

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.