User Guide

Table Of Contents
Optimizing ColdFusion applications 303
Optimizing database use
Poor database design and incorrect or inefficient use of the database are among the most common
causes of inefficient applications. Consider the different methods that are available for using
databases and information from databases when you design your application. For example, if you
need to average the price of a number of products from an SQL query, it is more efficient to use
SQL to get the average than to use a loop in ColdFusion.
Two important ColdFusion MX tools for optimizing your use of databases are the
cfstoredproc
tag and the
cfquery tag cachedWithin attribute.
Using stored procedures
The
cfstoredproc tag lets ColdFusion MX use stored procedures in your database management
system. A stored procedure is a sequence of SQL statements that is assigned a name, compiled,
and stored in the database system. Stored procedures can encapsulate programming logic in SQL
statements, and database systems are optimized to execute stored procedures efficiently. As a
result, stored procedures are faster than
cfquery tags.
You use the
cfprocparam tag to send parameters to the stored procedure, and the cfprocresult
tag to get the record sets that the stored procedure returns.
<h2>Check out the following specials</
h2>
<table>
<cfoutput query="specialQuery">
<cfset salePrice = BasePrice * .8>
<tr>
<td>#ItemNAme#</td>
<td>#Item_Link#</td>
<td>#Description#</td>
<td>#salePrice#</td>
</tr>
</cfoutput>
</table>
Displays the sale items in a table. Inside a cfoutput
tag, calculates each item’s sale price and displays the
item information in a table row.
Because this code is inside a
cfsavecontent tag,
ColdFusion does not display the results of the
cfoutput tag. Instead, it saves the formatted output as
HTML and text in the ProductCache variable.
</cfsavecontent>
Ends the cfsavecontent tag block.
<cflock scope="Application"
type="Exclusive"
timeout=30>
<cfset Application.productCache =
productcache>
</cflock>
Inside an Exclusive cflock tag, saves the contents of
the local variable
ProductCache in the Application
scope variable Application.productCache.
</cfif>
Ends the code that executes only if the
Application.productCache variable does not exist.
<cflock scope="application" timeout="20"
type="readonly">
<cfoutput>#Application.ProductCache#<
/cfoutput>
</cflock>
Inside a cflock tag, displays the contents of the
Application.productCache variable.
Code Description