Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Simple Index question ...

Former Member
0 Likes
782

Hello,

I've several index's created on a specific table. Does the order of the fields in the "WHERE" statement important for the right index to be chosen ?

For example:

============

Index fields: MANDT, BANFN (in this order)

statement:

SELECT * FROM EBAN WHERE BANFN = ... AND MANDT = ...

will this statement choose the existing index ?

Please advice,

Dimitry Haritonov

1 ACCEPTED SOLUTION

Hello,

I've several index's created on a specific table. Does the order of the fields in the "WHERE" statement important for the right index to be chosen ?

For example:

============

Index fields: MANDT, BANFN (in this order)

statement:

SELECT * FROM EBAN WHERE BANFN = ... AND MANDT = ...

will this statement choose the existing index ?

Please advice,

Dimitry Haritonov

6 REPLIES 6
Read only

Former Member
0 Likes
735

Yes, the order of the fields in the "WHERE" statement is important for the right index to be chosen.



The database system sometimes does not use a suitable index for a selection, even if there is one. The index used depends on the optimizer used for the database system. You should therefore check if the index you created is also used for the selection (see How to Check if an Index is Used).).

Creating an additional index could also have side effects on the performance. This is because an index that was used successfully for selection might not be used any longer by the optimizer if the optimizer estimates (sometimes incorrectly) that the newly created index is more selective.

The indexes on a table should therefore be as disjunct as possible, that is they should contain as few fields in common as possible. If two indexes on a table have a large number of common fields, this could make it more difficult for the optimizer to choose the most selective index.

How to Check if an Index is Used

Procedure

Open a second session and choose System ® Utilities ® Performance trace.

The Trace Requests screen appears.

Select Trace on.

The SQL trace is activated for your user, that is all the database operations under your user are recorded.

In the first window, perform the action in which the index should be used.

If your database system uses a cost-based optimizer, you should perform this action with as representative data as possible. A cost-based optimizer tries to determine the best index based on the statistics.

In the second session, choose Trace off and then Trace list.

Result

The format of the generated output depends on the database system used. You can determine the index that the database used for your action with the EXPLAIN function for the critical statements (PREPARE, OPEN, REPOPEN).

Best Regards,

Vibha

*Please mark all the helpful answers

Read only

0 Likes
735

Thanks a lot !

Vibha: Sorry but anver was first.

Read only

Former Member
0 Likes
735

Its ok :-)I am glad to help you out for query.

Read only

0 Likes
735

One more question:

==================

If the index includes 4 fields, and I include 3 fields in the right order (the 1, 2, 4 fields without the third field). Will the optimizer use the index ?

Please advice,

Dimitry

Read only

Former Member
0 Likes
735

An index is defined on fields FIELD1, FIELD2, FIELD3 and FIELD4 of table BSPTAB in this order. This table is accessed with the SELECT statement:

SELECT * FROM BSPTAB WHERE FIELD1 = X1 AND FIELD2 = X2 AND FIELD4= X4.

Since FIELD3 is not specified more exactly, only the index sorting up to FIELD2 is of any use. If the database system accesses the data using this index, it will quickly find all the records for which FIELD1 = X1 and FIELD2 = X2. You then have to select all the records for which FIELD4 = X4 from this set.

This is what is given by sap help. So i think in that case that index will not not be of much help.

I hope this answers ur question.

Best Regards,

Vibha

*Please mark all the helpful answers