skip to main content
Designing and coding the IP : SELECT processing : Optimized GROUP BY, ORDER BY and DISTINCT processing
 

Optimized GROUP BY, ORDER BY and DISTINCT processing

The IP can optionally process the GROUP BY and ORDER BY clauses of the result set to take advantage of the indexing that its database provides.
To check if the GROUP BY and ORDER BY clauses in the current query can be processed by the IP, the IP can call: dam_getInfo(DAM_INFO_GROUP_BY_OPTIMIZABLE), dam_getInfo(DAM_INFO_ORDER_BY_OPTIMIZABLE)
The OpenAccess SDK SQL engine allows the IP to process the GROUP BY and ORDER BY clauses if the columns in these clauses refer to result columns that are not computed columns. For example, the IP is not allowed to process ORDER BY in the following query:
SELECT empno, salary+commission AS total_pay FROM emp ORDER BY total_pay
The dam_getInfo function is used to determine the number of columns in the GROUP BY and ORDER BY clauses. To get details about the columns, use the number returned by dam_getInfo and call dam_getGroupByCol and dam_getOrderByCol.
If the IP returns the result set in the correct order, then to inform the OpenAccess SDK SQL engine to skip processing of the result set for the GROUP BY and ORDER BY, respectively, call:
dam_setOption(DAM_STMT_OPTION, DAM_STMT_OPTION_GROUP_BY)
or
dam_setOption(DAM_STMT_OPTION, DAM_STMT_OPTION_ORDER_BY)
The IP can optimize the DISTINCT processing by using dam_getInfo(DAM_INFO_DISTINCT_OPTIMIZABLE) to check whether the query has a SELECT DISTINCT clause that can be optimized. If the DISTINCT in the query can be optimized, then the IP uses dam_getFirstCol(DAM_COL_IN_RESULT) and dam_getNextCol to determine the result columns for which the result rows should have unique values. If the IP is going to return the result set with the distinct processing completed, then, to inform the OpenAccess SDK SQL engine to skip processing for the DISTINCT clause, the IP should call:
dam_setOption(DAM_STMT_OPTION, DAM_STMT_OPTION_DISTINCT)