如何进行快速查询

时间:2019-03-07 10:08:28

标签: sql oracle query-performance

我在创建小表时测试了此编码SQL,该表具有4行数据,并且得到了快速查询,但是当我深层更改为true表并且不给我任何结果时,该真值表有超过100万条记录

SELECT DISTINCT /*+ richs_secret_hint */
 em_code, (to_date(end_date,'DD-MM-YYYY') - level + 1) AS days
FROM
   islaam_vca where em_code = '2222'

CONNECT BY level <= (to_date(end_date,'DD-MM-YYYY') - to_date(start_date,'DD-MM-YYYY') + 1)
order by days ASC
;

给我快速的结果

任何人都可以帮助我如何从真实表中获得快速结果

1 个答案:

答案 0 :(得分:0)

  1. 在我看来,em_code和start_date的组合对于该表而言是唯一的,因此应该存在用于该唯一性的索引:
CREATE UNIQUE INDEX islaam_vca_uk1 ON islaam_vca(em_code, start_date)
  1. 第二,请将日期存储为DATE数据类型,而不是字符串。

  2. 进行了一些测试,可能会出现这种情况,当查询将为表islaam_vca尝试TABLE ACCESS FULL时,您可以尝试这样做:

SELECT /*+ALL_ROWS*/  distinct em_code --sorry about my interpretation of secret your hint
     , start_date
     , NVL(end_date, SYSDATE) end_date --don't know if end date is nullable or now
     , TRUNC(start_date) + level - 1 specific_date
  FROM (SELECT em_code
            , start_date
            , end_date
         FROM islaam_vca
        WHERE em_code = '2222')
CONNECT BY level <= TRUNC(NVL(end_date, SYSDATE)) - TRUNC(start_date) + 1
order by specific_date;