Is unique index always used if query is on first constituent column ?

Поиск
Список
Период
Сортировка
Искать

Is unique index always used if query is on first constituent column ?

От:
"Rajesh Kumar Mallah" <mallah.rajesh@gmail.com>
Дата:
Hi,

suppose we have  2 columns a and b  . we need to query by a and sometimes
by b  for data consistency UNIQUE(a,b) also needs to be there. we create
indexes below:

create unique index ab on t(a,b);
create index b on t(b);

it has been observed that index ab is used while querying
for a , can we always avoid creating the extra index on a ie,
create index a on t(a); (does other databases also behave
similar?)

Regds
mallah.

Re: Is unique index always used if query is on first constituent column ?

От:
Bruno Wolff III <bruno@wolff.to>
Дата:
On Sat, Dec 30, 2006 at 16:08:39 +0530,
  Rajesh Kumar Mallah  wrote:
> Hi,
> 
> suppose we have  2 columns a and b  . we need to query by a and sometimes
> by b  for data consistency UNIQUE(a,b) also needs to be there. we create
> indexes below:
> 
> create unique index ab on t(a,b);
> create index b on t(b);
> 
> it has been observed that index ab is used while querying
> for a , can we always avoid creating the extra index on a ie,
> create index a on t(a); (does other databases also behave
> similar?)

If the b data is large you might get some performance increase from adding
an index on just a. You will be trading off increased work when doing updates
to handle the extra index versus faster searches in a more compact index.

Re: Is unique index always used if query is on first constituent column ?

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Bruno Wolff III  writes:
>   Rajesh Kumar Mallah  wrote:
>> create unique index ab on t(a,b);
>> create index b on t(b);

> If the b data is large you might get some performance increase from adding
> an index on just a. You will be trading off increased work when doing updates
> to handle the extra index versus faster searches in a more compact index.

This is covered in some detail in TFM:
http://www.postgresql.org/docs/8.2/static/indexes-multicolumn.html
http://www.postgresql.org/docs/8.2/static/indexes-bitmap-scans.html

			regards, tom lane
FAQ