> ### Retrieving Data

> To retrieve data efficiently, return only the data that you need, and choose the most efficient method of doing so. The guidelines in this section will help you optimize system performance when retrieving data with ODBC applications.

> #### Retrieving Long Data

> Unless it is necessary, applications should not request long data (SQL_LONGVARCHAR and SQL_LONGVARBINARY data), because retrieving long data across a network is slow and resource-intensive.

> Most users do not want to see long data. If the user does need to see these result items, the application can query the database again, specifying only the long columns in the select list. This method allows the average user to retrieve result sets without having to pay a high performance penalty for network traffic.

> Although the best method is to exclude long data from the select list, some applications do not formulate the select list before sending the query to the ODBC driver (for example, some applications `SELECT * FROM` table_name`...`). If the select list contains long data, the driver must retrieve that data at fetch time, even if the application does not bind the long data in the result set. When possible, use a method that does not retrieve all columns of the table.

> #### Reducing the Size of Retrieved Data

> To reduce network traffic and improve performance, you can reduce the size of data being retrieved to some manageable limit by calling SQLSetStmtOption with the SQL_ATTR_MAX_LENGTH option. This reduces network traffic and improves performance.

> Although eliminating SQL_LONGVARCHAR and SQL_LONGVARBINARY data from the result set is ideal for performance optimization, sometimes, long data must be retrieved. When this is the case, remember that most users do not want to see 100 KB, or more, of text on the screen. What techniques, if any, are available to limit the amount of data retrieved?

> Many application developers mistakenly assume that if they call SQLGetData with a container of size x, the ODBC driver only retrieves x bytes of information from the server. Because SQLGetData can be called multiple times for any one column, most drivers optimize their network use by retrieving long data in large chunks and then returning it to the user when requested.

> For example:

>     char CaseContainer[1000];
>     ...
>     rc = SQLExecDirect (hstmt, "SELECT CaseHistory FROM Cases
>        WHERE CaseNo = 71164", SQL_NTS);
>     ...
>     rc = SQLFetch (hstmt);
>     rc = SQLGetData (hstmt, 1, CaseContainer,(SWORD)
>        sizeof(CaseContainer), ...);

> At this point, it is more likely that an ODBC driver will retrieve 64 KB of information from the server, instead of 1000 bytes. In terms of network access, one 64-KB retrieval is less expensive than 64 retrievals of 1000 bytes. Unfortunately, the application might not call SQLGetData again; therefore, the first and only retrieval of CaseHistory would be slowed by the fact that 64 KB of data must be sent across the network.

> Many ODBC drivers allow you to limit the amount of data retrieved across the network by supporting the SQL_MAX_LENGTH attribute. This attribute allows the driver to communicate to the database server that only x bytes of data are relevant to the client. The server responds by sending only the first x bytes of data for all result columns. This optimization substantially reduces network traffic and improves performance. The previous example returned only one row, but, consider the case where 100 rows are returned in the result set-the performance improvement would be substantial.

> #### Using Bound Columns

> Retrieving data using bound columns (SQLBindCol), instead of SQLGetData, reduces the ODBC call load and improves performance.

> Consider the following example:

>     rc = SQLExecDirect (hstmt, "SELECT <20 columns>
>        FROM Employees WHERE HireDate >= ?", SQL_NTS);
>     do {
>     rc = SQLFetch (hstmt);
>     // call SQLGetData 20 times
>     } while ((rc == SQL_SUCCESS) || (rc==
>     SQL_SUCCESS_WITH_INFO));

> Suppose the query returns 90 result rows. In this case, more than 1890 ODBC calls are made (20 calls to SQLGetData x 90 result rows + 91 calls to SQLFetch).

> Consider the same scenario that uses SQLBindCol, instead of SQLGetData:

>     rc = SQLExecDirect (hstmt, "SELECT <20 columns>
>        FROM Employees WHERE HireDate >= ?", SQL_NTS);
>     // call SQLBindCol 20 times
>     do {
>     rc = SQLFetch (hstmt);
>     } while ((rc == SQL_SUCCESS) || (rc==
>     SQL_SUCCESS_WITH_INFO));

> The number of ODBC calls made is reduced from more than 1890 to about 110 (20 calls to SQLBindCol + 91 calls to SQLFetch). In addition to reducing the call load, many drivers optimize how SQLBindCol is used by binding result information directly from the database server to the user's buffer. That is, instead of the driver retrieving information into a container and copying that information to the user's buffer, the driver simply requests the information from the server be placed directly into the user's buffer.

> #### Using SQLExtendedFetch Instead of SQLFetch

> Use SQLExtendedFetch instead of SQLFetch to retrieve data. The ODBC call load decreases (resulting in better performance) and the code is less complex, resulting in more maintainable code.

> Most ODBC drivers now support SQLExtendedFetch for forward only cursors; yet, most ODBC applications use SQLFetch to retrieve data. Again, consider the previous example using SQLExtendedFetch, instead of SQLFetch:

>     rc = SQLSetStmtOption (hstmt, SQL_ROWSET_SIZE, 100);
>     // use arrays of 100 elements
>     rc = SQLExecDirect (hstmt, "SELECT <20 columns>
>        FROM Employees WHERE HireDate >= ?", SQL_NTS);
>     // call SQLBindCol 1 time specifying row-wise binding
>     do {
>     rc = SQLExtendedFetch (hstmt, SQL_FETCH_NEXT, 0,
>        &RowsFetched, RowStatus);
>     } while ((rc == SQL_SUCCESS) || (rc==
>     SQL_SUCCESS_WITH_INFO));

> Notice the improvement from the previous examples. The initial call load was more than 1890 ODBC calls. By choosing ODBC calls carefully, the number of ODBC calls made by the application has now been reduced to 4 (1 SQLSetStmtOption + 1 SQLExecDirect + 1 SQLBindCol + 1 SQLExtendedFetch). In addition to reducing the call load, many ODBC drivers retrieve data from the server in arrays, further improving performance by reducing network traffic.

> For ODBC drivers that do not support SQLExtendedFetch, the application can enable forward-only cursors using the ODBC cursor library (call SQLSetConnectOption using SQL_ODBC_CURSORS/ SQL_CUR_USE_IF_NEEDED). Although using the cursor library does not improve performance, it should not be detrimental to application response time when using forward-only cursors (no logging is required). Furthermore, using the cursor library when SQLExtendedFetch is not supported natively by the ODBC driver simplifies the code because the application can depend on SQLExtendedFetch being available. The application does not require two algorithms (one using SQLExtendedFetch and another using SQLFetch).

> #### Choosing the Right Data Type

> Advances in processor technology brought significant improvements to the way that operations such as floating-point math are handled; however, retrieving and sending certain data types are still expensive when the active portion of your application will not fit into on-chip cache. When you are working with data on a large scale, it is still important to select the data type that can be processed most efficiently. For example, integer data is processed faster than floating-point data. Floating-point data is defined according to internal database-specific formats, usually in a compressed format. The data must be decompressed and converted into a different format so that it can be processed by the wire protocol.

> Processing time is shortest for character strings, followed by integers, which usually require some conversion or byte ordering. Processing floating-point data and timestamps is at least twice as slow as processing integers.