Best Practices - Avoid Unnecessary "Order By"

The purpose of this document is to provide best practices to access databases with large datasets when developing in X3 syntax.

Description

When using a For [ABV] loop on a table, the X3 engine sends a Select ... statement to the database, with an Order By clause by default. In most cases, this clause is critical to ensure the consistency of the expected results. Below are examples of situations where the Order By clause is useful:

However, the Order By clause can cause serious performance issues if a Select ... statement is performed on a large dataset (in the case it is not useful).

Rules used by the engine

By default, the X3 engine always sends an Order By clause. It generates the SQL sentences based on the various X3 instructions.

Example:For [table] does the following:

Workaround

Testing results

As a test, a search based on a post code value that matches approximately 40 lines was performed on POSCOD table (1,200,00 records). The script always starts with:

>> Local File POSCOD [POS]>> Filter [POS] Where POSCOD="..."

The following lines depend on the results expected:

SyntaxScript written to get the resultRelative timeRelative time if an index exists
1
For [POS]
 FOUND=1
 Break
Next
900
(930 if no break is done)
15
(no break done)
2
For [POS]reckey
 FOUND=1
 Break
Next
15
(900 if no break is done)
15
(no break done)
3
FOUND=nbrecord([POS])
5003
4
Look [POS]reckey First
FOUND=(fstat=0)
53
5
Read [POS]reckey First
FOUND=(fstat=0)
53

If this script is frequently used and no standard index exists, consider creating an additional index, or at least an optimization index for the customers that are frequently in this situation.

See also

For, Reckey, Order By, Filter.