count(*), EXISTS, indexes

Поиск
Список
Период
Сортировка
От Itai Zukerman
Тема count(*), EXISTS, indexes
Дата
Msg-id 874r54sr8q.fsf@matt.w80.math-hat.com
обсуждение исходный текст
Ответы Re: count(*), EXISTS, indexes  (Josh Berkus <josh@agliodbs.com>)
Re: count(*), EXISTS, indexes  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: count(*), EXISTS, indexes  (Itai Zukerman <zukerman@math-hat.com>)
Список pgsql-sql
Define:
 CREATE TABLE A (x int PRIMARY KEY, real v); CREATE TABLE B (x int);

I'd like to calculate:
 SELECT sum(v) FROM A WHERE EXISTS (SELECT 1 FROM B WHERE A.x=B.x);

...but then it won't use the primary key index on A.x.

It *will* use the index with:
 SELECT sum(A.v) FROM A,B WHERE A.x=B.x;

...but that's not the same thing.  This is soooo close:
 SELECT count(DISTINCT A.x) FROM A,B WHERE A.x=B.x;

Am I going to have to write a plpgsql function for this?

PS.  B is relatively small, a few thousand rows, while A has well over
500,000 rows.  The DISTINCT A.x should be about 10,000-50,000.

-- 
Itai Zukerman  <http://www.math-hat.com/~zukerman/>



В списке pgsql-sql по дате отправления:

Предыдущее
От: Lee Harr
Дата:
Сообщение: Re: Concatenating does not work properly
Следующее
От: Josh Berkus
Дата:
Сообщение: Re: count(*), EXISTS, indexes