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

Fields Orders in Select ..

former_member425121
Participant
0 Likes
1,126

Hi

Can somebody helpme in this doubt. I know is good to put the fields in the WHERE clausule at same order that fields appears in the table.

But, i want to confirm, this not applies if there is an Index with other fields sequence ? i mean if in the table FieldX is first than FieldY (these are not in the primary Key) , but there is an index wich has first FieldY than Fieldx; then i must to use in clausule WHERE the same field sequence that the Index (FieldY, FieldX).

Regards

Frank

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
750

In your specific case where primary-key fields are not in the where-clause, and index-fields are available, i feel it would not matter what the order of the fields is.

The SAP runtime environment will optimize this anyway, and choose the best available index based on your where-clause fields.

This is the reason why, for a table of fields F1, F2, F3, F4, where F1 and F2 are the primary key, and F3 and F4 are a secondary index, and your where-clause has F3, F4 and then F1, the primary-key will still get used.

In a case where F3 has an index and F4 also has an index, then if your where clause has F3 and F4, neither of the indexes will get used, due to ambiguity while trying to resolve indexes.

A more useful recommendation would be to order the fields in the correct sequence in the select-clause, ie., the fields being fetched should be in the correct order.

4 REPLIES 4
Read only

Former Member
0 Likes
751

In your specific case where primary-key fields are not in the where-clause, and index-fields are available, i feel it would not matter what the order of the fields is.

The SAP runtime environment will optimize this anyway, and choose the best available index based on your where-clause fields.

This is the reason why, for a table of fields F1, F2, F3, F4, where F1 and F2 are the primary key, and F3 and F4 are a secondary index, and your where-clause has F3, F4 and then F1, the primary-key will still get used.

In a case where F3 has an index and F4 also has an index, then if your where clause has F3 and F4, neither of the indexes will get used, due to ambiguity while trying to resolve indexes.

A more useful recommendation would be to order the fields in the correct sequence in the select-clause, ie., the fields being fetched should be in the correct order.

Read only

Former Member
0 Likes
750

I find it good to put fields in key field order (primary or secondary) to make the program more readable. It will also point out to you (or the next programmer to look at it) if you are missing a key field.

Rob

Read only

former_member183804
Active Contributor
0 Likes
750

Hello Frank,

as Rob already told keeping a certain order will improve readability of source code. Which index is chosen at runtime is left to the optimizer of the DBMS.

Regards,

Klaus

Read only

0 Likes
750

Good point Klaus. What I meant was the key you are <i>trying</i> to use. The EXPLAIN function of ST05 will show which key is actually used. And you may want to check that to make sure that the SELECT is working the way you expect.

Rob