READ vs SQL SELECT, A Quick Performance Test - Part 2

Pradeep Panga · September 24, 2020 · 2 min read

We have seen the comparison between READ and SQL SELECT (with CURSOR & Fetch one record at a time) and SQL SELECT has run about 30% less time than READ (70% of READ). Click Here to see more details on this.

However, Fetching more number of Records into a Multi Occurrence Data Structure and looping the data through DS should run even faster. We would be using same Table created in Part - 1, Please see Part - 1 using the above link for these details.

We will see a sample program with FETCH multiple rows into DS and compare the results.

Program with SELECT (PPSQL1):

**Free

Dcl-S wSql Char(50) ;

Dcl-Ds wResult Qualified DIM(9999) ;
  wCol1 Char(10) ;
  wCol2 Char(10) ;
  wCol3 Char(10) ;
  wCol4 Char(10) ;
END-DS;

Dcl-S wCol1 Char(10) ;
Dcl-S wCol2 Char(10) ;
Dcl-S wCol3 Char(10) ;
Dcl-S wCol4 Char(10) ;

Dcl-S wCount Packed(7 : 0) Inz;
Dcl-S wTotalRecords Packed(4 : 0) Inz ;
Dcl-S wCurrentRecord Packed(4 : 0) Inz ;
Dcl-S wStartTime Char(8) ;
Dcl-S wEndTime Char(8) ;
Dcl-S wText Char(50) ;

wStartTime = %Char(%Time()) ;
wSql = 'Select * From PTESTTABL Order By TCOLUMN1' ;

Exec Sql Prepare wQuery from :wSql ;

Exec Sql Declare C1 Cursor for wQuery ;

Exec Sql Open C1 ;

Exec Sql Fetch Next From C1 For 9999 rows Into :wResult ;

Dow SqlErrd(3) > 0 ;

  wTotalRecords = SqlErrd(3) ;
  wCurrentRecord = 0 ;
  Dow wCurrentRecord < wTotalRecords ;
    wCount += 1 ;
    wCurrentRecord += 1 ;
  EndDo ;

  If SqlErrd(5) = 100 ;
    Leave ;
  EndIf;

  Exec Sql Fetch Next From C1 For 9999 rows Into :wResult ;
EndDo;

Exec Sql Close C1 ;

wEndTime = %Char(%Time()) ;
wText = 'Start: ' + wStartTime
      + ' End: ' + wEndTime
      + ' Records: ' + %Char(wCount) ;

Dsply wText ;

*InLr = *On ;

Now, This program took just 2 seconds to read one million records, which is much faster than using READ (> 70% Faster) and SELECT with single record for every FETCH (> 60% Faster).

CALL PPSQL1
DSPLY  Start: 10.40.52 End: 10.40.54 Records: 1000000

Please note that, File Size and Data are same as used in the Part - 1.

Keep reading

More from IBM i →