Application Development 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: 

Difference between select single and select upto 1 rows

vinod_vemuru2
Active Contributor
0 Kudos

Hi all,

Can anybody tell me the exact difference between <b>select single</b> and <b>select upto 1 rows.</b> Performance wise which one is better?

Thanks,

Vinod.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi

<b>Knowing when to use SELECT SINGLE or SELECT ... UP TO 1 ROWS</b>

A lot of people use the SELECT SINGLE statement to check for the existence of a value in a database. Other people prefer to use the 'UP TO 1 ROWS' variant of the SELECT statement.

So what's the difference between using 'SELECT SINGLE' statement as against a 'SELECT .... UP TO 1 ROWS' statement ?

If you're considering the statements

SELECT SINGLE field INTO w_field FROM table.

and

SELECT field INTO w_field FROM table UP TO 1 ROWS. ENDSELECT.

then looking at the result, not much apart from the extra ENDSELECT statement. Look at the run time and memory usage and they may be worlds apart.

<b>Why is this ?? The answer is simple.</b>

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.

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.

<b>Get the difference ??</b>

If not, here is a good example, credit for this example goes to Richard Harper, a friend of mine on sapfans.com :

Create a Ztable called ZDifference with 2 fields in it, MANDT of type MANDT and POSNR of type POSNR. Make sure both of these are keys. Also create a table maintenance dialog for it (SE11->Utilities->Table Maintenance Generator). Fill the table with ten rows 000001-000010.

<b>Then run the program shown below:</b>

Code:

  • Program: Z_Difference

  • Purpose: A program that demonstrates the difference

  • between SELECT SINGLE and SELECT UP TO n ROWS.

  • This program requires the data table Z_DIFFERENCE

  • to have been created according to the structure

  • outlined in the text above and populated with

  • at least 10 records.

  • Creation Date: 21/04/2004

  • Requested By:

  • Reference Doc:

  • Author: R Harper

  • Modification History:

  • Date Reason Transport Who

Report Z_Difference

Message-id 38

Line-Size 80

Line-Count 0

No Standard Page Heading.

Start-Of-Selection.

Data: w_Single type Posnr,

t_Rows type standard table of Posnr

initial size 0

with header line.

Select single Posnr

from zDifference

into w_Single.

Select Posnr

into table t_Rows

from zDifference

up to 1 rows

order by Posnr descending.

Write 😕 'Select single:', w_Single.

Skip 1.

Write 😕 'Up to 1 rows :'.

Loop at t_Rows.

Write t_Rows.

EndLoop.

<b>You should see the output:</b>

Select single: 000001

Up to 1 rows : 000010

The first 'SELECT' statement selected the first record in the database according to any selection criterion in the 'WHERE' clause. This is what a 'SELECT SINGLE' does. The second 'SELECT' has asked the database to reverse the order of the records before returning the first row of the result.

In order to be able to do this the database has read the entire table, sort it and then return the first record. If there was no ORDER BY clause then the results would have been identical (ie both '000001') but the second select if given a big enough table to look at would be far slower.

Note that this causes a problem in the Extended Program Check if the full key is not specified in a 'SELECT SINGLE'. Replacing the 'SELECT SINGLE' by an "UP TO 1 ROWS" will give the same exact results without any warning but the program will run slower and consume more memory. This is a good example of a warning that we should ignore... considering you are sure of what you are doing !!

<b>Reward if usefull</b>

11 REPLIES 11

Former Member
0 Kudos

Any where in ABAP. If u r providing full primary key then go for SELECT SINGLE.

ofherwise UP TO 1 ROWS best as performence wise,

Former Member
0 Kudos

If SINGLE is specified, the resulting set has a single line. If the remaining additions to the SELECT command select more than one line from the database, the first line that is found is entered into the resulting set. The data objects specified after INTO may not be internal tables, and the APPENDING addition may not be used. The addition ORDER BY can also not be used.

... UP TO n ROWS

Effect

This addition restricts the number of rows in the result set. A data object of the Type i is expected for n. A positive number in n indicates the maximum number of rows in the result set. If n contains the value 0, all selected rows are passed to the result set. If n contains a negative number, an exception that cannot be handled is raised.

Former Member
0 Kudos

hi

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.

Reward if usefull

Former Member
0 Kudos

Hi

<b>Knowing when to use SELECT SINGLE or SELECT ... UP TO 1 ROWS</b>

A lot of people use the SELECT SINGLE statement to check for the existence of a value in a database. Other people prefer to use the 'UP TO 1 ROWS' variant of the SELECT statement.

So what's the difference between using 'SELECT SINGLE' statement as against a 'SELECT .... UP TO 1 ROWS' statement ?

If you're considering the statements

SELECT SINGLE field INTO w_field FROM table.

and

SELECT field INTO w_field FROM table UP TO 1 ROWS. ENDSELECT.

then looking at the result, not much apart from the extra ENDSELECT statement. Look at the run time and memory usage and they may be worlds apart.

<b>Why is this ?? The answer is simple.</b>

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.

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.

<b>Get the difference ??</b>

If not, here is a good example, credit for this example goes to Richard Harper, a friend of mine on sapfans.com :

Create a Ztable called ZDifference with 2 fields in it, MANDT of type MANDT and POSNR of type POSNR. Make sure both of these are keys. Also create a table maintenance dialog for it (SE11->Utilities->Table Maintenance Generator). Fill the table with ten rows 000001-000010.

<b>Then run the program shown below:</b>

Code:

  • Program: Z_Difference

  • Purpose: A program that demonstrates the difference

  • between SELECT SINGLE and SELECT UP TO n ROWS.

  • This program requires the data table Z_DIFFERENCE

  • to have been created according to the structure

  • outlined in the text above and populated with

  • at least 10 records.

  • Creation Date: 21/04/2004

  • Requested By:

  • Reference Doc:

  • Author: R Harper

  • Modification History:

  • Date Reason Transport Who

Report Z_Difference

Message-id 38

Line-Size 80

Line-Count 0

No Standard Page Heading.

Start-Of-Selection.

Data: w_Single type Posnr,

t_Rows type standard table of Posnr

initial size 0

with header line.

Select single Posnr

from zDifference

into w_Single.

Select Posnr

into table t_Rows

from zDifference

up to 1 rows

order by Posnr descending.

Write 😕 'Select single:', w_Single.

Skip 1.

Write 😕 'Up to 1 rows :'.

Loop at t_Rows.

Write t_Rows.

EndLoop.

<b>You should see the output:</b>

Select single: 000001

Up to 1 rows : 000010

The first 'SELECT' statement selected the first record in the database according to any selection criterion in the 'WHERE' clause. This is what a 'SELECT SINGLE' does. The second 'SELECT' has asked the database to reverse the order of the records before returning the first row of the result.

In order to be able to do this the database has read the entire table, sort it and then return the first record. If there was no ORDER BY clause then the results would have been identical (ie both '000001') but the second select if given a big enough table to look at would be far slower.

Note that this causes a problem in the Extended Program Check if the full key is not specified in a 'SELECT SINGLE'. Replacing the 'SELECT SINGLE' by an "UP TO 1 ROWS" will give the same exact results without any warning but the program will run slower and consume more memory. This is a good example of a warning that we should ignore... considering you are sure of what you are doing !!

<b>Reward if usefull</b>

Former Member
0 Kudos

Select single will fetch 1 record from the database table.

select upto 1 row fetch all the records to buffer from the DB and it will take only one record.

uwe_schieferstein
Active Contributor
0 Kudos

Hello Vinod

If you go to <b>SE80</b>: menu <i>Environment </i>-> <i>Examples </i>-> <i>Performance Examples</i> => Folder <i>SQL Interface</i> -> Report <b>"Test Existence"</b>

you can do a comparison of different SQL statements, e.g.:

1. SELECT ... EXIT.  ENDSELECT  vs. SELECT ... UP TO 1 ROWS

2nd version is about 10 times faster

2. SELECT SINGLE     vs.   SELECT ... UP TO 1 ROWS

Similar response times.

Perhaps in your specific context there may be bigger differences between the different SQL statements. Here you have a means to test this.

Regards

Uwe

Former Member
0 Kudos

Hi,

select single is used when ever we know the primary key to fetch data from a particular table..we use the key field to read the data..and fetch a single record..

if we dont know the primary key, its not advisable to use it.It may not give an error but when u perform existended prog check by going into program tab in menu bar,we get a warning saying there can be more than one record that could be fetched,so we should not use select single in that case..for example we want to fetch matnr from vbrk table,then we can use select single,it gives out one record..

when we donot know the key field then we go for 'select upto' it gives out so many rows u ask for...like select upto one row...it fetches one row..

I think am clear for u..reward if helpful..thank you..

0 Kudos

Thank u all for clarifying.

Points awarded...

Regards,

Vinod.

Former Member
0 Kudos

Hi Fnd,

i will send some information abt ur query.

If u are satisfy with the answer Plz give me REWARD POINTS.

SELECT SINGLE returns the first matching row for the given condition and it may not be unique, if there are more matching rows for the given condition.

SELECT ... UP TO 1 ROWS retrieves all the matching records and applies aggregation and ordering and returns the first record.

Inorder to check for the existence of a record then it is better to use SELECT SINGLE than using SELECT ... UP TO 1 ROWS since it uses low memory and has better performance.

Former Member
0 Kudos

Hi Vinod Vemuru ,

The Major difference between Select Single and Select UPTO 1 rows is The Usage Of Buffer for each.

Select Single will search for all the satisfied data and bring all that data into Buffer and later it will give to that data to the program.

Select UPTO 1 Rows will end the search after getting the 1st satisfied record and gives that record to the program.

Thus Select Single will take much processing time when compare with Select UPTO 1 rows.

cheers!

gyanaraj

****Pls reward points if u find this helpful

former_member194613
Active Contributor
0 Kudos

> 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.

urban legend of the Forum here, but there is no prove for that!

Performancewise there is no difference, use SELECT SINGLE if you want to make clear that you have specified the full primary key and there is only one record which can fulfill the WHERE condition.

Use UP TO n ROWS ( with n =1 as special case) if several records can fulfill the WHERE condition.

Siegfried