Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

scalar function brings performance problems

Posted on 2010-08-15
4
Medium Priority
?
415 Views
Last Modified: 2012-05-10
Hi experts, a friend told me that a scalar function brings performance problems because it runs row by row, is that true? but does not use scalar function then how would you do?. ULR know of any that speaks this kind of thing
0
Comment
Question by:enrique_aeo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 2

Assisted Solution

by:coalanexpert
coalanexpert earned 332 total points
ID: 33443071
Depending on the cost of the query that the function actually executes it might affect performance. The book says to try to use inline table functions instead, but again if your scalar function is simple, no need to complicate things.
Here a reference on how to use inline table valued functions

http://stackoverflow.com/questions/800017/why-do-sql-server-scalar-valued-functions-get-slower

0
 

Author Comment

by:enrique_aeo
ID: 33443117
I have this function
CREATE FUNCTION dbo.SumVta(@ProductID int)
RETURNS int
AS
BEGIN
  DECLARE @ret int
  SELECT @ret = SUM(Quantity)
  FROM [Order Details]
  WHERE ProductID = @ProductID
  IF (@ret IS NULL)
  SET @ret = 0
RETURN @ret
END
how could I make a table inline Functions?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 336 total points
ID: 33443484
>because it runs row by row

yes.

your function would require a (clustered) index on ProductID on table order details to run fastest, unless you can build up the "sum_quantity" column in the products table directly, and keep that updated with triggers (real-time) or regular jobs (scheduled updates)
0
 
LVL 10

Assisted Solution

by:joriszwaenepoel
joriszwaenepoel earned 332 total points
ID: 33445631
I think you can solve this with a JOIN and a GROUP BY:

Something like:

SELECT Order.OrderID, OD.ProductID, SUM(OD.Quantity) AS SumVta
FROM [Order]
INNER JOIN [Order Details] OD
ON Order.OrderID = OD.OrderID
GROUP BY Order.OrderID, OD.ProductID

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question