ColdFusion CFQuery BlockFactor Is Not Impressing Me

Posted October 11, 2006 at 6:09 PM

Tags: ColdFusion, SQL

I have never used the ColdFusion CFQuery blockfactor attribute before. I have heard about it, but never payed much attention. I came across it in an article the other day, so I thought I'd give it a spin to see how it performs... and I have to say, I am not really seeing any great results. To test it, I am doing a simple query that returns the union repeated union of the largest table I have in the database.

Here is the standard CFQuery tag, no blockfactor in use:

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

  • <!--- Test the speed of the query. --->
  • <cftimer label="Standard Query" type="outline">
  •  
  • <cfquery name="qHit" datasource="...">
  • SELECT
  • t.id
  • FROM
  • (
  • (
  • SELECT
  • id
  • FROM
  • web_stats_hit
  • )
  •  
  • UNION ALL
  •  
  • (
  • SELECT
  • (id * 100)
  • FROM
  • web_stats_hit
  • )
  •  
  • UNION ALL
  •  
  • (
  • SELECT
  • (id * 1000)
  • FROM
  • web_stats_hit
  • )
  •  
  • UNION ALL
  •  
  • (
  • SELECT
  • (id * 10000)
  • FROM
  • web_stats_hit
  • )
  • ) AS t
  • ORDER BY
  • t.id ASC
  • </cfquery>
  •  
  •  
  • <p>
  • #NumberFormat( qHit.RecordCount, "," )# web hits found
  • </p>
  •  
  • </cftimer>

This returns 210,868 records in about a 800 ms (on average). That's a pretty good record set I think. Now, let's throw in the blockfactor attribute. In ColdFusion, this can be between 1 and 100 and stands for the number of records that the SQL server can return at a given time.

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

  • <!--- Test the speed of the query with BlockFactor. --->
  • <cftimer label="BlockFactor Query" type="outline">
  •  
  • <cfquery name="qHit" blockfactor="100" datasource="...">
  • SELECT
  • t.id
  • FROM
  • (
  • (
  • SELECT
  • id
  • FROM
  • web_stats_hit
  • )
  •  
  • UNION ALL
  •  
  • (
  • SELECT
  • (id * 100)
  • FROM
  • web_stats_hit
  • )
  •  
  • UNION ALL
  •  
  • (
  • SELECT
  • (id * 1000)
  • FROM
  • web_stats_hit
  • )
  •  
  • UNION ALL
  •  
  • (
  • SELECT
  • (id * 10000)
  • FROM
  • web_stats_hit
  • )
  • ) AS t
  • ORDER BY
  • t.id ASC
  • </cfquery>
  •  
  •  
  • <p>
  • #NumberFormat( qHit.RecordCount, "," )# web hits found
  • </p>
  •  
  • </cftimer>

Again, this returns 210,868 records. This ran in about 900 ms on average. That's 100 ms SLOWER than the same query with no blockfactor. I tried adjusting the size of the blockfactor, but none of the numbers I used seemed to make a difference.

I understand that not all databases understand the blockfactor. But, I'm using Microsoft SQL Server. I don't know, but I am guessing that it understand blockfactor as it is like THE sql server out there (not intending to stir up conflict here).

So, operating under the assumption that MS SQL Server does indeed work with blockfactor, my testing shows (repeatedly) that the CFQuery that uses blockfactor performs slower than the non-blockfactor CFQuery. What gives? Is there something here that I am totally not getting? Is 211,000 records not enough for the blockfactor to make a dent? Why is the blockfactor running slower?

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

Oct 11, 2006 at 7:24 PM // reply »
9 Comments

Ben,

It's been a while, but as I remember it, blockfactor seemed to work best with oracle databases.


Oct 11, 2006 at 10:25 PM // reply »
6,516 Comments

Damn you Oracle... Damn you :) Thanks for tip Rob.


Oct 12, 2006 at 12:25 PM // reply »
6 Comments

A couple of things I've noticed over the years about blockfactor. First off, in Oracle they can make a HUGE difference. For very intense queries, where without the processing time could be 40,000+ milliseconds without blockfactor, I've seen them drop to 1,000+. Quite a difference.

Another thing, I think I remember about them, it isn't that MS SQL Server doesn't know how to use them, it might be that the driver you are using to connect to the database doesn't use them.

Last but not least, you don't always see the speed difference in a query if you only run it once. Sometimes you have to run it several time to see that there is in fact a difference.

Just my two cents, hope it sheds some light...


Oct 12, 2006 at 12:28 PM // reply »
6,516 Comments

Demian,

I appreciate the comment. I never thought about the actual driver having anything to do with it. I am a bit removed from all of that stuff. I also live in a MS SQL Server-centric universe :) Good to know though. Thanks.


Oct 13, 2006 at 10:30 AM // reply »
11 Comments

One thing I remember about blockfactor that may be of large importance to you is that the blockfactor is not the NUMBER OF ROWS that the database should return in a single packet, but the SIZE OF THE DATA it should return in a single transfer packet.

You may find this quote from MM's coding guidlines useful:

BEGIN QUOTE

Use blockFactor to increase query performance:
Adding blockFactor to a query can significantly improve performance. To add blockFactor, examine the data that is being returned. Determine the maximum size (in bytes) of each row. Take that size and determine how many times that number would divide into 32k. That number is your blockFactor, but be aware that the max blockFactor is 100. So, if for example you were getting 200 bytes per row, you could easily fit over 100 rows into the 32k buffer that CF 'grabs' at one time.

If you know at runtime that you will have less then 100 rows returned, for example you're writing a query that either returns 0 or 1 rows, do not bother adding the blockFactor attribute.

http://livedocs.macromedia.com/wtg/public/coding_standards/performance.html

END QUOTE

I have seen this attribute make a big difference when used correctly. Also consider how fast the network between your database and CF server are (did you mention where your DB is relative to your CF Server?)...on a faster network, the benefit may not show up until larger amounts of data are being passed or the complexity of the work blockFactor adds to the process is less than the benefit it provides to the data transfer process.

Anyway, I would suggest some more playing around on this, not that the CF Docs really give an accurate depiction of the blockFactor attribute in the first place.

Mike.


Oct 13, 2006 at 10:57 AM // reply »
6,516 Comments

Mike,

Thanks for the good comment. Our local DB is on a different machine than our CF server. Same network... same shelf :) I will do some more playing around, but at over 200,000 records in my example, I was very suprised to only see no advantage, but to actually see a disadvantage to using blockfactor.


Jun 5, 2009 at 12:34 PM // reply »
1 Comments

Working on a large query using the blockfactor tag attribute did make a difference in the return of the data. Using a data source connecting to a MS SQL Server 2005.

Thank you for the awesome site...


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 »