Solved

Need hint help in existing sql

Posted on 2014-03-25
3
453 Views
Last Modified: 2014-03-25
Hello Experts,

I have the below SQL :

EXPLAIN PLAN FOR 
SELECT  QST_RESPONSE ,
  QST_RESPONSE_ATTACHMENT ,
  QST_RESPONSE_DATE ,
  QST_ALL_AREAS ,
  QST_APPLE_DEDICATED_AREAS ,
  QST_NON_PRODUCTION_UTILITIES ,
  QST_PRODUCTION_AREAS_DEDICATED ,
  QST_COMMON_PRODUCTION_USED
FROM MS_SRA_VISIT_QUESTIONS
WHERE QUESTION_ID                          = :B2
AND REGEXP_SUBSTR(VISIT_ID ,'[^-]+' ,1 ,2) = :B1
AND QST_TYPE                              <> '1'
AND DD_CREATED_ON                          =
  (SELECT /*+ index_ss(QS MS_SRA_VISIT_QUESTIONS_PK) */ MAX(qs.DD_CREATED_ON)
  FROM MS_SRA_VISIT_QUESTIONS QS
  WHERE QS.QUESTION_ID                          = :B2
  AND REGEXP_SUBSTR(QS.VISIT_ID ,'[^-]+' ,1 ,2) = :B1
  AND qs.QST_TYPE                              <> '1'
  ) ;

Open in new window



Plan hash value: 283024080
 
-----------------------------------------------------------------------------------------------------------
| Id  | Operation                     | Name                      | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT              |                           |     1 |  1290 |    80   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS BY INDEX ROWID  | MS_SRA_VISIT_QUESTIONS    |     1 |  1290 |    40   (0)| 00:00:01 |
|*  2 |   INDEX SKIP SCAN             | MS_SRA_VISIT_QUESTIONS_PK |     1 |       |    40   (0)| 00:00:01 |
|   3 |   SORT AGGREGATE              |                           |     1 |    32 |            |          |
|*  4 |    TABLE ACCESS BY INDEX ROWID| MS_SRA_VISIT_QUESTIONS    |     1 |    32 |    40   (0)| 00:00:01 |
|*  5 |     INDEX SKIP SCAN           | MS_SRA_VISIT_QUESTIONS_PK |     1 |       |    40   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------------------
 
Outline Data
-------------
 
  /*+
      BEGIN_OUTLINE_DATA
      INDEX_SS(@"SEL$2" "QS"@"SEL$2" ("MS_SRA_VISIT_QUESTIONS"."VISIT_ID" 
              "MS_SRA_VISIT_QUESTIONS"."QUESTION_ID" "MS_SRA_VISIT_QUESTIONS"."QST_SOURCE_OBJECT_TYPE"))
      PUSH_SUBQ(@"SEL$2")
      INDEX_SS(@"SEL$1" "MS_SRA_VISIT_QUESTIONS"@"SEL$1" ("MS_SRA_VISIT_QUESTIONS"."VISIT_ID" 
              "MS_SRA_VISIT_QUESTIONS"."QUESTION_ID" "MS_SRA_VISIT_QUESTIONS"."QST_SOURCE_OBJECT_TYPE"))
      OUTLINE_LEAF(@"SEL$1")
      OUTLINE_LEAF(@"SEL$2")
      ALL_ROWS
      OPT_PARAM('optimizer_index_cost_adj' 40)
      OPT_PARAM('star_transformation_enabled' 'true')
      OPT_PARAM('_gby_hash_aggregation_enabled' 'false')
      OPT_PARAM('_optimizer_cost_based_transformation' 'off')
      OPT_PARAM('_optim_peek_user_binds' 'false')
      OPT_PARAM('_index_join_enabled' 'false')
      OPT_PARAM('query_rewrite_enabled' 'false')
      OPT_PARAM('_complex_view_merging' 'false')
      OPT_PARAM('_b_tree_bitmap_plans' 'false')
      DB_VERSION('11.2.0.3')
      OPTIMIZER_FEATURES_ENABLE('11.2.0.3')
      IGNORE_OPTIM_EMBEDDED_HINTS
      END_OUTLINE_DATA
  */
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   1 - filter("QST_TYPE"<>'1' AND "DD_CREATED_ON"= (SELECT /*+ INDEX_SS ("QS" 
              "MS_SRA_VISIT_QUESTIONS_PK") */ MAX("QS"."DD_CREATED_ON") FROM "MS_SRA_VISIT_QUESTIONS" "QS" WHERE 
              "QS"."QST_TYPE"<>'1' AND "QS"."QUESTION_ID"=TO_NUMBER(:B2) AND  REGEXP_SUBSTR 
              ("QS"."VISIT_ID",'[^-]+',1,2)=:B1))
   2 - access("QUESTION_ID"=TO_NUMBER(:B2))
       filter("QUESTION_ID"=TO_NUMBER(:B2) AND  REGEXP_SUBSTR ("VISIT_ID",'[^-]+',1,2)=:B1)
   4 - filter("QS"."QST_TYPE"<>'1')
   5 - access("QS"."QUESTION_ID"=TO_NUMBER(:B2))
       filter("QS"."QUESTION_ID"=TO_NUMBER(:B2) AND  REGEXP_SUBSTR 
              ("QS"."VISIT_ID",'[^-]+',1,2)=:B1)

Open in new window



Want to change in such a way that the indexes should not be skiped and also the way IN condition are been used.
0
Comment
Question by:Swadhin Ray
3 Comments
 
LVL 34

Expert Comment

by:johnsone
ID: 39953067
The index is not being skipped.  The index is still being used.  The index being used is a composite index (it has more than one column).  The first column in the index (VISIT_ID) cannot be used because of the REGEXPR_SUBSTR call on it.  However, QUESTION_ID (the second column in the index) can be used.  Oracle is smart enough now to be able to used that index to search for QUESTION_ID.

If you want an index range scan instead of an index skip scan, then create an index on QUESTION_ID.  If you do that, I would probably add DD_CREATED_ON as a second column in that index.  You may already have that indexed, but with the hint you have, you are forcing an index to be used.

You may also consider a function based index on the REGEXP_SUBSTR.  Not sure if you would get good cardinality there or not.
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 39953230
rather than querying the table twice, have you considered using an analytic?


SELECT qst_response,
       qst_response_attachment,
       qst_response_date,
       qst_all_areas,
       qst_apple_dedicated_areas,
       qst_non_production_utilities,
       qst_production_areas_dedicated,
       qst_common_production_used
  FROM (SELECT qst_response,
               qst_response_attachment,
               qst_response_date,
               qst_all_areas,
               qst_apple_dedicated_areas,
               qst_non_production_utilities,
               qst_production_areas_dedicated,
               qst_common_production_used,
               RANK() OVER (ORDER BY dd_created_on DESC) r
          FROM ms_sra_visit_questions
         WHERE question_id = :b2
           AND REGEXP_SUBSTR(
                   visit_id,
                   '[^-]+',
                   1,
                   2
               ) = :b1
           AND qst_type <> '1')
 WHERE r = 1;


if the dd_created_on will be unique then you could use row_number() instead of rank()
0
 
LVL 16

Author Closing Comment

by:Swadhin Ray
ID: 39955098
thanks a lot
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question