Search code examples
sqloracle-databasequery-performance

how to make fast query


i tested this coding SQL on create small table it have 4 row data and i get fast query but when i change to true table deep time and don't give me any result, this truth table it have more than 1 million record

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
;

give me fast result

any one help me how can get fast result from true table


Solution

    1. To me it seems that combination of em_code and start_date is a unique for this table, so there should exists index for this uniqueness:
    CREATE UNIQUE INDEX islaam_vca_uk1 ON islaam_vca(em_code, start_date)
    
    1. Second, please store dates as DATE datatype, not as string.

    2. Tested it a bit and there could be situation, when query will try TABLE ACCESS FULL for table islaam_vca, so you could try to do:

    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;