How to utilize index in selection statement

hi
how to utilize index in selection statement and how is it reduces performance whether another alternative is there to reduce performance .
thanks

Hi Suresh,
For each SQL statement, the database optimizer determines the strategy for accessing data records. Access can be with database indexes (index access), or without database indexes (full table scan).The cost-based database optimizer determines the access strategy on the basis of:
*Conditions in the WHERE clause of the SQL statement
*Database indexes of the table(s) affected
*Selectivity of the table fields contained in the database indexes
*Size of the table(s) affected
*The table and index statistics supply information about the selectivity of table fields, the selectivity of combinations of table fields, and table size.     Before a database access is performed, the database optimizer cannot calculate the exact cost of a database access. It uses the information described above to estimate the cost of the database access.The optimization calculation is the amount by which the data blocks to be read (logical read accesses) can be reduced. Data blocks show the level of detail in which data is written to the hard disk or read from the hard disk.
<b>Inroduction to Database Indexes</b>
When you create a database table in the ABAP Dictionary, you must specify the combination of fields that enable an entry within the table to be clearly identified. Position these fields at the top of the table field list, and define them as key fields.
After activating the table, an index is created (for Oracle, Informix, DB2) that consists of all key fields. This index is called a primary index. The primary index is unique by definition. As well as the primary index, you can define one or more secondary indexes for a table in the ABAP Dictionary, and create them on the database. Secondary indexes can be unique or non-unique. Index records and table records are organized in data blocks.
If you dispatch an SQL statement from an ABAP program to the database, the program searches for the data records requested either in the database table itself (full table scan) or by using an index (index unique scan or index range scan). If all fields requested are found in the index using an index scan, the table records do not need to be accessed.
A data block shows the level of detail in which data is written to the hard disk or read from the hard disk. Data blocks may contain multiple data records, but a single data record may be spread across several data blocks.
Data blocks can be index blocks or table blocks. The database organizes the index blocks in the form of a multi-level B* tree. There is a single index block at root level, which contains pointers to the index blocks at branch level. The branch blocks contain either some of the index fields and pointers to index blocks at leaf level, or all index fields and a pointer to the table records organized in table blocks. The index blocks at leaf level contain all index fields and pointers to the table records from the table blocks.
The pointer that identifies one or more table records has a specific name. It is called, for example, ROWID for Oracle databases. The ROWID consists of the number of the database file, the number of the table block, and the row number within the table block.
The index records are stored in the index tree and sorted according to index field. This enables accelerated access using the index. The table records in the table blocks are not sorted.
An index should not consist of too many fields. Having a few very selective fields increases the chance of reusability, and reduces the chance of the database optimizer selecting an unsuitable access path.
<b>Index Unique Scan</b>
If, for all fields in a unique index (primary index or unique secondary index), WHERE conditions are specified with '=' in the WHERE clause, the database optimizer selects the access strategy index unique scan.
For the index unique scan access strategy, the database usually needs to read a maximum of four data blocks (three index blocks and one table block) to access the table record.
<b><i>select * from VVBAK here vbeln = '00123' ......end select.</i></b>
In the SELECT statement shown above, the table VVBAK is accessed. The fields MANDT and VBELN form the primary key, and are specified with '=' in the WHERE clause. The database optimizer therefore selects the index unique scan access strategy, and only needs to read four data blocks to find the table record requested.
<b>Index Range Scan</b>
<b><i>select * from VVBAP here vbeln = '00123' ......end select.</i></b>
In the example above, not all fields in the primary index of the table VVBAP (key fields MANDT, VBELN, POSNR) are specified with '=' in the WHERE clause. The database optimizer checks a range of index records and deduces the table records from these index records. This access strategy is called an index range scan.
To execute the SQL statement, the DBMS first reads a root block (1) and a branch block (2). The branch block contains pointers to two leaf blocks (3 and 4). In order to find the index records that fulfill the criteria in the WHERE clause of the SQL statement, the DBMS searches through these leaf blocks sequentially. The index records found point to the table records within the table blocks (5 and 6).
If index records from different index blocks point to the same table block, this table block must be read more than once. In the example above, an index record from index block 3 and an index record from index block 4 point to table records in table block 5. This table block must therefore be read twice. In total, seven data blocks (four index blocks and three table blocks) are read.
The index search string is determined by the concatenation of the WHERE conditions for the fields contained in the index. To ensure that as few index blocks as possible are checked, the index search string should be specified starting from the left, without placeholders ('_' or %). Because the index is stored and sorted according to the index fields, a connected range of index records can be checked, and fewer index blocks need to be read.
All index blocks and table blocks read during an index range scan are stored in the data buffer at the top of a LRU (least recently used) list. This can lead to many other data blocks being forced out of the data buffer. Consequently, more physical read accesses become necessary when other SQL statements are executed
<b>DB Indexex :Concatenation</b>
     In the concatenation access strategy, one index is reused. Therefore, various index search strings also exist. An index unique scan or an index range scan can be performed for the various index search strings. Duplicate entries in the results set are filtered out when the search results are concatenated.
<i><b>Select * from vvbap where vbeln in ('00123', '00133', '00134').
endselect.</b></i>
In the SQL statement above, a WHERE condition with an IN operation is specified over field VBELN. The fields MANDT and VBELN are shown on the left of the primary index. Various index search strings are created, and an index range scan is performed over the primary index for each index search string. Finally, the result is concatenated.
<b>Full Table Scan</b>
<b><i>select * from vvbap where matnr = '00015'.
endselect</i></b>
If the database optimizer selects the full table scan access strategy, the table is read sequentially. Index blocks do not need to be read.
For a full table scan, the read table blocks are added to the end of an LRU list. Therefore, no data blocks are forced out of the data buffer. As a result, in order to process a full table scan, comparatively little memory space is required within the data buffer.
The full table scan access strategy is very effective if a large part of a table (for example, 5% of all table records) needs to be read. In the example above, a full table scan is more efficient than access using the primary index.
<i><b>In Brief</b></i>
<i>Index unique scan:</i> The index selected is unique (primary index or unique secondary index) and fully specified. One or no table record is returned. This type of access is very effective, because a maximum of four data blocks needs to be read.
<i>Index range scan:</i> The index selected is unique or non-unique. For a non-unique index, this means that not all index fields are specified in the WHERE clause. A range of the index is read and checked. An index range scan may not be as effective as a full table scan. The table records returned can range from none to all.
<i>Full table scan:</i> The whole table is read sequentially. Each table block is read once. Since no index is used, no index blocks are read. The table records returned can range from none to all.
<i>Concatenation:</i> An index is used more than once. Various areas of the index are read and checked. To ensure that the application receives each table record only once, the search results are concatenated to eliminate duplicate entries. The table records returned can range from none to all.
Regards,
Balaji Reddy G
***Rewards if answers are helpful

Similar Messages

  • Want to use Index in Select statement

    Hi,
    I want to use the index tha is created for LIPS table for creation date. I want to use that INdex in the select statement to get the data from that table. Any one can tell me how can I right the select statement in report?
    thanks.

    Like I mentioned earlier, the optimizer is smart enough to choose the correct index based on the WHERE clause.  So if you created an index on field ERDAT for LIPS and your SELECT statement is like so.....
    Select ERDAT WERKS MATNR
              from LIPS
                          into table Itab
                                    Where erdat in s_erdat        "< - ERDAT First in WHERE Clause
                                        and werks in s_werks
                                        and Matnr in s_matnr.
    Then the optimizer will choose your index to use to access the data.  You can see these if you would do an SQL trace over your program and use the "Explain" button on ST05.  It will tell you which index it used and why.
    There is no need to use HINTs to force the use of the index.
    Regards,
    Rich Heilman

  • How to find for which select statement performance is more

    hi gurus
    can anyone suggest me
    if we have 2 select statements than
    how to find for which select statement performance is more
    thanks&regards
    kals.

    hi check this..
    1 .the select statement in which the primary and secondary keys are used will gives the good performance .
    2.if the select statement had select up to  i row is good than the select single..
    go to st05 and check the performance..
    regards,
    venkat

  • How to convert simple SQL Select statements into Stored Procedures?

    Hi,
    How can I convert following SELECT statement into a Stored Procedure?
    SELECT a.empno, b.deptno
    FROM emp a, dept b
    WHERE a.deptno=b.deptno;
    Thanking in advance.
    Wajid

    stored procedure is nothing but a named PL/SQL block
    so you can do it like this see below example
    SQL> create or replace procedure emp_details is
      2  cursor c1 is SELECT a.empno, b.deptno
      3  FROM scott.emp a, scott.dept b
      4  WHERE a.deptno=b.deptno;
      5  begin for c2 in c1
      6  LOOP
      7  dbms_output.put_line('name is '||c2.empno);
      8  dbms_output.put_line('deptno is ' ||c2.deptno);
      9  END LOOP;
    10  END;
    11  /
    Procedure created.and to call it use like below
    SQL> begin
      2  emp_details;
      3  end;
      4  /
    PL/SQL procedure successfully completed.
    SQL> set serveroutput on;
    SQL> /
    empno is 7839
    deptno is 10
    empno is 7698
    deptno is 30
    empno is 7782
    deptno is 10
    empno is 7566
    deptno is 20
    empno is 7654
    deptno is 30
    empno is 7499
    deptno is 30
    empno is 7844
    deptno is 30
    empno is 7900
    deptno is 30
    empno is 7521
    deptno is 30
    empno is 7902
    deptno is 20
    empno is 7369
    deptno is 20
    empno is 7788
    deptno is 20
    empno is 7876
    deptno is 20
    empno is 7934
    deptno is 10Edited by: Qwerty on Sep 17, 2009 8:37 PM

  • How to pass values in select statement as a parameter?

    Hi,
    Very simple query, how do I pass the values that i get in the cursor to a select statement. If table1 values are 1,2,3,4 etc , each time the cursor goes through , I will get one value in the variable - Offer
    So I want to pass that value to the select statement.. how do i do it?
    the one below does not work.
    drop table L1;
    create table L1
    (col1 varchar(300) null) ;
    insert into L1 (col1)
    select filter_name from table1 ;
    SET SERVEROUTPUT ON;
    DECLARE
    offer table1.col1%TYPE;
    factor INTEGER := 0;
    CURSOR c1 IS
    SELECT col1 FROM table1;
    BEGIN
    OPEN c1; -- PL/SQL evaluates factor
    LOOP
    FETCH c1 INTO offer;
    EXIT WHEN c1%NOTFOUND;
    DBMS_OUTPUT.PUT_LINE(offer);
    select * from table1 f where f.filter_name =:offer ;
    factor := factor + 1;
    DBMS_OUTPUT.PUT_LINE(factor);
    END LOOP;
    CLOSE c1;
    END;

    Hi User,
    You are looking somethuing like this, as passing the values to the Cursor as a Paramter.
    DECLARE
       CURSOR CURR (V_DEPT IN NUMBER)    --- Cursor Declaration which accepts the deptno as parameter.
       IS
          SELECT *
            FROM EMP
           WHERE DEPTNO = V_DEPT;    --- The, Input V_DEPT is passed here.
       L_EMP   EMP%ROWTYPE;
    BEGIN
       OPEN CURR (30);       -- Opening the Cursor to Process the Value for Department Number 30 and Processing it with a Loop below.
       DBMS_OUTPUT.PUT_LINE ('Employee Details for Deptno:30');
       LOOP
          FETCH CURR INTO L_EMP;
          EXIT WHEN CURR%NOTFOUND;
          DBMS_OUTPUT.PUT ('EMPNO: ' || L_EMP.EMPNO || ' is ');
          DBMS_OUTPUT.PUT_LINE (L_EMP.ENAME);
       END LOOP;
       CLOSE CURR;
       DBMS_OUTPUT.PUT_LINE ('Employee Details for Deptno:20'); -- Opening the Cursor to Process the Value for Department Number 20
       OPEN CURR (20);
       LOOP
          FETCH CURR INTO L_EMP;
          EXIT WHEN CURR%NOTFOUND;
          DBMS_OUTPUT.PUT ('EMPNO: ' || L_EMP.EMPNO || ' is ');
          DBMS_OUTPUT.PUT_LINE (L_EMP.ENAME);
       END LOOP;
       CLOSE CURR;
    END;Thanks,
    Shankar

  • How build where clause in select statement in FM for Virtual provider

    Hi
    I looking for example of FM for Virtual provider where I find code how assign to select statement "where" clause value from query variable.
    In following code how build t_r_custtype range and how assign value to it.
    CODE********************************
    TYPE-POOLS: abap.
    initialize
      CLEAR: e_t_data, e_t_msg.
    this is specific to infoprovider VIRTPROV
      CHECK i_infoprov = 'VIRTPROV'.
      FIELD-SYMBOLS: <l_s_sbook> TYPE sbook,
                     <l_s_data>  TYPE ANY.
      DATA: l_t_component TYPE abap_compdescr_tab,
            l_t_sbook     TYPE TABLE OF sbook.
    initialize
      CLEAR e_t_data.
    Data selection / only Business Customer
      SELECT * FROM sbook
        INTO CORRESPONDING FIELDS OF TABLE l_t_sbook
        WHERE custtype in t_r_custtype.
    ENDCODE********************************
    Thanks a lot
    Adam

    Hello,
    Would you like fill the ranges in Customer exit for BEx..? 
    If Yes. please refer the attachment for the whole code...
    "Sample code in Customer Exit in BEx"
    IF i_step = 2.
    CASE i_vnam.
    WHEN 'ZDAY_CX'.
    LOOP AT i_t_var_range INTO loc_var_range WHERE vnam = 'ZDAY_IN'.
    CLEAR: l_s_range.
    ZT_DT1 = loc_var_range-low.
    ZT_DT2 = loc_var_range-HIGH.
    CALL FUNCTION 'DATE_CREATE'
    EXPORTING
    ANZAHL_JAHRE = 0
    ANZAHL_KALTAGE = 0
    ANZAHL_MONATE = '-1'
    ANZAHL_TAGE = 0
    DATUM_EIN = ZT_DT1
    DATUM_EIN_ULT = ' '
    ULTIMO_SETZEN = ' '
    IMPORTING
    DATUM_AUS = ZFIDAY .
    E_TT =
    E_ULTKZ =
    CALL FUNCTION 'DATE_CREATE'
    EXPORTING
    ANZAHL_JAHRE = 0
    ANZAHL_KALTAGE = 0
    ANZAHL_MONATE = '-1'
    ANZAHL_TAGE = 0
    DATUM_EIN = ZT_DT2
    DATUM_EIN_ULT = ' '
    ULTIMO_SETZEN = ' '
    IMPORTING
    DATUM_AUS = ZLSDAY.
    E_TT =
    E_ULTKZ =
    l_s_range-low = ZFIDAY .
    l_s_range-high = ZLSDAY .
    l_s_range-sign = 'I'.
    l_s_range-opt = 'EQ'.
    APPEND l_s_range TO e_t_range.
    ENDLOOP.
    *****************************************End*************************************
    **To get the From date (For Text Variable) as per the user input date interval range**
    WHEN 'ZR_S'.
    LOOP AT i_t_var_range INTO loc_var_range WHERE vnam = 'ZDAY_IN'.
    CLEAR: l_s_range.
    ZT_DT1 = loc_var_range-low.
    ZT_DT2 = loc_var_range-HIGH.
    CALL FUNCTION 'DATE_CREATE'
    EXPORTING
    ANZAHL_JAHRE = 0
    ANZAHL_KALTAGE = 0
    ANZAHL_MONATE = 0
    ANZAHL_TAGE = 0
    DATUM_EIN = ZT_DT1
    DATUM_EIN_ULT = ' '
    ULTIMO_SETZEN = ' '
    IMPORTING
    DATUM_AUS = ZFIDAY .
    E_TT =
    E_ULTKZ =
    l_s_range-low0(2) = ZFIDAY6(2).
    l_s_range-low+2(1) = '.'.
    l_s_range-low3(2) = ZFIDAY4(2).
    l_s_range-low+5(1) ='.'.
    l_s_range-low6(4) = ZFIDAY0(4).
    l_s_range-sign = 'I'.
    l_s_range-opt = 'EQ'.
    APPEND l_s_range TO e_t_range.
    ENDLOOP.
    *****************************************End*************************************
    Please let me know if any clarification required..
    Rinku..

  • How is utilization index calculated?

    Hi there,
    When I create a dashboard widget, I can select from top highest or top lowest utilization. I am curious how the utilization is calculated? Is it average over all metrics or is there some sampling?
    Thanks,
    -Nigel

    In other words, if I take a 10 mpx file, a 16 mpx file, a 24 mpx file and a 40 mpx file; at 100% zoom they will all display 1,680 x 1,050 pixels of that file?
    Exactly! Lightroom calls it 1:1 instead of 100% but that is a detail.
    So when you are looking at a 10 MP file at 1:1 you see the equivalent of a crop from a 30x20" print, with 16 MP a crop from a 38*25" print, with 24MP, a crop from a 47x31" print and with a 40MP file, a crop from a 60x40" print.
    I do these calculations more often as you might be able to tell ;-)

  • How to get index of selected image??

    hi all
    I have a set of images that is displayed for the user and when he choose one image click on it and it will do sime action ... these images are stored in an array of images . How can i know the index of selected image ??

    i woul like to append an array of images in the screen and whenever i choose one of this images it link me to the class

  • How to mention index in select query?

    Hi All,
       If I am using fields of two diffrent secondary index in a single select query on a table how I can mention that data should be picked according to first index used in the query?
    Regards
    Deepak

    Please search before asking basic questions.
    Thread locked.
    Rob

  • How to improve Performance for Select statement

    Hi Friends,
    Can  you please help me in improving the performance of the following query:
    SELECT SINGLE MAX( policyterm ) startterm INTO (lv_term, lv_cal_date) FROM zu1cd_policyterm WHERE gpart = gv_part GROUP BY startterm.
    Thanks and Regards,
    Johny

    long lists can not be produced with  a SELECT SINGLE, there is also nothing to group.
    But I guess the SINGLE is a bug
    SELECT MAX( policyterm ) startterm
                  INTO (lv_term, lv_cal_date)
                  FROM zu1cd_policyterm
                  WHERE gpart = gv_part
                  GROUP BY startterm.
    How many records are in zu1cd_policyterm ?
    Is there an index starting with gpart?
    If first answer is 'large' and second 'no'   =>   slow
    What is the meaning of gpart?  How many different values can it assume?
    If many different values then an index makes sense, if you are allowed to create
    an index.
    Otherwise you must be patient.
    Siegfried

  • How do I write a select statement to see if table field is in variable list

    I was wondering if someone knew the best way to write a query to pull back rows where a table field is found in a a variable string.  I'm using Cold Fusion 10 on Microsoft SQL Server.
    The table has a series of fields such as
    carcode          carname
    G                Garage
    C                Carport
    A               Attached
    D               Detached
    And another query pulls back a field that stores as text all the abbreviations that apply
    I want to pull back all the carnames that would match the abbreviations stored in that field.  I thought I could use a contains line but that does not work.  How do I pull back matches against a variable string... I tried an IN statment but that didn't work.
    CFQUERY...
    Select carname from CarTable WHERE CONTAINS (carcode, '#query.carlist#') does not work. 
       select carname from CarTable WHERE CONTAINS(carcode, 'G,A')
    I'm stumped.  The list of abbreviations could be very LOOOONG or short and I just need to translate those abbreviations into the full names by grabbing it from another table.  It isn't just one abbreviation so I can't use LIKE in the one to one sense.
    Thank you so much.

    If you have records with these values,
    G,A
    0
    then your first mistake is that you designed your database poorly.  Storing lists in a single field is essentially stroring unuseable data.  A one to many relationship is better.  If you don't know what that is, I've heard good things about the book, Database Design for Mere Mortals.
    Next, this syntax, #queryname.car_storage#, only returns the first record.  To get all the records, use the valuelist function.
    Next, in sql, if you have a list of strings, each one has to be quoted.  Something like this.
    where myfield in ('a', 'b', 'c')
    The best way to achieve this in ColdFusion is to use the cfqueryparam tag with the list attribute set to yes.

  • How to handle exception in select statement

    eg:select empno into variable_1 from emp where empno=10 (Fails here the value of variable_1 should be 010)
    select empno into variable_1 from emp where empno=102 (sucess)
    in this statement i have to handle no_data_found exception
    can u please suggest
    emp table
    empno ename sal
    102 abc 1000
    103 abcd 10002

    In that case, don't use an implicit cursor, use an explicit cursor and do a fetch.
    If you're going to use an implicit cursor, you will need to cater for no_data_found and (if the statement doesn't guarentee that only one row will be returned) too_many_rows (or you could leave them unhandled so they can raise an error back to the calling proc).
    I would question why you don't want to use an exception block in this case, though.

  • How to find the number of fetched lines from select statement

    Hi Experts,
    Can you tell me how to find the number of fetched lines from select statements..
    and one more thing is can you tell me how to check the written select statement or written statement is correct or not????
    Thanks in advance
    santosh

    Hi,
    Look for the system field SY_TABIX. That will contain the number of records which have been put into an internal table through a select statement.
    For ex:
    data: itab type mara occurs 0 with header line.
    Select * from mara into table itab.
    Write: Sy-tabix.
    This will give you the number of entries that has been selected.
    I am not sure what you mean by the second question. If you can let me know what you need then we might have a solution.
    Hope this helps,
    Sudhi
    Message was edited by:
            Sudhindra Chandrashekar

  • How to use a table name in the select statement using a variable?

    Hi Everybody,
                       I got a internal table which has a field or a variable that gets me some tables names. Now I need to retrieve the data from all these tables in that field dynamically at runtime. So could you suggest me a way out to use the select query which uses this variable as a table ?
    Regards,
    Mallik.

    Hi all,
    Actually i need some more clarification. How to use the same select statement, if i've to use the tabname in the where clause too?
    for ex : select * from (tab_name) where....?
    Can we do inner join on such select statements? If so how?
    Thanks & Regards,
    Mallik.

  • How to use the select statement in for loop

    Hi All,
    my question is can we use select statement in for loop like as follows .
    for key in select key from one_table.
    when i am using this am getting an error like Found select invalid i identifier
    how to make use of select statement in for loop
    please suggest me .
    Thanks
    Sree

    SQL>set serveroutput on;
    SQL> DECLARE
         BEGIN
         FOR Cur_Rec IN (SELECT dname FROM dept) LOOP
          DBMS_OUTPUT.PUT_LINE(Cur_Rec.dname);
         END LOOP;
         END;
    SQL>
    ACCOUNTING
    RESEARCH
    SALES
    OPERATIONSAs per your requirement always filter the Query beforehand
    Like
    FOR Cur_Rec IN (SELECT key FROM <table> WHERE key=1) LOOP
    END LOOP;Edited by: Lokanath Giri on १ दिसंबर, २०११ ३:५६ अपराह्न

Maybe you are looking for