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.
For this tutorial we will look at one coldfusion task that almost everyone
uses. The next-n style of navigating
multiple pages of data. The next-n navigation allows users to brows back and
forth through query results one page at a time. The problem here is that every
time the page is loaded it has to query the database. To make things worse,
It usually returns ALL the data from the database, It is normally the cfoutput
loop that restricts the amount of data being displayed. Regardless of what is
being displayed, ALL the rows are being retrieved. If your visitor clicks the
next or back buttons 10 times you just performed 10 queries for the exact same
data.
I guess you figured out by now that this is a non-efficent query method and
really a waste of resources. This is where Query Caching comes in handy. You
can cache query results for a specified amount of time and coldfusion
does not have to rerun the query until is needs too. Query cashing is the easiest
item to add and only involves adding a single line to your existing code.
A sample query for this tutorial might look something like this:
<cfquery name="movies"
datasource="MovieList">
SELECT MovieTitle, ProductionCompany,
MovieYear, Catagory
FROM MovieList
ORDER BY MovieTitle
</cfquery>
If I had hundreds of records in my movie database I would want to cache the
query result, and add a next-n style navigation since we want to take advantage
of the saved data, we could add one line to save the data for us and reuse it
for the next page. That query would look like this:
<cfquery name="application.list"
datasource="MovieList" CACHEDWITHIN="#CreateTimeSpan(0,0,15,0)#">
SELECT MovieTitle, ProductionCompany,
MovieYear, Catagory
FROM MovieList
ORDER BY MovieTitle
</cfquery>
As you can see, the big difference here is the CACHEDWITHIN attribute that
we added. I'll explain what this does and how it works.
The CACHEDWITHIN attribute specifies how long a query is to be used before
it needs to be re-run. The CreateTimeSpan () function is used to create the
interval values. It needs 4 values in order to operate. days, hours, minutes,
seconds. So in my query above, CreateTimeSpan(0,0,15,0) would be (0 days, 0
hours, 15 minutes, and 0 seconds) after 15 minutes my query will be re-read
and and possible cashed again.
That's it, no other programming involved.
Date added: Mon. January 12, 2004
Posted by: Mark Aplet | Views: 8169 | Tested Platforms: CF3,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
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. - 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
|
Wat if the database was updated?
If the database was updated by another user wouldnt the record retrieve previously within the cached time be outdated?
Posted by: Andrew
Posted on: 02/02/2005 08:53 PM
|
What if the database was updated?
Yes it would be outdated so to speek. It would not query again until the time limit expired. This is good for queries that do not need to up to the moment.
Posted by: Mark
Posted on: 02/03/2005 12:53 AM
|
|