Solved

scalar function brings performance problems

Posted on 2010-08-15
4
406 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
4 Comments
 
LVL 2

Assisted Solution

by:coalanexpert
coalanexpert earned 83 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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 84 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 83 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

In this article I will describe the Backup & Restore 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 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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now