[Solved] Oracle SQL Developer: Calculating Selectivity

EverSQL Database Performance Knowledge Base

Oracle SQL Developer: Calculating Selectivity

Database type:

I have a table called FANS that stores demographic information on various people including name, address and DOB. The table itself contains 7,655 rows of data. I have created a single INDEX on the CITY column. I know for a certainty that there are only 40 unique cities in the table and there are no NULL values. I'm trying to run a simple experiment on when the optimizer begins to use the INDEX.

SELECT CITY
FROM FANS
WHERE CITY BETWEEN 'APOLLO BEACH' AND 'BRANDON';

I learned previous from MySQL that Selectivity = cardinality / number of records * 100% where cardinality would be the number of unique values in the index.

When I run "Explain Plan" in SQL developer, it returns a cardinality of 500. Thus the correct math would be 500 / 7,655 * 100%?

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `FANS` ADD INDEX `fans_idx_city` (`CITY`);
The optimized query:
SELECT
        FANS.CITY 
    FROM
        FANS 
    WHERE
        FANS.CITY BETWEEN 'APOLLO BEACH' AND 'BRANDON'

Related Articles



* original question posted on StackOverflow here.