473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Speed up UDF

Hello all-
Given the following UDF, in sql 2000 can it be sped up, complied or
anything of the like. A query returning 300,000 + rows times out when
ran through the udf, inline case statements returns the rows in 5
seconds.
Thanks!
Jeff

CREATE FUNCTION dbo.TimeFormat
(
@input datetime,
@groupformat varchar(20) --DAY, WEEK, MONTH
)

RETURNS datetime

AS

BEGIN
declare @dtvar as datetime

if @groupformat = 'DAY'
set @dtvar = CAST(CONVERT(char(10), @input, 101) AS datetime)
else if @groupformat = 'WEEK'
set @dtvar = CAST(DATEADD([DAY], 1 - DATEPART(dw, CONVERT(char(10),
@input, 101)), CONVERT(char(10), @input, 101)) AS datetime)
else if @groupformat = 'MONTH'
set @dtvar = CAST(CONVERT(CHAR(6), @input, 112) + '01' AS datetime)
return @dtvar
END

Jun 22 '06 #1
9 1720
How about an inline table-valued UDF? And getting rid of all those CASTs
and CONVERTs... After all you're inputting a DATETIME and returning a
DATETIME:

CREATE FUNCTION dbo.TimeFormat
(
@input DATETIME,
@groupformat VARCHAR(20)
)
RETURNS TABLE
AS
RETURN
(
SELECT CASE @groupformat
WHEN 'DAY' THEN @input
WHEN 'WEEK' THEN DATEADD(day, 1 - DATEPART(dw, @input), @input)
WHEN 'MONTH' THEN DATEADD(day, -DATEPART(d, @input)+1, @input)
END AS [XDate]
)

SELECT XDate
FROM dbo.TimeFormat ('2006-06-22', 'DAY')

SELECT XDate
FROM dbo.TimeFormat ('2006-06-22', 'WEEK')

SELECT XDate
FROM dbo.TimeFormat ('2006-06-22', 'MONTH')

"ujjc001" <uj*****@gmail.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
Hello all-
Given the following UDF, in sql 2000 can it be sped up, complied or
anything of the like. A query returning 300,000 + rows times out when
ran through the udf, inline case statements returns the rows in 5
seconds.
Thanks!
Jeff

CREATE FUNCTION dbo.TimeFormat
(
@input datetime,
@groupformat varchar(20) --DAY, WEEK, MONTH
)

RETURNS datetime

AS

BEGIN
declare @dtvar as datetime

if @groupformat = 'DAY'
set @dtvar = CAST(CONVERT(char(10), @input, 101) AS datetime)
else if @groupformat = 'WEEK'
set @dtvar = CAST(DATEADD([DAY], 1 - DATEPART(dw, CONVERT(char(10),
@input, 101)), CONVERT(char(10), @input, 101)) AS datetime)
else if @groupformat = 'MONTH'
set @dtvar = CAST(CONVERT(CHAR(6), @input, 112) + '01' AS datetime)
return @dtvar
END

Jun 22 '06 #2
I allways recommend returning raw data by sql and doing formatting on
the client side.

Jun 23 '06 #3
ujjc001 (uj*****@gmail.com) writes:
Given the following UDF, in sql 2000 can it be sped up, complied or
anything of the like. A query returning 300,000 + rows times out when
ran through the udf, inline case statements returns the rows in 5
seconds.


Scalar UDFs can incur a large overhead in SQL 2000, and a lot of this
lies in the overhead for the call itself. So you are better off without
the function and using inline code.

In SQL 2005 you could write a UDF like this in C# or VB .Net, and you
could very well get performance than inline SQL. Overall, the overhead
cost for a call to a scalar UDF is lower in SQL 2005.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 23 '06 #4
how would I use an inline udf in an existing stored procedure? I would
need to pass in a field such as table.date below :

SELECT
(
SELECT XDate FROM TimeFormat (table.Date, 'Month')
) AS formattedXDate,
table.Title,
table2.Title AS title2.....

Mike C# wrote:
How about an inline table-valued UDF? And getting rid of all those CASTs
and CONVERTs... After all you're inputting a DATETIME and returning a
DATETIME:

CREATE FUNCTION dbo.TimeFormat
(
@input DATETIME,
@groupformat VARCHAR(20)
)
RETURNS TABLE
AS
RETURN
(
SELECT CASE @groupformat
WHEN 'DAY' THEN @input
WHEN 'WEEK' THEN DATEADD(day, 1 - DATEPART(dw, @input), @input)
WHEN 'MONTH' THEN DATEADD(day, -DATEPART(d, @input)+1, @input)
END AS [XDate]
)

SELECT XDate
FROM dbo.TimeFormat ('2006-06-22', 'DAY')

SELECT XDate
FROM dbo.TimeFormat ('2006-06-22', 'WEEK')

SELECT XDate
FROM dbo.TimeFormat ('2006-06-22', 'MONTH')

"ujjc001" <uj*****@gmail.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
Hello all-
Given the following UDF, in sql 2000 can it be sped up, complied or
anything of the like. A query returning 300,000 + rows times out when
ran through the udf, inline case statements returns the rows in 5
seconds.
Thanks!
Jeff

CREATE FUNCTION dbo.TimeFormat
(
@input datetime,
@groupformat varchar(20) --DAY, WEEK, MONTH
)

RETURNS datetime

AS

BEGIN
declare @dtvar as datetime

if @groupformat = 'DAY'
set @dtvar = CAST(CONVERT(char(10), @input, 101) AS datetime)
else if @groupformat = 'WEEK'
set @dtvar = CAST(DATEADD([DAY], 1 - DATEPART(dw, CONVERT(char(10),
@input, 101)), CONVERT(char(10), @input, 101)) AS datetime)
else if @groupformat = 'MONTH'
set @dtvar = CAST(CONVERT(CHAR(6), @input, 112) + '01' AS datetime)
return @dtvar
END


Jun 23 '06 #5
ujjc001 (uj*****@gmail.com) writes:
how would I use an inline udf in an existing stored procedure? I would
need to pass in a field such as table.date below :

SELECT
(
SELECT XDate FROM TimeFormat (table.Date, 'Month')
) AS formattedXDate,
table.Title,
table2.Title AS title2.....


Yeah, I asked myself he same thing. In SQL 2005 you could do it if you
use the CROSS APPLY operator, but in SQL 2000 there is not a way, as
far as I can see. Maybe Mike C# has some more tricks up his sleeve?

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 23 '06 #6
Nope, just wrapping it in another scalar UDF LOL. I misread it and didn't
realize he was passing them thar columns in.

"Erland Sommarskog" <es****@sommarskog.se> wrote in message
news:Xn**********************@127.0.0.1...
ujjc001 (uj*****@gmail.com) writes:
how would I use an inline udf in an existing stored procedure? I would
need to pass in a field such as table.date below :

SELECT
(
SELECT XDate FROM TimeFormat (table.Date, 'Month')
) AS formattedXDate,
table.Title,
table2.Title AS title2.....


Yeah, I asked myself he same thing. In SQL 2005 you could do it if you
use the CROSS APPLY operator, but in SQL 2000 there is not a way, as
far as I can see. Maybe Mike C# has some more tricks up his sleeve?

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx

Jun 23 '06 #7
Stu
Another alternative would be to modify a calendar table in such a way
as to return the values you want. You could then join on the calendar
table (using the date value as a lookup) and return the start day of
the week or month as needed.

Stu
ujjc001 wrote:
Hello all-
Given the following UDF, in sql 2000 can it be sped up, complied or
anything of the like. A query returning 300,000 + rows times out when
ran through the udf, inline case statements returns the rows in 5
seconds.
Thanks!
Jeff

CREATE FUNCTION dbo.TimeFormat
(
@input datetime,
@groupformat varchar(20) --DAY, WEEK, MONTH
)

RETURNS datetime

AS

BEGIN
declare @dtvar as datetime

if @groupformat = 'DAY'
set @dtvar = CAST(CONVERT(char(10), @input, 101) AS datetime)
else if @groupformat = 'WEEK'
set @dtvar = CAST(DATEADD([DAY], 1 - DATEPART(dw, CONVERT(char(10),
@input, 101)), CONVERT(char(10), @input, 101)) AS datetime)
else if @groupformat = 'MONTH'
set @dtvar = CAST(CONVERT(CHAR(6), @input, 112) + '01' AS datetime)
return @dtvar
END


Jun 24 '06 #8
for some reason that sounds painful. Well I went with the simple case
statement inline, which works nicely just takes up a lot more room in
the sp. Another note, I did need the cast and converts to get my date
in a nice format for reporting purposes. I don't recally my exact
reasoning why, but it would have been much more difficult in crystal to
format my report the way I needed by passing in the raw data or for
that mater, anything w/ time on the end. The cast converts remove the
time too.
Thanks for the help.
Jeff
Stu wrote:
Another alternative would be to modify a calendar table in such a way
as to return the values you want. You could then join on the calendar
table (using the date value as a lookup) and return the start day of
the week or month as needed.

Stu
ujjc001 wrote:
Hello all-
Given the following UDF, in sql 2000 can it be sped up, complied or
anything of the like. A query returning 300,000 + rows times out when
ran through the udf, inline case statements returns the rows in 5
seconds.
Thanks!
Jeff

CREATE FUNCTION dbo.TimeFormat
(
@input datetime,
@groupformat varchar(20) --DAY, WEEK, MONTH
)

RETURNS datetime

AS

BEGIN
declare @dtvar as datetime

if @groupformat = 'DAY'
set @dtvar = CAST(CONVERT(char(10), @input, 101) AS datetime)
else if @groupformat = 'WEEK'
set @dtvar = CAST(DATEADD([DAY], 1 - DATEPART(dw, CONVERT(char(10),
@input, 101)), CONVERT(char(10), @input, 101)) AS datetime)
else if @groupformat = 'MONTH'
set @dtvar = CAST(CONVERT(CHAR(6), @input, 112) + '01' AS datetime)
return @dtvar
END


Jun 26 '06 #9
Stu
Actually a calendar table is pretty simple to use, and very effecient
for situations like these. If you haven't used one before, it's very
simple to set up. Check out http://www.aspfaq.com/show.asp?id=2519

ujjc001 wrote:
for some reason that sounds painful. Well I went with the simple case
statement inline, which works nicely just takes up a lot more room in
the sp. Another note, I did need the cast and converts to get my date
in a nice format for reporting purposes. I don't recally my exact
reasoning why, but it would have been much more difficult in crystal to
format my report the way I needed by passing in the raw data or for
that mater, anything w/ time on the end. The cast converts remove the
time too.
Thanks for the help.
Jeff
Stu wrote:
Another alternative would be to modify a calendar table in such a way
as to return the values you want. You could then join on the calendar
table (using the date value as a lookup) and return the start day of
the week or month as needed.

Stu
ujjc001 wrote:
Hello all-
Given the following UDF, in sql 2000 can it be sped up, complied or
anything of the like. A query returning 300,000 + rows times out when
ran through the udf, inline case statements returns the rows in 5
seconds.
Thanks!
Jeff

CREATE FUNCTION dbo.TimeFormat
(
@input datetime,
@groupformat varchar(20) --DAY, WEEK, MONTH
)

RETURNS datetime

AS

BEGIN
declare @dtvar as datetime

if @groupformat = 'DAY'
set @dtvar = CAST(CONVERT(char(10), @input, 101) AS datetime)
else if @groupformat = 'WEEK'
set @dtvar = CAST(DATEADD([DAY], 1 - DATEPART(dw, CONVERT(char(10),
@input, 101)), CONVERT(char(10), @input, 101)) AS datetime)
else if @groupformat = 'MONTH'
set @dtvar = CAST(CONVERT(CHAR(6), @input, 112) + '01' AS datetime)
return @dtvar
END


Jun 27 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Yang Li Ke | last post by:
Hi guys, Is it possible to know the internet speed of the visitors with php? Thanx -- Yang
8
by: Rob Ristroph | last post by:
I have tried out PHP 5 for the first time (with assistance from this group -- thanks!). The people I was working with have a site that uses lots of php objects. They are having problems with...
34
by: Jacek Generowicz | last post by:
I have a program in which I make very good use of a memoizer: def memoize(callable): cache = {} def proxy(*args): try: return cache except KeyError: return cache.setdefault(args,...
28
by: Maboroshi | last post by:
Hi I am fairly new to programming but not as such that I am a total beginner From what I understand C and C++ are faster languages than Python. Is this because of Pythons ability to operate on...
52
by: Neuruss | last post by:
It seems there are quite a few projects aimed to improve Python's speed and, therefore, eliminate its main limitation for mainstream acceptance. I just wonder what do you all think? Will Python...
7
by: YAZ | last post by:
Hello, I have a dll which do some number crunching. Performances (execution speed) are very important in my application. I use VC6 to compile the DLL. A friend of mine told me that in Visual...
6
by: Ham | last post by:
Yeah, Gotto work with my VB.Net graphic application for days, do any possible type of code optimization, check for unhandled errors and finally come up with sth that can't process 2D graphics and...
6
by: Jassim Rahma | last post by:
I want to detect the internet speed using C# to show the user on what speed he's connecting to internet?
11
by: kyosohma | last post by:
Hi, We use a script here at work that runs whenever someone logs into their machine that logs various bits of information to a database. One of those bits is the CPU's model and speed. While...
4
by: nestle | last post by:
I have DSL with a download speed of 32MB/s and an upload speed of 8MB/s(according to my ISP), and I am using a router. My upload speed is always between 8MB/s and 9MB/s(which is above the max upload...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.