help: optimizer can't use index on Y to speed up select .. sum(X) ... group by Y
Date: 1995/08/23
Message-ID: <41fl6g$fk5_at_fred.cas-ps.com>#1/1
I have a table created by
CREATE TABLE T1 ( KEY CHAR(1) NOT NULL, VALUE NUMBER NOT NULL );
indexed on the key-column with
CREATE INDEX T1_01 ON T1 ( KEY )
Analyzing the query
SELECT KEY,SUM(VALUE) FROM T1 GROUP BY KEY;
i get the execution plan
SORT GROUP BY
TABLE ACCESS FULL T1
Since T1 is indexed on KEY, I'd expect the optimizer to avoid the
'SORT GROUP BY', but obviously it can't take advantage of the index.
Investigating further I found that
SELECT KEY,COUNT(*) FROM T1 GROUP BY KEY
is planned
SELECT MIN(KEY) FROM T1
is planned
SORT AGGREGATE
INDEX RANGE SCAN T1_01
whereas
SELECT MIN(KEY),MIN(KEY) FROM T1
is planned
SORT AGGREGATE
TABLE ACCESS FULL T1
and even
SORT GROUP BY
TABLE ACCESS FULL T1
The execution of the (more complex) original statement on a big table
requires enormous amounts of temp-space for sorting, writes lots of
redo-log and is very slow.
Of course I could do the task in PL/SQL with a cursor 'select key,value from
t1 order by key'
computing the sum of values per key 'by hand', but since this kind of problem
doesn't sound
uncommon I wonder, if there are other solutions.
Any experiences/help/hints/suggestions are appreciated
Thanks in advance
-- _/_/_/ _/_/_/ _/ _/ // Reinhard Kuhn / It can be _/ _/ _/ _/ _/ // (kuhn_at_cas-ps.com) / done quickly, _/_/_/ _/_/_/ _/_/ // CAS GmbH / cheaply or well _/ _/ _/ _/ _/ // Lemberger Strasse 14 / - pick any two! _/ _/ _/_/_/ _/ _/ // 66955 Pirmasens, Germany /Received on Wed Aug 23 1995 - 00:00:00 CEST