|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
CF101 Using Database Views and Stored Procedures
Offloading some of the processing from ColdFusion to the database server
By: Jeffry Houser
Oct. 10, 2005 01:00 PM
Most of us are never going to work on a Web site that gets the amount of traffic that Google or Yahoo (or MySpace) gets. Unfortunately, you can get away with bad coding practices on a small site that only gets 100 unique visitors a month and no one will ever know. With such a low visitor count, you'll never have to deal with the "why isn't my site loading quick enough" problem.
Using a View to Pre-Join Tables Now suppose you want to write a query that retrieves all the songs by a particular band. In ColdFusion it might look something like the JH Code Segment 1 at the end of this article. You're joining five tables to get the information you need, and this can be considered a fairly complicated join. If we were to move this code into an SQL Server view, you'd achieve efficiency. A view is a virtual table that lets you represent data in alternate ways. The most common way to create them is using Enterprise Manager, but you can also use T-SQL. In Enterprise Manager, you can follow these steps:
When writing ColdFusion code you can access the view just like a table, as shown in JH Code Segment 2. Notice that the view doesn't contain the query qualifier 'band = #variables.bandID#'. The view returns all the bands and their respective songs. SQL Server will optimize views using built-in optimization techniques. These techniques aren't applied to single queries. You can also index views the same way you index tables.
Advanced Logic in a Stored Procedure In many of my objects, I'll often implement an "initByData" method. Instead of a method that makes a database call and sets up the component, all data is manually passed into this method to set up the component. Using this method, I won't have to daisy chain calls to the database. So instead of 27 different database calls, I can use one query to get the band information, one query to get the band's album information, and one query to get the song information about each album. Our database calls went from 27 queries to three. Not bad. Using a stored procedure, we can put all three queries in a single stored procedure call, resulting in only one trip to the database. This will put more pressure on the database server, but will most likely give your ColdFusion application server a break. (It's been working hard, so it's earned it.) You can follow these steps to create a stored procedure:
Stored procedures are precompiled objects. Unlike traditional queries (that you execute using the cfquery tag), the code doesn't have to be compiled first, which improves performance at runtime. You can execute a stored procedure using the cfquery tag using the 'exec' command, like JH Code Segment 4. This method works, but only returns a single recordset to ColdFusion. It's undesirable in situations where you want to return multiple recordsets. The alternative is to use the cfstoredproc tag. Full documentation on the tag can be found at http://livedocs.macromedia.com/coldfusion/7/ htmldocs/00000338.htm#2607555. Some attributes are similar to what you'd find in the cfquery tag, such as a datasource, a username, password, blockfactor, and result. Other attributes are specific to cfstoredproc, such as procedure for the procedure name. Along with the cfstoredproc, there are two subtags that can be used. Cfprocparam is used to send information to the stored procedure, and full documentation can be found at http://livedocs.macromedia.com/coldfusion/7/ htmldocs/00000313.htm#1102102. Cfprocresult is used to return information from the procedure. Full documentation for cfprocresult can be found at http://livedocs.macromedia.com/coldfusion/7/ htmldocs/00000314.htm#1102246. The code is in JH Code Segment 5. For cfprocparam, the two parameters needed for this example are the value and the cfsqltype. The cfprocresult tag uses two parameters: name and resultset. The name is the variable that will contain the resultset after the stored procedure has executed. This is similar to the name attribute of the cfquery tag. The resultset should contain a numerical value. If the value is one, then the value will be given the first recordset returned from the stored procedure. If the value is two, then the variable is given the second record set.
Where To Go from Here You might also look up how to create views and stored procedures using Transact SQL. With one of my current projects, we decided to create SQL files that contain the T-SQL to create the views and stored procedures. By storing these files in the repository, we're able to track database changes and even add some version control to the code. Reader Feedback: Page 1 of 1
Latest Cloud Developer Stories
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
Breaking Cloud Computing News
|
|||||||||||||||||||||||||||||||||||||||||||||||||