Writing queries for performance
- Last Updated: January 17, 2024
- 2 minute read
- OpenEdge
- Version 12.8
- Documentation
Writing queries for performance
This section provides a collection of tips and guidelines
to follow when writing queries. For example, a query that processes
a large number of rows performs best if it uses NO-LOCK,
lookahead cursors, a large cache size, and a small field list.
These tips and guidelines might improve the performance of your
DataServer applications. To assess the usefulness of a particular
suggestion, apply it, then use the DEBUG diagnostic
options to gather statistics on how your application runs:
- Use
FOR EACH,GET, andOPEN QUERYstatements rather thanFINDstatements, which generally perform more slowly. Consider using theFOR FIRSTstatement instead ofFINDFIRST.The only exception is that
FIND LASTis faster thanGET LAST. This is becauseGET LASTcauses the client to process all of the records; theFIND LASTstatement allows the server to retrieve the last record. - Use field lists.
- Use the
QUERY-TUNINGoptions. - Use lookahead cursors.
- Use
NO-LOCKwhere possible. - Avoid specifying lock upgrades. Instead, allow the DataServer and the MS SQL Server data source to handle lock upgrades.
- Do not ask for a particular ordering of results with
USE-INDEXorBYclauses unless your application requires it. Instead, allow the DataServer and the MS SQL Server data source to determine the most efficient index (if any) for processing a query and avoid the overhead of sorting results. - If you use a
BYclause that will sort a large amount of data, make sure a corresponding index exists in your data source to make sorting efficient. In some cases it may also be desirable to have indexes over columns used inWHEREclause selection criteria. - For aggregates, use either the
RUN-STORED-PROCEDURE send-sql-statementsyntax or an OpenEdge SQL statement. If you use an OpenEdge SQL statement with a cursor, declare the cursor read-only. - When you test for the existence of a record, use the
CAN-FIND FIRSTfunction, which does not retrieve the record if the DataServer passes the entireWHEREclause to the MS SQL Server data source for processing. However, avoid nestingCAN-FINDfunctions. - Avoid using the
RECIDfunction. Instead, use theROWIDfunction. - Avoid queries that include server LOB types in their results if the LOB data is not pertinent with the needs of your application. LOB types, and their processing overhead, can be omitted from your query results using field lists.