Solved

scalar function brings performance problems

Posted on 2010-08-15
4
407 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

20 Experts available now in Live!

Get 1:1 Help Now