Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Performance CLR function

 
   Database Help (Home) -> Programming RSS
Next:  best way to compare data  
Author Message
TimXox

External


Since: Jun 12, 2008
Posts: 7



(Msg. 1) Posted: Thu Jun 12, 2008 11:18 am
Post subject: Performance CLR function
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Hi,
I created CLR aggregate function MULT which works like Sum, but instead of +
it does *. I compare the performance of my function with:
SELECT exp(sum(log(AttributionValue+1)))
the result is the same , but for the same number of rows my function takes
almost twice time more.
Please advice,
Tim.

 >> Stay informed about: Performance CLR function 
Back to top
Login to vote
TheSQLGuru

External


Since: Jan 11, 2008
Posts: 579



(Msg. 2) Posted: Thu Jun 12, 2008 5:15 pm
Post subject: Re: Performance CLR function [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I advise you to not reinvent the wheel. Smile

--
Kevin G. Boles
Indicium Resources, Inc.
SQL Server MVP
kgboles a earthlink dt net


"TimXox" wrote in message

> Hi,
> I created CLR aggregate function MULT which works like Sum, but instead of
> +
> it does *. I compare the performance of my function with:
> SELECT exp(sum(log(AttributionValue+1)))
> the result is the same , but for the same number of rows my function takes
> almost twice time more.
> Please advice,
> Tim.

 >> Stay informed about: Performance CLR function 
Back to top
Login to vote
Tibor Karaszi

External


Since: Jan 29, 2004
Posts: 1559



(Msg. 3) Posted: Thu Jun 12, 2008 9:54 pm
Post subject: Re: Performance CLR function [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I think that the simple answer is that code written as managed code will never be as fast as the
built-in functions that are shipped with the product...

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi


"TimXox" wrote in message

> Hi,
> I created CLR aggregate function MULT which works like Sum, but instead of +
> it does *. I compare the performance of my function with:
> SELECT exp(sum(log(AttributionValue+1)))
> the result is the same , but for the same number of rows my function takes
> almost twice time more.
> Please advice,
> Tim.
 >> Stay informed about: Performance CLR function 
Back to top
Login to vote
Gert-Jan Strik

External


Since: Sep 09, 2003
Posts: 255



(Msg. 4) Posted: Thu Jun 12, 2008 9:56 pm
Post subject: Re: Performance CLR function [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

TimXox wrote:
>
> Hi,
> I created CLR aggregate function MULT which works like Sum, but instead of +
> it does *. I compare the performance of my function with:
> SELECT exp(sum(log(AttributionValue+1)))
> the result is the same , but for the same number of rows my function takes
> almost twice time more.
> Please advice,
> Tim.

On what would you like to be advised? You cannot expect the same
performance from a CLR function when compared to a simple native T-SQL
equivalent. If there is a simple T-SQL equivalent, it will always
outperform a similar CLR function because of the overhead that is
involved in calling a CLR function.

--
Gert-Jan
SQL Server MVP
 >> Stay informed about: Performance CLR function 
Back to top
Login to vote
TimXox

External


Since: Jun 12, 2008
Posts: 7



(Msg. 5) Posted: Fri Jun 13, 2008 6:37 am
Post subject: Re: Performance CLR function [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I cannot agree that SQL - 92 has equvalent to this aggregation function.
The standart SQL has SUM, AVG,MAX,MIN, ect , but not MULTIPLY.
All I do is just M = a1 * a2 * a3 * a4 ..............
Of couse we may work around using native functon, but they are much more
complicated and have some limitations:

M = EXP( LOG(a1) + LOG(a2) + LOG(3) + LOG(a3) +......

Notice that amount of calculations which native functions have to do is
enormous compare to my func. So it looks like for me that CLR is very slow
for this ( PURE CALCULATION) case.
So, what is the point to have CLR. Or let me put another question: If I
have to do some regretional analisys with millions of row should I use pure
T-SQL ? I hoped CLR is good candidate here.
Thanks,
Tim.



"Gert-Jan Strik" wrote:

> TimXox wrote:
> >
> > Hi,
> > I created CLR aggregate function MULT which works like Sum, but instead of +
> > it does *. I compare the performance of my function with:
> > SELECT exp(sum(log(AttributionValue+1)))
> > the result is the same , but for the same number of rows my function takes
> > almost twice time more.
> > Please advice,
> > Tim.
>
> On what would you like to be advised? You cannot expect the same
> performance from a CLR function when compared to a simple native T-SQL
> equivalent. If there is a simple T-SQL equivalent, it will always
> outperform a similar CLR function because of the overhead that is
> involved in calling a CLR function.
>
> --
> Gert-Jan
> SQL Server MVP
>
 >> Stay informed about: Performance CLR function 
Back to top
Login to vote
--CELKO--

External


Since: Jan 11, 2008
Posts: 1089



(Msg. 6) Posted: Fri Jun 13, 2008 9:58 am
Post subject: Re: Performance CLR function [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

>> Or let me put another question: If I have to do some regression Analysis with millions of row should I use pure T-SQL ? I hoped CLR is good candidate here. <<

The right answer is get SAS or another stat package for this kind of
work. They will handle floating point errors, have good functions,
etc. SQL is not a computational language.

CLR will stink and make maintaining the code a nightmare. Gert-Jan is
right on the money with his remark about CLR being a way for non-SQL
programmers to screw up databases in their native language.

As an aside, your PRD() function is wrong. Here is a version of the
aggregate product function in SQL. You will need to have the
logarithm and exponential functions. They are not standards, but they
are very common.

The idea is that there are three special cases - all positive numbers,
one or more zeros, and some negative numbers in the set.

You can find out what your situation is with a quick test on the
sign() of the minimum value in the set.

Within the case where you have negative numbers, there are two sub-
cases: (1) an even number of negatives or (2) an odd number of
negatives. You then need to apply some High School algebra to
determine the sign of the final result.

SELECT CASE MIN (SIGN(nbr))
WHEN 1 THEN EXP(SUM(LN(nbr))) -- all positive numbers
WHEN 0 THEN 0.00 -- some zeroes
WHEN -1 -- some negative numbers
THEN (EXP(SUM(LN(ABS(nbr))))
* (CASE WHEN
MOD (SUM(ABS(SIGN(nbr)-1)/ 2)), 2) = 1
THEN -1.00 ELSE 1.00 END)
ELSE NULL END AS big_pi
FROM Foobar;

The book BYPASSES:A SIMPLE APPROACH TO COMPLEXITY by Z. A. Melzak
(Wiley-Interscience, 1983, ISBN 0-471-86854-X), is short mathematical
book on the general principle of conjugacy. This is the method of
using a transform and its inverse to reduce the complexity of a
calculation.
 >> Stay informed about: Performance CLR function 
Back to top
Login to vote
TimXox

External


Since: Jun 12, 2008
Posts: 7



(Msg. 7) Posted: Fri Jun 13, 2008 1:08 pm
Post subject: Re: Performance CLR function [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Gentlemen,
Thank you for your input. Here what I read in SQL 2005 docs:

...”Because managed code compiles to native code prior to execution, you can
achieve significant performance increases in some scenarios….. Managed code
is better suited than Transact-SQL for calculations and complicated execution
logic…..Use managed code for CPU-intensive functions and procedures that
feature complex logic…”

Couple months ago I finished a big project (in SQL 2000) with financial
calculations prototyped by a C++ client-server application. I changed
architecture and moved everything (including calculations) to SQL Server. The
performance gain is huge, but code looks ugly, very difficult to read even
simple formulas. I agree T-SQL is not a language for computations.
My intension was to move code to SQL 2005 and re-write calculation functions
in CLR. ( I used formula exp(sum(log(AttributionValue+1))) just to illustrate
the issue – in realty it is a function with logic very close to the one in
Mr.Celko post).
I am kind of disappointed, but still hope I am doing something wrong.
Below is the code I use.
Please advice.
Tim.



[Serializable]
[Microsoft.SqlServer.Server.SqlUserDefinedAggregate(
Microsoft.SqlServer.Server.Format.UserDefined, //use clr
serialization to serialize the intermediate result
IsInvariantToNulls = true, //optimizer property
IsInvariantToDuplicates = false, //optimizer property
IsInvariantToOrder = false, //optimizer property
MaxByteSize = 8000) //maximum size in bytes of persisted value
]

public class MULT : Microsoft.SqlServer.Server.IBinarySerialize
{
private SqlDouble intermediateResult ;

public void Init() { intermediateResult = 1; }

public void Accumulate(SqlDouble value)
{ if (value.IsNull) {return;} intermediateResult *= value; }

public void Merge(MULT other)
{ intermediateResult *= other.intermediateResult;}

public SqlDouble Terminate()
{ SqlDouble output = intermediateResult;
return (output);
}
public void Read(BinaryReader r)
{
if (r == null) throw new ArgumentNullException("r");
intermediateResult = r.ReadDouble();
}
public void Write(BinaryWriter w)
{
if (w == null) throw new ArgumentNullException("w");
w.Write((double)intermediateResult);
}
}


"--CELKO--" wrote:

> >> Or let me put another question: If I have to do some regression Analysis with millions of row should I use pure T-SQL ? I hoped CLR is good candidate here. <<
>
> The right answer is get SAS or another stat package for this kind of
> work. They will handle floating point errors, have good functions,
> etc. SQL is not a computational language.
>
> CLR will stink and make maintaining the code a nightmare. Gert-Jan is
> right on the money with his remark about CLR being a way for non-SQL
> programmers to screw up databases in their native language.
>
> As an aside, your PRD() function is wrong. Here is a version of the
> aggregate product function in SQL. You will need to have the
> logarithm and exponential functions. They are not standards, but they
> are very common.
>
> The idea is that there are three special cases - all positive numbers,
> one or more zeros, and some negative numbers in the set.
>
> You can find out what your situation is with a quick test on the
> sign() of the minimum value in the set.
>
> Within the case where you have negative numbers, there are two sub-
> cases: (1) an even number of negatives or (2) an odd number of
> negatives. You then need to apply some High School algebra to
> determine the sign of the final result.
>
> SELECT CASE MIN (SIGN(nbr))
> WHEN 1 THEN EXP(SUM(LN(nbr))) -- all positive numbers
> WHEN 0 THEN 0.00 -- some zeroes
> WHEN -1 -- some negative numbers
> THEN (EXP(SUM(LN(ABS(nbr))))
> * (CASE WHEN
> MOD (SUM(ABS(SIGN(nbr)-1)/ 2)), 2) = 1
> THEN -1.00 ELSE 1.00 END)
> ELSE NULL END AS big_pi
> FROM Foobar;
>
> The book BYPASSES:A SIMPLE APPROACH TO COMPLEXITY by Z. A. Melzak
> (Wiley-Interscience, 1983, ISBN 0-471-86854-X), is short mathematical
> book on the general principle of conjugacy. This is the method of
> using a transform and its inverse to reduce the complexity of a
> calculation.
>
>
>
>
 >> Stay informed about: Performance CLR function 
Back to top
Login to vote
Gert-Jan Strik

External


Since: Sep 09, 2003
Posts: 255



(Msg. 8) Posted: Fri Jun 13, 2008 6:21 pm
Post subject: Re: Performance CLR function [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Tim,

IMO, Microsoft added CLR support primarily to make it easier for
application developers with inadequate SQL skills to leverage their
knowledge about their CLR language.

The mere fact that SQL Server supports CLR functions does not mean that
you should use them. The CLR is good for procedural handling. If you
have to do complex manipulation, it can be much easier to write the
algorithm in C# or whatever, because those languages are very rich in
their syntax and supporting functions.

If you have to process large data sets, then the row by row processing
of scalar UDFs might bite you when it comes to performance, and you will
also notice the overhead when using CLR for the function(s).

So I guess it depends. If you want to do "regretional analysis with
millions of rows", you either write it in a language that is easiest for
you (in CLR) and accept that the performance will be accordingly, or you
take the (probably more complex) set oriented approach.

--
Gert-Jan
SQL Server MVP


TimXox wrote:
>
> I cannot agree that SQL - 92 has equvalent to this aggregation function.
> The standart SQL has SUM, AVG,MAX,MIN, ect , but not MULTIPLY.
> All I do is just M = a1 * a2 * a3 * a4 ..............
> Of couse we may work around using native functon, but they are much more
> complicated and have some limitations:
>
> M = EXP( LOG(a1) + LOG(a2) + LOG(3) + LOG(a3) +......
>
> Notice that amount of calculations which native functions have to do is
> enormous compare to my func. So it looks like for me that CLR is very slow
> for this ( PURE CALCULATION) case.
> So, what is the point to have CLR. Or let me put another question: If I
> have to do some regretional analisys with millions of row should I use pure
> T-SQL ? I hoped CLR is good candidate here.
> Thanks,
> Tim.
>
> "Gert-Jan Strik" wrote:
>
> > TimXox wrote:
> > >
> > > Hi,
> > > I created CLR aggregate function MULT which works like Sum, but instead of +
> > > it does *. I compare the performance of my function with:
> > > SELECT exp(sum(log(AttributionValue+1)))
> > > the result is the same , but for the same number of rows my function takes
> > > almost twice time more.
> > > Please advice,
> > > Tim.
> >
> > On what would you like to be advised? You cannot expect the same
> > performance from a CLR function when compared to a simple native T-SQL
> > equivalent. If there is a simple T-SQL equivalent, it will always
> > outperform a similar CLR function because of the overhead that is
> > involved in calling a CLR function.
> >
> > --
> > Gert-Jan
> > SQL Server MVP
> >
 >> Stay informed about: Performance CLR function 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
sql performance for function - hey all, the following link is an image of my query execution plan properties: http://home.comcast.net/~charity154/sqlpaste20080611a.htm i'm not sure i'm showing the right information but i was wondering if this image can tell someone if my function..

Case function in SQL Vs iff function in access - Hi I'm new to SQL and I'm having an issue with the Case function. I have a field which has the Amount in various currencies. The problem is that Yen has no decimal places so the flat files I use to pickup the data does not include any leading zero. ..

Convert VB function to SQL function - I have to convert VB function to SQL: Function CalcCompItem() Dim mValue1 Dim mValue2 Dim CompValue,CompPerc Select Case int(sCPExt) Case 0 mValue1 = rs("Tickets") Case 1 mValue1 = rs("ExtFee1") Case 2 mValue1 = rs...

Problem with Table Function calling another table function - Greetings, I have 2 inline table functions, one function is called by the other function. The 'Base' function takes 8 input parameters. It compiles fine and executes fine when you hard code the 8 input parameters. The other function calls the 'Base'....

best performance - What is ur opinion ? i've 2 model db. 1) CREATE TABLE [dbo].[TDTRANS] ( [ID_TRANS] [int] NOT NULL , [NU] [smallint] NOT NULL , [IDBRG] [int] NOT NULL , [KODELINK] [char] (1) COLLATE Latin1_General_CI_AS NULL , [NOLINK] [varchar] (15) COLLATE..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]