Hi, I provide training in SAP UI5. Also feel free to checkout my new web page https://saptechblog.com
ABAP

Index in SAP ABAP

INDEX

 

Purpose: Index makes the data base search faster.

 

How it works:

 

Index can be considered as a copy of the database reduced to certain fields.  The copy generated at the time of creation of index is always in sorted form which makes the search faster. This index table also contains a field which contains the pointer to the actual record in the table.

 

Types of Index:

  1. Primary Index.
  2. Secondary Index.

 

 

Primary Index:

  1. Primary index contains all the key fields of the table and a pointer to the non key fields.
  2. It is automatically created at the time of creation of table.

 

Let us take an example table scounter.

 

The structure of the table is as shown below.

 

clip_image002[4]

 

 

The primary index created at the time of creation of table will contains all primary key and a pointer to the non key fields. The primary index table will contains field like mandt carrid countnum and pointer in a sorted order.

 

clip_image004[4]

 

The above example demonstrates how the primary index table will look like and the pointer refers to position where the same record in the sorted primary table is available in the actual database table which you can see with the highlighted Red line.

 

Secondary Index:

We can also add our own index to the table known as secondary index. We create secondary index for the non key fields. The secondary indexes are only to be created when these fields are used extensively in search.

 

In the above mentioned table consider a scenario where I wanted to fetch the record from the database based on the airport code which is actually a non-key field.  Sorting of the primary keys will not be of any use as the airport code will still be scattered. A secondary index for the field airport code needs to be created to support the faster access.

 

 

Creating a secondary index:

 

Step 1: Go to the Tcode SE11. Enter the table name and choose display mode.

 

clip_image006[4]

 

 

Step 2:

 

Click on the indexes button.

 

clip_image008[4]

 

 

 

 

 

Step 3 :

 

Click on the create index button.

 

clip_image010[4]

 

 

Step 4:

 

Enter the name for the index in the popup and press enter.

 

clip_image012[4]

 

 

 

 

Step 5:

 

Enter the short description and the field name in the displayed screen,

 

clip_image014[4]

 

Save and activate.

 

The secondary index for the field airport is now created.

 

The structure of the secondary index which we created now will contain mandt airport and  pointer column which will be in the sorted form as shown below.

 

clip_image016[4] 

Now when I select the records using the airport code, then the system will identify the list of records for the airport code in the index table and fetch the corresponding records using the pointer which you can see through the highlighted color red for the airport search LCY.

 

There are two types of secondary indexes.

 

  1. Non-Unique Index
  2. Unique Index

 

Which you can choose through option

 

clip_image018[4]

 

 

Unique Index:

 

An entry in an index can refer to several records that have the same values for the index fields. A unique index does not permit these multiple entries. The index fields of a unique index thus have key function that is they already uniquely identify each record of the table.

 

The primary index of a table is always a unique index since the index fields form the key of the table, uniquely identifying each data record.

 

Suppose if I create the index as a Unique index for the field airport then the system will not allow the duplication of the record similar to LCY highlighted in the red in the secondary index table.

 

The use of unique index does not influence the speed it simply means that certain combinations of the fields in the table are unique.

 

Note: A unique index for client dependent table must contain the field mandt.

 

 

The optimizer of the database decides whether the index has to be used for search so index may only result in gaining the performance.

 

 

 

 

 

 

 

Key Points when creating the secondary index.

 

* The first field in the index must be the one which have constant value for the large number of selections.

 

Suppose if I have four fields in the index field1 field2 field3 and field4 for the table “TABLE” then field1 has more constant values than other fields.

 

My index table structure will be like field1, field2, field3 field4 and pointer in a sorted form by the four fields and I specify a select query like.

 

Select * from table TABLE into table IT_TABLE where field1 = condition

                                                                                      and Field2 = condition

                                                                                    and field4 = condition.

 

Since the third field of the index has not been mentioned, sorting the index table only to two fields will be of use. 

 

The system will select the set of records for the field1 and field2 quickly and then finds the record for the field4 from the selected set.

 

 

* Only those fields that significantly restrict the set of results in a selection are rational for an index.

Example:

Suppose if I create an index for the address table ADRTAB with field’s tile, first name and last name and if I write my select query like.

Select * from adrtab into table it_adrtab where title = “Mr” and first name = “X” and last name = “Y”.

Since there won’t be many peoples with the same name and same title, it is not sensible to create an index in a combination like this where as title can be considered as there will be may people with the same title.

 

 

 

About Arun

1 Comments:

Powered by Blogger.