Optimizing Sage X3 Code: Comparison between FOR and READ instructions

By: | Category: ERP

We are currently creating a new rebate module for a client. This module calculates the best net rebate cost for each line entered during the sales flow cycle from quotes to invoices. Since this is a very resource-intensive process, we decided to compare techniques to access the Sage X3 database tables and views to choose the most optimal ones.

We started by comparing two of the most used 4GL instructions – FOR instruction and READ instruction. We were using FOR when we needed to loop through data extracted from the database and READ when we needed to retrieve one specific value. After our research and testing, we decided not to use READ anymore. Keep reading if you want to know why.

FOR Instruction

Sage X3 technical documentation says FOR can be used to perform loops in two cases: read loops on database tables or joins and loops on index values or on a list of values.

By default FOR retrieves all fields in the table and also does an ORDER BY using the first index. This is inefficient in most cases and it is not a recommended practice when writing SQL queries. Luckily for us, the FOR instruction can be optimized by using the COLUMNS instruction and the RECKEY keyword. COLUMNS instruction allows us to restrict the fields returned by the FOR instruction and RECKEY suppresses the ORDER BY so the records are returned in the physical order they are stored in the database.

This a typical FOR instruction:

For [F:YYRBT] Where REBTXNTYP=3 and DOCNUM=[M:SDH0]SDHNUM
If func YSC25REBATE.FIND_REBATE([F:YYRBT]REBID) = 0 Then
GOK = 0
Endif
Next

We ran SQL traces to see exactly what SQL query is generated by the 4GL instructions. This is the one generated by the 4GL instructions above:

Select YYRBT_.ROWID, YYRBT_.*
From CASHBACK.YREBATE YYRBT_
Where YYRBT_.REBTXNTYP_0 = @P1 And YYRBT_.DOCNUM_0 = @P2 Order by YYRBT_.REBID_0
Option (FAST 1)

This is the optimized FOR and how we’re doing it now:

Columns [YYRBT](REBID)
For [YYRBT]reckey Where REBTXNTYP=3 and DOCNUM=[M:SDH0]SDHNUM
If func YSC25REBATE.FIND_REBATE([F:YYRBT]REBID) = 0 Then
GOK = 0
Endif
Next

This is the SQL query generated by the 4GL instructions above. Notice the SELECT SQL query includes the fields in the COLUMN instruction and also the fields that are part of the WHERE clause (REBTXNTYP and DOCNUM), which is preferable than returning all the fields in the table.

Select YYRBT_.ROWID, YYRBT_.DOCNUM_0, YYRBT_.REBTXNTYP_0, YYRBT_.REBID_0, YYRBT_.UPDTICK_0
From CASHBACK.YREBATE YYRBT_
Where YYRBT_.REBTXNTYP_0 = @P1 And YYRBT_.DOCNUM_0 = @P2

Option (FAST 1)

Much better, right? We still don’t like the FAST 1 hint as it is better to let SQL optimizer decide what is the best execution plan to retrieve records, but nothing we can do as that hint is added automatically by Sage X3 programming language.

READ Instruction

Sage X3 technical documentation says READ can be used to get data from a table or a join based on the value of an index key previously defined or a temporary index.

This instruction by default also retrieves all fields in the table and does an ORDER BY. It can be optimized with the COLUMNS instruction to restrict the fields returned by the SQL query but it doesn’t provide a way to suppress the ORDER BY making it less efficient than FOR instruction which does allow it.

This is a typical READ instruction:

Read [F:YYRBT]YRBT1=PREBID
This is the SQL query generated by the 4GL instruction above.
Select YYRBT_.ROWID, YYRBT_.*
From CASHBACK.YREBATE YYRBT_
Where YYRBT_. REBID_0= @P1
Order by YYRBT_. REBID_0

Option (FAST 1)

This is the optimized READ. Extended keyword must be used when using COLUMNS with Read.

Columns [YYRBT](REBID) Extended
Read [F:YYRBT]YRBT1=PREBID

This is the SQL query generated by the 4GL instruction above. The SQL query still has the ORDER BY which in most cases is time consuming and not necessary.

Select YYRBT_.ROWID, YYRBT_.REBID_0
From CASHBACK.YREBATE YYRBT_
Where YYRBT_. REBID_0= @P1
Order by YYRBT_. REBID_0

Option (FAST 1)

Why bother using READ that always adds the ORDER BY if we can use FOR to achieve the same results in a more efficient way? Here at Net at Work, we write all type of integrations with Sage X3 using 4GL programming language and we pride ourselves in having code efficiencies for the best outcome.