The principal performance concern with CGI database access is in eliminating the overhead of opening the database connection for each instance of the CGI. Opening a database connection can be very time-consuming and may require the loading of large libraries. It is worthwhile to buy or write a connection manager that opens the connection only once and brokers CGI requests to it, rather like FastCGI, but for database connections. RDBMS makers have realized this need for a fast or persistent connection and are now releasing products to fill the gap.
Another performance trick is to use one complex SQL query rather than multiple smaller queries, the results of which must be integrated by the CGI process before presentation to the user. Not only is the time spent in the SQL engine shorter overall, but the CGI does less work. Make the database do the work. See Chapter 17, for more tips.