|
Comments
|
Today's Top SOA Links
BF on CF Ben Forta on ColdFusion
Ben Forta on ColdFusion
By: Ben Forta
May. 6, 1999 12:00 AM
In my last column (CFDJ Vol. 1, Issue 2) I discussed database query caching - how to improve application performance by eliminating unnecessary database access. While caching query results will improve application performance, optimizing your application doesn't stop there. In this column I'd like to continue with the subject of improving performance by improving the efficiency of database operations, but from another perspective. Specifically, I'd like to take a look at how databases should (and shouldn't) be used, and at database features that can be used to enhance application performance. It's worth noting that some of the features and technologies discussed here (particularly items 3-5) are available only in client/server databases - which is all the more reason you should be using one.
1. Don't Do the Database's Job Let's look at a simple example - you need to retrieve a list of all customers who have placed more than one order. You could write a SQL statement to retrieve all orders, and then loop the results to count orders per customer (you'd also need some sort of array or list to store the customers in). Then you'd filter out the ones with only one order. Or you could use a SQL statement that looks something like this:
SELECT cust_id, COUNT(*) AS num_orders If you want to count records that match a specific criterion, total items in an order or find the highest value in a set, you can perform these operations with simple SQL SELECT statements. Sure, you can retrieve the records with a <CFQUERY> and then loop through the results to perform the calculations yourself. But why would you want to? All you're doing is making ColdFusion work harder than it should, while forcing your database server to sit idle. Any time you find yourself looping through result sets to perform calculations, passing the results of one query to a second query, or maintaining long lists of variables that contain running totals or other calculations - take a step back, and determine if you're doing the database's job. That's something you never want to do. It's worth noting that more often than not, ColdFusion developers violate this rule because they're having a hard time with the SQL needed to perform the specific operations (be it the aggregate functions, joins or the correct use of subqueries). And while re-creating the wheel in ColdFusion works well as an interim solution, it's going to create performance and scalability problems down the road. My advice is to grab a good SQL book and master the language (try Sams Teach Yourself SQL in 24 Hours, or wait for my own soon-to-be-released Sams Teach Yourself SQL in 10 Minutes).
2. Enforce Data Constraints Data constraints are used to enforce data integrity, and employing constraints of some kind is an important part of application design. Many ColdFusion developers write code to enforce integrity - they validate form fields against databases (using a <CFQUERY> tag) before inserting them into tables; they write complex <CFIF> statements to check for valid values, and then convert data on the fly using lists of <CFSET> statements. Once again, this is a task best left to the database. Every decent database allows you to define constraints. From simple rules that establish default values or restrict values to those in another table (foreign keys) to more complex rules that define sets of valid values under specific conditions. Here's a practical example, and one I see asked repeatedly on the Allaire Developer's Forum. You're inserting a record into a table, and you want to save the current date and time along with the record in a timestamp field. Sure, you could play with the ColdFusion Now() and CreateODBCDateTime() functions, but why would you want to? All you need to do is instruct the database to use the current date and time if no explicit value is provided, and then provide no value!
So why is this validation better suited for the database level rather than at the application level? There are two primary reasons: Stored procedures are blocks of SQL code that are stored on the database server - similar to custom functions (or ColdFusion Custom Tags). Using stored procedures, you can group sets of related SQL statements together as a single unit. Instead of calling each SQL statement individually and passing values retrieved in one statement down to the next, you can simply call a stored procedure that contains all the needed statements.
The fine points of stored-procedure use probably require their own column, but for now it's worth noting the primary advantages of using stored procedures: ColdFusion fully supports the use of stored procedures. In ColdFusion 4.0 you now have two different methods with which to invoke stored procedures: as simple SQL passed to a <CFQUERY> tag, and via the new <CFSTOREDPROC> tag family. The detailed differences between the two are beyond the scope of this column, but Table 1 lists the basic advantages of each. There is no hard and fast rule about where to use stored procedures and where not to, but here's one place to start: if you ever find yourself calling a <CFQUERY> and then passing the results of that query to another <CFQUERY> (perhaps using the ValueList() or QuotedValueList() functions), that code should probably be a stored procedure.
4. Be Trigger-Happy An ideal use of triggers is data cleanup. For instance, if you want state abbreviations always inserted in uppercase you can write a simple trigger for INSERT and UPDATE operations that will replace the passed state abbreviation with an uppercase version. Of course, not only will this be faster than doing data conversion at the ColdFusion level, but it also will allow all client applications (including ColdFusion) to share the same code - and it all happens transparently.
5. Schedule Tasks For example, if you need to execute daily SQL cleanup code (to perform database cleanup), or if you need to move data from a live table to an archive table on a regular basis, or if you need to run nightly calculations on one table and write the results to another - all of these are best handled by the database's own scheduling engine. The rule here is to use ColdFusion scheduling to schedule ColdFusion processing and use database scheduling to schedule database processing. It makes absolutely no sense to tie up ColdFusion when it's the database doing the actual work.
Conclusion Reader Feedback: Page 1 of 1
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
|||||||||||||||||||||||||||