2007 Jun 14 1:20 PM
Hi
I want to know when is good to do join between 2 tables
or to do select and loop and in the loop select single
When it good to use for all entries
Thanks
have a nice day
2007 Jun 14 1:25 PM
Hi
JOIN is faster to fetch the data from database tables provided they have some connecting fields in both tables
for all entries
The WHERE clause of the SELECT statement has a special variant that allows you to derive conditions from the lines and columns of an internal table:
SELECT ... FOR ALL ENTRIES IN <itab> WHERE <cond> ...
<cond> may be formulated as described above. If you specify a field of the internal table <itab> as an operand in a condition, you address all lines of the internal table. The comparison is then performed for each line of the internal table. For each line, the system selects the lines from the database table that satisfy the condition. The result set of the SELECT statement is the union of the individual selections for each line of the internal table. Duplicate lines are automatically eliminated from the result set. If <itab> is empty, the addition FOR ALL ENTRIES is disregarded, and all entries are read.
The internal table <itab> must have a structured line type, and each field that occurs in the condition <cond> must be compatible with the column of the database with which it is compared. Do not use the operators LIKE, BETWEEN, and IN in comparisons using internal table fields. You may not use the ORDER BY clause in the same SELECT statement.
You can use the option FOR ALL ENTRIES to replace nested select loops by operations on internal tables. This can significantly improve the performance for large sets of selected data.
Select single and Upto 1 rows
Difference Between Select Single and Select UpTo One Rows
According to SAP Performance course the SELECT UP TO 1 ROWS is faster than SELECT SINGLE because you are not using all the primary key fields.
select single is a construct designed to read database records with primary key. In the absence of the primary key, it might end up doing a sequential search, whereas the select up to 1 rows may assume that there is no primary key supplied and will try to find most suitable index.
The best way to find out is through sql trace or runtime analysis.
Use "select up to 1 rows" only if you are sure that all the records returned will have the same value for the field(s) you are interested in. If not, you will be reading only the first record which matches the criteria, but may be the second or the third record has the value you are looking for.
The System test result showed that the variant Single * takes less time than Up to 1 rows as there is an additional level for COUNT STOP KEY for SELECT ENDSELECT UP TO 1 ROWS.
The 'SELECT SINGLE' statement selects the first row in the database that it finds that fulfils the 'WHERE' clause If this results in multiple records then only the first one will be returned and therefore may not be unique.
Mainly: to read data from
The 'SELECT .... UP TO 1 ROWS' statement is subtly different. The database selects all of the relevant records that are defined by the WHERE clause, applies any aggregate, ordering or grouping functions to them and then returns the first record of the result set.
Mainly: to check if entries exist.
Reward points for useful Answers
Regards
Anji
2007 Jun 14 1:22 PM
HI,
Its good to use JOIN than having one SELECT and then LOOPING at the result table and executing SELECT SINGLE in the loop.
You should never have SELECT inside a LOOP.
Having SELECT inside a loop will hinder the performance of the program drastically.
Regards,
Sesh
.
2007 Jun 14 1:25 PM
Hi
JOIN is faster to fetch the data from database tables provided they have some connecting fields in both tables
for all entries
The WHERE clause of the SELECT statement has a special variant that allows you to derive conditions from the lines and columns of an internal table:
SELECT ... FOR ALL ENTRIES IN <itab> WHERE <cond> ...
<cond> may be formulated as described above. If you specify a field of the internal table <itab> as an operand in a condition, you address all lines of the internal table. The comparison is then performed for each line of the internal table. For each line, the system selects the lines from the database table that satisfy the condition. The result set of the SELECT statement is the union of the individual selections for each line of the internal table. Duplicate lines are automatically eliminated from the result set. If <itab> is empty, the addition FOR ALL ENTRIES is disregarded, and all entries are read.
The internal table <itab> must have a structured line type, and each field that occurs in the condition <cond> must be compatible with the column of the database with which it is compared. Do not use the operators LIKE, BETWEEN, and IN in comparisons using internal table fields. You may not use the ORDER BY clause in the same SELECT statement.
You can use the option FOR ALL ENTRIES to replace nested select loops by operations on internal tables. This can significantly improve the performance for large sets of selected data.
Select single and Upto 1 rows
Difference Between Select Single and Select UpTo One Rows
According to SAP Performance course the SELECT UP TO 1 ROWS is faster than SELECT SINGLE because you are not using all the primary key fields.
select single is a construct designed to read database records with primary key. In the absence of the primary key, it might end up doing a sequential search, whereas the select up to 1 rows may assume that there is no primary key supplied and will try to find most suitable index.
The best way to find out is through sql trace or runtime analysis.
Use "select up to 1 rows" only if you are sure that all the records returned will have the same value for the field(s) you are interested in. If not, you will be reading only the first record which matches the criteria, but may be the second or the third record has the value you are looking for.
The System test result showed that the variant Single * takes less time than Up to 1 rows as there is an additional level for COUNT STOP KEY for SELECT ENDSELECT UP TO 1 ROWS.
The 'SELECT SINGLE' statement selects the first row in the database that it finds that fulfils the 'WHERE' clause If this results in multiple records then only the first one will be returned and therefore may not be unique.
Mainly: to read data from
The 'SELECT .... UP TO 1 ROWS' statement is subtly different. The database selects all of the relevant records that are defined by the WHERE clause, applies any aggregate, ordering or grouping functions to them and then returns the first record of the result set.
Mainly: to check if entries exist.
Reward points for useful Answers
Regards
Anji
2007 Jun 14 1:25 PM
hi
using joins can cause performance issues.
even select * should be avoided as much as possible as it causes performance degradation.
to avoid loop inside loop <b>it is advisable to use for all entries'</b>
select single should always be the first option to use.
hi
regards
ravish
<i><b>plz dont forget to reward points if helpful</b></i>
2007 Jun 14 1:25 PM
hi,
select - join statement is always better than using a select statement in a loop.
avoid using select single in a loop.
for all entries is also prefered. if u have to join more than 2 tables then i suggest you to use for all entries.
reward points if helpful
regards
fazeel
2007 Jun 14 1:26 PM
If you use the select st inside the looop ... It will effect the performance..
Better u can use the For all enteries...