473,473 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using Scalar functions in views

I have a view that contains a complex query. A few of the columns
call a function that returns a specific output. I also use a function
to do a join as well.

For example:

SELECT l.ID, dbo.getStatus(l.ID) AS statusID
FROM tableName A
LEFT OUTER JOIN dbo.Status_LKP s ON dbo.getStatus(l.Leg_ID) =
s.statusID

For 800 records, this query takes about 9 seconds. I realize that for
each record, the function is executed on a per row basis, so I am
looking for alternatives.

Does anyone know of other ways to accomplish something of the same?
Basically I would like to include UDFs in a query and use those UDFs
in the where and join clauses.

Thanks

Sep 19 '07 #1
2 9996
(mc*****@sympatico.ca) writes:
I have a view that contains a complex query. A few of the columns
call a function that returns a specific output. I also use a function
to do a join as well.

For example:

SELECT l.ID, dbo.getStatus(l.ID) AS statusID
FROM tableName A
LEFT OUTER JOIN dbo.Status_LKP s ON dbo.getStatus(l.Leg_ID) =
s.statusID

For 800 records, this query takes about 9 seconds. I realize that for
each record, the function is executed on a per row basis, so I am
looking for alternatives.

Does anyone know of other ways to accomplish something of the same?
Basically I would like to include UDFs in a query and use those UDFs
in the where and join clauses.
Scalar UDFs with data access in a join condition is sure recipe for poor
performance. You do best to incorporate the logic of the UDF in the view.

Also keep in mind that putting a column into an expression precludes the
usage of any index on that column.
--
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
Sep 19 '07 #2
If you need a UDF to be able to join to another table, then you have not
designed your schema properly. An ID does not come out of thin air. If
you have a relation between tables, you should register the foreign key
properly. So not only is it a bad idea to do this from a performance
point of view, but also from a design point of view.

If dbo.getStatus does a lookup in a related table, then you could simply
join with that table. If you don't want to write such an extra join, you
can move it to a view and select from the view (instead of the table).

--
Gert-Jan
mc*****@sympatico.ca wrote:
>
I have a view that contains a complex query. A few of the columns
call a function that returns a specific output. I also use a function
to do a join as well.

For example:

SELECT l.ID, dbo.getStatus(l.ID) AS statusID
FROM tableName A
LEFT OUTER JOIN dbo.Status_LKP s ON dbo.getStatus(l.Leg_ID) =
s.statusID

For 800 records, this query takes about 9 seconds. I realize that for
each record, the function is executed on a per row basis, so I am
looking for alternatives.

Does anyone know of other ways to accomplish something of the same?
Basically I would like to include UDFs in a query and use those UDFs
in the where and join clauses.

Thanks
Sep 20 '07 #3

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

Similar topics

7
by: Jim Geissman | last post by:
Take a table, where not all the columns are populated: CREATE TABLE #T (A int, B int, C int, D int) INSERT #T (A,B) VALUES (1,2) INSERT #T (A,B) VALUES (3,4) INSERT #T (A,B) VALUES (5,6)...
2
by: Martin MacRobert | last post by:
Hi, I'm trying to make a specialisation of a template function, so that the second parameter accepts scalar types only (int,double,float etc.). How can I do this without writing an explicit...
5
by: Matt | last post by:
I am working in a project where the business model is complex enough that many common retrieval functions become difficult to develop and maintain in single query statements or functions. I have...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
5
by: Bob Stearns | last post by:
When I run the following query with the two sections commented out, the response time is between 1 an 2 seconds; with the first indicated section enabled, the response goes up to 15 seconds even...
5
by: serge | last post by:
Is it generally or almost always better to have multiple small SPs and functions to return a result set instead of using a single big 1000+ lines SP? I have one SP for example that is 1000+...
43
by: markryde | last post by:
Hello, I saw in some open source projects a use of "!!" in "C" code; for example: in some header file #define event_pending(v) \ (!!(v)->vcpu_info->evtchn_upcall_pending & \...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
20
by: Rob Stevens | last post by:
Can someone tell me how to import the sqlite3.dll file into c#. I am pretty new to this, and I want to use sqlite. The problem is I don't have a clue on how to import the dll file so i can call...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.