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