Sunday, November 29, 2026

Oracle Database Performance: Avoiding Unnecessary Table Scans

Oracle Database Performance: Avoiding Unnecessary Table Scans

One of the common recommendations in database performance tuning is to avoid full table scans on large tables.

However, there is an important distinction:

A Full Table Scan (TABLE ACCESS FULL) is not necessarily a performance problem. An inefficient access path is.

The objective of SQL tuning should not be to eliminate table scans, but to ensure that the optimizer chooses the most efficient access path based on the query, data distribution, available indexes, and system workload.

1. When Can a Table Scan Become a Problem?

A table scan reads all relevant table blocks, regardless of how many rows ultimately satisfy the query predicate.

For example, consider a PRODUCT table containing millions of rows:

CREATE TABLE product (

    sku         CHAR(6)       NOT NULL,

    type        CHAR(1)       NOT NULL,

    name        VARCHAR2(50)  NOT NULL,

    description VARCHAR2(200)

);

CREATE UNIQUE INDEX i1 ON product(sku);

CREATE INDEX i2 ON product(name);

CREATE INDEX i3 ON product(type);

Consider the following query:

SELECT * FROM product WHERE sku = 'a12345';

Because SKU is uniquely indexed, Oracle can efficiently locate the required row using an index access path, typically an INDEX UNIQUE SCAN followed by TABLE ACCESS BY INDEX ROWID.

This is an excellent example of a highly selective predicate.

2. An Index Does Not Automatically Guarantee Good Performance

Consider:

SELECT * FROM product WHERE sku > 'abc';

Although SKU is indexed, this condition might match a significant percentage of the table.

If the optimizer estimates that many rows will be returned, a Full Table Scan may be cheaper than accessing the index and subsequently retrieving table blocks through ROWIDs.

Remember: Index availability does not mean index usage is always desirable.

3. Leading Wildcards and Index Access

Consider:

SELECT * ROM product WHERE description LIKE '%shoes%';

A conventional B-tree index on DESCRIPTION generally cannot support an efficient range scan for this predicate because the search pattern starts with a wildcard.

Compare it with:

SELECT * FROM product WHERE name LIKE 'stereo%';

A conventional B-tree index may be usable for this prefix search, subject to data types, collation, and optimizer conditions.

However, for substring searches, alternative approaches such as Oracle Text indexes may be more appropriate, depending on the search requirements.

4. Use Partitioning to Reduce the Amount of Data Scanned

For very large tables, indexing is not the only solution. Partitioning can significantly reduce the amount of data Oracle needs to access.

Oracle Partitioning divides a large logical table into smaller, manageable physical segments based on a partitioning key.

For example, consider a transaction table containing several years of banking transactions.

CREATE TABLE transactions (

    transaction_id NUMBER,

    transaction_date DATE,

    account_id NUMBER,

    amount NUMBER

)

PARTITION BY RANGE (transaction_date)

INTERVAL (NUMTOYMINTERVAL(1, 'MONTH'))

(

    PARTITION p_initial

    VALUES LESS THAN (DATE '2025-01-01')

);

Now consider:

SELECT * FROM transactions WHERE transaction_date >= DATE '2026-01-01'

  AND transaction_date <  DATE '2026-02-01';

Instead of scanning the entire table, Oracle can use Partition Pruning to eliminate partitions that cannot contain matching rows.

Benefits include:

  • Reduced I/O by accessing only relevant partitions.
  • Improved query performance for partition-key predicates.
  • Easier data lifecycle management, including archival and purging.
  • Better manageability of large tables.
  • Support for partition-level maintenance operations.

Important: Partitioning does not automatically eliminate Full Table Scans. Oracle may still perform a Full Table Scan, but only on the relevant partition or partitions.

Also, partitioning is not a replacement for indexing. Depending on the workload, local or global indexes may complement partitioning.

5. What Should We Consider Before Blaming TABLE ACCESS FULL?

When investigating a Full Table Scan, consider these factors:

• Table Size: Scanning a small table can be cheaper than using an index.

• Selectivity: How many rows are expected to satisfy the predicate?

• Clustering Factor: How efficiently are table rows physically organized relative to index key order?

• Data Distribution: Are optimizer statistics and histograms representative of the actual data?

• Selected Columns: Does the query require most of the table's rows or columns?

• Multiblock I/O: Full Table Scans can benefit from multiblock reads and efficient sequential I/O.

• System Workload: Oracle's optimizer considers factors such as system statistics and the relative costs of CPU and I/O.

6. Practical Performance Tuning Recommendation

Instead of blindly forcing index access with hints, investigate the actual execution plan and runtime statistics.

For example:

SELECT * FROM product WHERE sku = 'a12345';

SELECT *FROM TABLE (DBMS_XPLAN.DISPLAY_CURSOR (NULL, NULL, 'ALLSTATS LAST +PREDICATE’));

 

Pay attention to:

  • E-Rows vs. A-Rows (estimated vs. actual rows).
  • Buffers (logical reads).
  • Physical reads.
  • Predicate information.
  • Access paths and execution time.

7. Use Oracle SQL Access Advisor

Beyond manual SQL tuning and execution plan analysis, Oracle provides SQL Access Advisor, which helps identify opportunities to improve SQL workload performance through appropriate access structures.

SQL Access Advisor analyzes a representative SQL workload and can recommend:

  • Creating or modifying indexes.
  • Implementing partitioning strategies.
  • Using materialized views where appropriate.
  • Improving existing access structures based on workload characteristics.

 The advisor provides the following advantages:

  • Does not require you to have expert knowledge
  • Makes decisions based on rules that reside in the optimizer
  • Covers all aspects of SQL access in a single advisor
  • Provides simple, user-friendly GUI wizards in Cloud Control
  • Generates scripts for implementation of recommendations

 

Rather than creating indexes based on individual queries, SQL Access Advisor considers the broader workload to help identify access structures that can provide performance benefits.

This is particularly useful in environments with large tables, complex SQL workloads, and recurring queries where selecting the right combination of indexes and partitioning strategies can make a significant difference.

The advisor can recommend that you create database objects such as the following:

  • Indexes

SQL Access Advisor index recommendations include bitmap, function-based, and B-tree indexes. A bitmap index offers a reduced response time for many types of ad hoc queries and reduced storage requirements compared to other indexing techniques. B-tree indexes are most commonly used in a data warehouse to index unique or near-unique keys. SQL Access Advisor materialized view recommendations include fast refreshable and full refreshable materialized views, for either general rewrite or exact text match rewrite.

  • Materialized views

SQL Access Advisor, using the TUNE_MVIEW procedure, also recommends how to optimize materialized views so that they can be fast refreshable and take advantage of general query rewrite.

  • Materialized view logs

A materialized view log is a table at the materialized view's primary site or primary materialized view site that records all DML changes to the primary table or primary materialized view. A fast refresh of a materialized view is possible only if the materialized view's primary has a materialized view log.

  • Partitions

SQL Access Advisor can recommend partitioning on an existing unpartitioned base table to improve performance. Furthermore, it may recommend new indexes and materialized views that are themselves partitioned.

While creating new partitioned indexes and materialized view is no different from the unpartitioned case, partition existing base tables with care. This is especially true when indexes, views, constraints, or triggers are defined on the table.

To make recommendations, SQL Access Advisor relies on structural statistics about table and index cardinalities of dimension level columns, JOIN KEY columns, and fact table key columns. You can gather exact or estimated statistics with the DBMS_STATS package.

8. Using partial Index

In Oracle Database, partial indexes work in two distinct ways depending on whether your table is partitioned or non-partitioned:

  1. Native Partial Indexes (Partitioned Tables Only): Introduced in Oracle 12c, this allows you to index only specific partitions of a table.
  2. Simulated Partial Indexes (Any Table): Because Oracle does not natively support a CREATE INDEX ... WHERE clause (unlike PostgreSQL or SQL Server), you simulate row-level partial indexes using Function-Based Indexes or Virtual Columns.

 

8.1. Native Partial Indexes (For Partitioned Tables)

If you have a partitioned table (e.g., partitioned by date), you often only want to index recent, active partitions while leaving historical partitions unindexed to save storage and reduce DML overhead.

  • Step 1: Set the indexing property on table partitions. You can set INDEXING OFF for partitions you want to exclude (the default is ON).

CREATE TABLE sales (

    sale_id NUMBER,

    sale_date DATE,

    amount NUMBER

)

PARTITION BY RANGE (sale_date) (

    PARTITION p_2025 VALUES LESS THAN (DATE '2026-01-01') INDEXING OFF,

    PARTITION p_2026 VALUES LESS THAN (DATE '2027-01-01') INDEXING ON

);

  • Step 2: Create the index with INDEXING PARTIAL. This works for both local and global indexes.

CREATE INDEX idx_sales_amount ON sales(amount) LOCAL INDEXING PARTIAL;

Result: Oracle builds usable index segments only for p_2026 and marks p_2025 as UNUSABLE (consuming zero space).

8.2. Row-Level Partial Indexes (For Non-Partitioned Tables)

To index a specific subset of rows (e.g., WHERE status = 'PENDING'), you exploit a core Oracle behavior: standard B-tree indexes do not store entries where all indexed columns evaluate to NULL.

Method A: Function-Based Index

Use a CASE expression inside the CREATE INDEX statement so that unwanted rows evaluate to NULL:

CREATE INDEX idx_orders_pending ON orders (

    CASE WHEN status = 'PENDING' THEN order_id ELSE NULL END);

  • Crucial Caveat: To use this index, your query's WHERE clause must match the exact CASE expression:

SELECT * FROM orders

WHERE (CASE WHEN status = 'PENDING' THEN order_id ELSE NULL END) = 105;

Method B: Virtual Column (Cleaner for Queries)

Instead of repeating complex CASE logic in every query, define the condition once on the table:

ALTER TABLE orders ADD (

    pending_order_id GENERATED ALWAYS AS (

        CASE WHEN status = 'PENDING' THEN order_id ELSE NULL END

    ) VIRTUAL

);

CREATE INDEX idx_orders_pending ON orders(pending_order_id);

You can then query the virtual column directly (WHERE pending_order_id = 105), and the index will only contain rows where status = 'PENDING'.

 

Enforcing Conditional Uniqueness

This technique is frequently used to enforce unique constraints on a subset of rows (for example, ensuring a user can only have one active order at a time, while allowing multiple completed orders):

CREATE UNIQUE INDEX uq_one_active_order ON orders (

    CASE WHEN status = 'ACTIVE' THEN customer_id ELSE NULL END

);

 

Quick Comparison

Approach

Scope

Use Case

Query Impact

INDEXING PARTIAL

Partition-level

Skipping old/archive partitions

Transparent to queries

Function-Based Index

Row-level

Indexing rare statuses or conditional uniqueness

Requires matching expression in WHERE

Virtual Column + Index

Row-level

Same as above, with cleaner SQL

Query the virtual column name directly

Tips to considering partial index:

  • Whether your table is partitioned or non-partitioned
  • Your filtering condition (e.g., specific status or date range)
  • Whether you need a unique constraint or just faster lookups

 

Final Thought

A well-designed database does not necessarily have zero Full Table Scans.

It has execution plans where the chosen access path is appropriate for the workload.

Ø  The real tuning question is not: "Why is Oracle performing a Full Table Scan?"

It is:

Ø  "Given the table size, selectivity, data distribution, and workload, is a Full Table Scan actually more expensive than the available alternatives?"

And some tips:

LIKE '%value%': A conventional B-tree index generally cannot provide a normal index range scan for this predicate. Oracle Text is worth considering for genuine text-search workloads.

Selectivity is not the only factor: Clustering factor, table block count, multiblock read characteristics, and the cost of accessing table rows can make a Full Table Scan preferable.

Don't use INDEX hints as a default solution: Forcing index access can actually increase logical reads and degrade performance.

Use SQL Patch or Oracle SPM to modify/ tune execution plan in some scenario.

Use Extended statistics and check histograms to achieve better plan.

*****************************

What is your approach when you encounter TABLE ACCESS FULL on a large Oracle table?

 

References:

https://docs.oracle.com/en/database/oracle/oracle-database/26/tgsql/optimizer-access-paths.html

https://docs.oracle.com/en/database/oracle/oracle-database/21/tgsql/sql-access-advisor.html

https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/CREATE-INDEX.html

 

 

 

 

 


No comments:

Post a Comment

Oracle Database Performance: Avoiding Unnecessary Table Scans

Oracle Database Performance: Avoiding Unnecessary Table Scans One of the common recommendations in database performance tuning is to avoid...