SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Training
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


Article Topics

All Articles
Performance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
Developer
General DBA
ASP.NET / ADO.NET

Write for Us

Share your SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

Working with Windows Communication Foundation (WCF)
Transfer Logins Task and Transfer Database Task in SSIS
Practical Database Change Management (Part 2)
Practical Database Change Management (Part 1)

More     
 
Latest FAQ's

ALTER TABLE SWITCH statement failed because column '%.*ls' has data type ...
ALTER TABLE SWITCH statement failed because column '%.*ls' has data type ...
ALTER TABLE SWITCH statement failed. There is no identical index in ...
'%ls' statement failed because the expression identifying partition number for the ...

More     
   
Latest Software Reviews

Spotlight on ApexSQL Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Data Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Doc 2008
ApexSQL Enforce

More     

articles >> performance tuning >> StrCat.Catter - An Efficient String Concatenation Component ...

StrCat.Catter - An Efficient String Concatenation Component

By : Michael Balloni
Apr 03, 2000

I wrote the COM component - StrCat.Catter - because string concatenation in VBscript has poor performance characteristics. I found that code like this:

<%
Dim rs
Set rs = ExecuteSql("SELECT Username FROM Users ORDER BY Username")
Dim some_str
some_str = ""
do until rs.EOF
some_str = some_str & rs(0) & "<br>" & vbCrLf
rs.MoveNext
loop
Response.Write some_str
%>


performed very poorly. The code took a long time to execute, during which time the processor was totally saturated. I found articles on MSDN stating that this type of code was a bad idea, and I've heard that this type of concatenation results in run times that are proportional to the square of the number of concatenations.

There are times when a solution based on passing strings, instead of passing function pointers or function objects capable of outputting strings, is favorable. For example, let's say I have a general routine that asks the user to verify their billing information before they make a purchase, and this routine needs to output a summary of what the user is buying. You might be tempted to say:

<%
' Output your purchase summary...
' ...then output your billing information verification system.
%>


But what if your billing information verification code needs to sprinkle HTML on both sides of the purchase summary? Then you're better off passing that purchase summary, as a string, to the billing information verification code. You could put together some sort of "object that outputs purchase summaries when this method is called" type of system. While VBScript allows for this type of system, let's keep things simple and pretend we just want to pass in a string and we need efficient string concatenation.

Enter StrCat.Catter. "Catter" is a C++ COM component with a very small interface. Here's how it can typically be used:

<%
Dim strcat
Set strcat = Server.CreateObject("StrCat.Catter")
Dim rs
Set rs = ExecuteSql("SELECT Username FROM Users ORDER BY Username")
do until rs.EOF
strcat rs(0) & "<br>" & vbCrLf
rs.MoveNext
loop
Dim some_str
some_str = strcat.Dump
Set strcat = nothing
Response.Write some_str
%>


That's pretty simple, isn't it? By having the concatenation method be the default method, you just say the Catter's name - in this case "strcat" - and it just tacks on the string you give it. When you're ready to use the whole string, call the "Dump" method and you've got it!

Interface
For those who know IDL - or can fake it - Catter's interface is as follows:

[id(0), helpstring("Add a string to the end of this. Like str = str & <param>.")]
HRESULT Cat([in] BSTR strToAdd);

[id(1), helpstring("Compute the total string for this.")]
HRESULT Resolve([out, retval] BSTR* pStrOutput);

[id(2), helpstring("Empty the contents of this.")]
HRESULT Reset();

[propget, id(3), helpstring("Get the current length of the total string. Like Len, but much faster.")]
HRESULT Length([out, retval] int *pVal);

[id(4), helpstring("Add a string to the front of this. Like str = <param> & str.")]
HRESULT Prepend([in] BSTR strToPrepend);

[id(5), helpstring("Surround the current contents with a before and after string. Like str = <param1> & str & <param2>.")]
HRESULT Surround([in] BSTR strToPrepend, [in] BSTR strToAppend);

[id(6), helpstring("Dump = Resolve then Reset. Returns the total strings, and empties the contents of this.")]
HRESULT Dump([out, retval] BSTR* pStrOutput);

[id(7), helpstring("Synonym for Cat. Like str = str & <param>.")]
HRESULT Append([in] BSTR strToAdd);


As you can see, this is general-purpose string assembly engine. At first there was just Cat and Resolve. Then I found that I needed Length, Prepend, and Surround. I mostly use Dump instead of Resolve because it frees resources sooner. Append and Reset are included for completeness.

Usage
I like to use this component for concatenation of SQL query strings, both in VB and in ASP scripts. SQL Server devours big batches of SQL much better than lots of little requests. Since Catter is so efficient, great gains can be had when the size of the strings and the number of concatenations grow.

As an example, I use this component to manufacture SQL that marks all of a set of database-modeled files and folders and their children folders and files as public or private. If there are just a few files and folders, you can get away with VB string concatenation. Throw in a few thousand files and deeply nested folders, and you need the improved performance of Catter.

    Next Page>>    








Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | SQL Server Training Videos | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | QDPMA Performance Tuning | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 1999-2008 by T10 Media. All rights reserved