One thing I am always trying to do is speed up my applications. As my site
grows in size and complexity I find that I spend a fair amount of time re-coding
pages because of a new technique I just learned. I wish I had learned about
these techniques long before, and thereby allowing me to create more effective
code. In this tutorial I'll try to explain some problem areas that I have identified,
and some of the things you can do to improve performance.
Eliminate Unnecessary Queries:
Database access is a very important part of any web application. But that access
has performance costs. It seams that many bottlenecks are at the database level.
Finding a way to eliminate unnecessary queries can really improve application
performance. Just so you know, we are not going to actually delete the queries,
just restrict them a bit.
So what sort of queries can be eliminated? Good question glad you asked. Here
are some checkpoints that I am using to determine what queries I can eliminate.
1. Is the query static? (not built dynamically)
2. Does the information not change very frequently?
3. Is the data used throughout the application?
Through the use of APPLICATION variables we can share data from a single query
across all the users of a site. Because APPLICATION are available to all users
and shared across an entire application, so is the data within them. In other
words by using APPLICATION variables, it become possible to execute a query
once and then reuse the data from that query over and over again.
Lets begin by creating the application.cfm
<!--- // BEGIN APPLICATION
// --->
<cfapplication name="MovieList">
<!--- // RUN QUERY
ONLY IF IT HAS NOT BEEN SAVED // --->
<cfif NOT
IsDefined ("application.list")>
<cfquery name="application.list" datasource="MovieList">
SELECT MovieTitle, ProductionCompany,
MovieYear, Catagory
FROM MovieList
ORDER BY MovieTitle
</cfquery>
</cfif>
When saving APPLICATION scope variables make sure to always wrap them in a
<cfif> block that checks if the query had already been saved. If you do
not do this it will run the query on every page defeating the purpose of this
tutorial.
In this application, the key is that the first time the page loads, it will
look for "application.list" if it does
not find a matching variable then it will perform the query otherwise it will
skip it as it is already saved. Also notice how the query name is prefixed with
the APPLICATION scope.
Next we will create the output page called display.cfm
<html>
<head>
<title>My Movies</title>
</head>
<table width="100%"
border="1" cellspacing="0"
cellpadding="2">
<tr>
<td><b>Movie
Title</b></td>
<td><b>Production
Company</b></td>
<td><b>Year</b></td>
<td><b>Category</b></td>
</tr>
<cfoutput query="application.list">
<tr>
<td>#MovieTitle#</td>
<td>#ProductionCompany#</td>
<td>#MovieYear#</td>
<td>#Catagory#</td></tr>
</cfoutput>
</table>
</body>
</html>
When you load the page for the first time the query is run and the variables
are loaded into the application. Now the variables are available to everyone
surfing your site and don't need to be reloaded until the application times
out or expires. Look below at the screen shot. You can see that the debuging
has shown that the SQL for the query has been executed.

If you refresh the page the query is not preformed and the variables are reused.
So as you can see if you have data that does not need to be updated on every
query and if the data is not specific to the user, then you can help save a
valuable cpu time and improve the performance of your application.

Date added: Mon. January 12, 2004
Posted by: Mark Aplet | Views: 10636 | Tested Platforms: CF4,CF5,CFMX | Difficulty: Intermediate
Best Practices
Methodologies
Reusing Code
 |
Banning the spam
Internet spam is on the rise, and more importantly spammers are targeting your sites comment forms. They are looking for the trackback urls to fool search engines into ranking their website higher in the search results. When this started to happen to me, I wanted to sent out emails to the offenders demanding that they stop. Unfortunatly the spam is being generated by bots and programs not some pimple faced kid behind a keyboard. Banning IP addresses is not enough and rarely works since intelligent spammers hide their true identity anyway. Next approach... Banning Keywords used by the offending sites. Thats where this tutorial comes in. - Date added: Wed. March 15, 2006
Improving Application Performance (Part 2)
Not all queries can be saved as an application variable. For Queries that do not meet the checkpoints in my previous tutorial there is another way to improve performance. Query Caching is another way to save data and eliminate unnecessary queries. This is for queries that are more dynamic in nature. - Date added: Mon. January 12, 2004
Slighty better search
Someone on the forum posed a question a short while ago asking how to create a more advanced search function using a + symbol as a separator. So I created this advanced search function. This search function is just slightly better than a normal search as it adds the ability to separate two keywords with a + symbol. Lets start with the search form. - Date added: Thu. December 4, 2003
Dynamic Sorting with CFSWITCH
Quickly and easily sort and order records in your database using a cfswitch in your query. Great technique for admin areas of your site, or just allowing visitors to sort the fields they want. - Date added: Sun. August 3, 2003
Color Picker
Sometimes, you want to be able to change the color of something on your page. Be it one item, or every item on the page. Using this simple color picker, you can create admin areas that can allow you or your visitors to pick their own colors and the value is automatically inserted into a text field. - Date added: Sat. July 12, 2003
· Adding an indexed Search to your site (Part 2)
· Adding an indexed Search to your site (Part 1)
· Changing site color scheme
|
solid, simple...
Great tip since it's so solid and so simple.
Perhaps could be expanded so that it is more clear how to reset it via an expired application or manually after more records are added to the database.
Still a good one since it will cut down on a lot of uneededdb queries.
Posted by: count_schemula
Posted on: 04/02/2005 01:41 PM
|
CFLOCK ?
Should you be using <cflock> when you read application variables@?
Posted by: Paul
Posted on: 08/09/2005 08:16 AM
|
Query check
You can always skip the 'IsDefined' step for the query, and do this instead:
<cfquery name='Application.Data' CACHEDWITHIN='#CreateTimeSpan(1,0,0,0)#'>
that will cache the query auto-magically using coldfusion for 1 day, or until your application scope expires.
Posted by: Justice
Posted on: 08/18/2005 03:26 PM
|
|