Databases 7 min read

How Indexing Can Slash Oracle Query Time from Minutes to Seconds

This article examines a real‑world Oracle SQL query, identifies costly full‑table scans and low‑selectivity columns, and demonstrates how creating an index on a key column reduces execution time from 24 minutes to just one second, highlighting essential SQL performance‑tuning techniques.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How Indexing Can Slash Oracle Query Time from Minutes to Seconds

SQL optimization often overlooked; complex queries cause performance issues. This article presents a real‑world Oracle SQL case involving three tables (PAT_INPAT_ORDER_INFO, PAT_INPAT_ORDER_COST, DICT_ZL_PRO) and demonstrates how full‑table scans on PAT_INPAT_ORDER_COST dominate the cost.

Using AWR reports and execution plans (Fig 1, Fig 2) the high cost of Id 10 (full scan) is identified. The selectivity of the FY_STATUS column is poor, providing little filtering.

By analyzing column statistics (Fig 3) and selectivity (Fig 4), the article shows that creating an index on the SFXM_ID column of PAT_INPAT_ORDER_COST can turn the query from a 24‑minute operation into a 1‑second execution.

After creating the index, the new execution plan (shown below) confirms the expected cost reduction.

SQL> select sum(cggzl) cggzl, sum(qbgzl) qbgzl
  from (select case
                when zlxm_mc like '%2ê3?3£1??ì2é%' then gzl
                else 0
               end cggzl,
               case
                when zlxm_mc like '%?3±í?÷1ù%' then gzl
                else 0
               end qbgzl
          from dictmanage.dict_zl_pro   b,
               his.pat_inpat_order_info c,
               pat_inpat_order_cost     d
         where d.sfxm_id = b.zlxm_id
           and c.yzjl_id = d.dyzy_yzjl_id
           and zlxm_mc like '%2???%'
           and c.yz_zxrq >= to_date(sysdate)
           and c.yz_zxrq < to_date(sysdate + 1)
           and d.fy_status in ('1', '2')
           and sfxm_je > 0
           and c.yz_zfrq is null
           and c.zylsh = :in_zylsh);
SQL> create index IDX_SFXM_ID on PAT_INPAT_ORDER_COST (SFXM_ID);

Thus, simple index creation is a crucial and effective technique for SQL performance tuning when application changes are not feasible.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

sqlindexingperformance tuningDatabase OptimizationOracleAWR
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.