Home » SQL & PL/SQL » SQL & PL/SQL » Using a field with low cardinality in a where clause.
Using a field with low cardinality in a where clause. [message #19547] Tue, 26 March 2002 20:14 Go to next message
Nabil Fanaian
Messages: 1
Registered: March 2002
Junior Member
Let's say I have a table defined as:
CREATE TABLE TABLEA( F1, F2, F3 )

Now lets say that the table has 1mil records in it, but there are only 3 distinct values for F3. If I execute the following query, Oracle 8i performs a FTS and takes 35 secs to return.
SELECT DISTINCT F1, F2, F3 FROM TABLEA WHERE F3 = 'A'

I create a bitmap index on F3 and Oracle still performs a FTS. I create a hint to use the bitmap index and the query takes 50 secs. Why is this happening?

However, if I remove the DISTINCT from my SQL statement, then the query completes in milliseconds.

How can I get this SQL to execute most efficiently when I need to use DISTINCT!? I works fine with the DISTINCT taken out, but I need to use the DISTINCT clause in my SQL stmt.

Thanks.
Re: Using a field with low cardinality in a where clause. [message #19551 is a reply to message #19547] Wed, 27 March 2002 05:47 Go to previous messageGo to next message
Alex Mazur
Messages: 17
Registered: March 2002
Junior Member
Interesting issue...
Have you gathered statistics before running the query?
Try to analyze table:

analyze table TABLEA estimate statistics
/

May be it will help.
Re: Using a field with low cardinality in a where clause. [message #19553 is a reply to message #19547] Wed, 27 March 2002 06:45 Go to previous messageGo to next message
Grant
Messages: 578
Registered: January 2002
Senior Member
Try compute statistics on the table and the columns. DISTINCT does a sort so you might want to increase SORT_ARE_SIZE.
Re: Using a field with low cardinality in a where clause. [message #19573 is a reply to message #19551] Thu, 28 March 2002 15:29 Go to previous message
Mike
Messages: 417
Registered: September 1998
Senior Member
Hi,

I think the reson why he does a full table scan is the following: the access of a row via an index costs about 5 or 10 times more ressources then a full table scan. So because you access about one third of your data with your condition WHERE F3 = 'A' the FTS is probably faster.

Because you want to get the distinct data the database has also to compare each record to see if it is a distinct value. A solution for this would probably be, as Alex said, to create a index like:
create index some_index on TABLE(f3,f2,f1)
the f3 value has to be the first column listed in the index.

HTH
Mike
Previous Topic: Execute Package.Function
Next Topic: Truncate and COALESCE
Goto Forum:
  


Current Time: Thu Apr 18 07:08:56 CDT 2024