Structs As Query Indexes, Speed, And Rick Osborne

Posted September 13, 2006 at 8:02 PM

Tags: ColdFusion

For those of you who follow my blog, you will know that Rick Osborne is the guy who comes in after I explain things and makes killer suggestions about how they can be done better. I recently gave a case study of how ColdFusion code can be optimized. It involved using the IndexOf() method of the ColdFusion query column object. Rick came in and suggested that using a Struct to create your own query-index would perform faster. As I am a man who likes to learn by doing, I thought I would put this to the test.

To test this, I query from a web statistics program. I am getting information from two tables: web_stats_hit and web_stats_session. Each "hit" in the hits table will have a session id in the session table. The session table also have a UUID column "session_id".

As I am trying to test the merging of two data sources, I am going to hit each table individually and then try to update one with matching values from the other. First I am grabbing the two different data sets:

 Launch code in new window » Download code as text file »

  • <!--- Query for web hits. --->
  • <cfquery name="qHit" datasource="...">
  • SELECT
  • h.id,
  • h.date_created,
  • h.web_stats_session_id,
  • (
  • ''
  • ) AS session_id
  • FROM
  • web_stats_hit h
  • </cfquery>
  •  
  •  
  • <!--- Query for web sessions. --->
  • <cfquery name="qSession" datasource="...">
  • SELECT
  • s.id,
  • s.session_id
  • FROM
  • web_stats_session s
  • </cfquery>

As I tried to explain earlier, for every qHit.web_stats_session_id, there is a matching session, such that for some combo, qHit.web_stats_session_id == qSession.id. And just to get an idea of the amount of data we are talking about:

qHit: 52,290 records

qSession: 34,753 records

That's a LOT of data to go through. Let's get our "test" on:

 Launch code in new window » Download code as text file »

  • <cftimer label="IndexOf() Methodology" type="outline">
  •  
  • <!--- Loop over the hit query. --->
  • <cfloop query="qHit">
  •  
  • <!---
  • We want to find a matching session_id based
  • on the session. Get index of matching row.
  • --->
  • <cfset intIndex = qSession[ "id" ].IndexOf(
  • JavaCast( "int", qHit.web_stats_session_id )
  • ) />
  •  
  • <!--- Add one to index (to be ColdFusion friendly). --->
  • <cfset intIndex = (intIndex + 1) />
  •  
  • <!--- Check to see if we have an index. --->
  • <cfif intIndex>
  •  
  • <!--- We found the match, update the row. --->
  • <cfset qHit[ "session_id" ][ qHit.CurrentRow ] =
  • qSession[ "session_id" ][ intIndex ]
  • />
  •  
  • </cfif>
  •  
  • </cfloop>
  •  
  • </cftimer>
  •  
  • <cftimer label="Struct Index Methodology" type="outline">
  •  
  • <!--- Create a session look up table. --->
  • <cfset objSessionLookUp = StructNew() />
  •  
  • <!---
  • Loop over session and set index rows. We will be using
  • the id column of the session as the key and the
  • session_id as the value. This creates our very own,
  • in-memory index of the qSession query based on ID.
  • --->
  • <cfloop query="qSession">
  •  
  • <!--- Index this value. --->
  • <cfset objSessionLookUp[ qSession.id ] = qSession.CurrentRow />
  •  
  • </cfloop>
  •  
  •  
  • <!--- Loop over the hit query. --->
  • <cfloop query="qHit">
  •  
  • <!---
  • Check to see if the session key exists. If it
  • does, then we found a match.
  • --->
  • <cfif StructKeyExists(
  • objSessionLookUp,
  • qHit.web_stats_session_id
  • )>
  •  
  • <!--- Update the session based on the struct-index. --->
  • <cfset qHit[ "session_id" ][ qHit.CurrentRow ] =
  • objSessionLookUp[ qHit.web_stats_session_id ]
  • />
  •  
  • </cfif>
  •  
  • </cfloop>
  •  
  • </cftimer>

It turns out Rick was absolute correct. The struct index performs MUCH faster. Here are the stats:

IndexOf() Methodology : 402,037 ms

Struct Index Methodology : 33,403 ms

Some quick math will show you that the struct index method performs in 8% of the time that the IndexOf() methodology does. EIGHT PERCENT! Nuts. I guess the only downside is that you can potentially create a HUGE in-memory structure; but it's only temporary.

Nicely done Rick!

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Sep 14, 2006 at 11:29 AM // reply »
153 Comments

I figured you'd appreciate this:

http://rickosborne.org/images/screenshots/wtg-ben.png

That graph represents the last week's traffic to my site. This site has accounted for 3% of the traffic to my site, behind only Google and MXNA.


Sep 14, 2006 at 11:32 AM // reply »
6,516 Comments

Rick,

That's awesome :) Glad to be sending people to you. You have good information to share.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »