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

x
?
Solved

Query / Criteria ?

Posted on 2016-09-14
6
Medium Priority
?
59 Views
Last Modified: 2016-09-20
See the attached Function.
tblGuides is about 5M rows. tblPOSupplierItemList 377.
When I run this function it takes < 1 second and returns 732 rows.
But when I add this criteria to the Where it takes more than 10 minutes and returns 75 rows. It does not matter where I add this criteria, inside the cte, outside or even in ssms (select * from dbo.POItemsBySupplierList(.5) <Where adding it here>). Is there a way to tell SQL to only apply this criteria to the result of 732?

Criteria:
((g.[New Net Pr] - ((sl.NetPrice ) + @ShippingCost)) / ((sl.NetPrice ) + @ShippingCost)) > 0

ALTER FUNCTION [dbo].[POItemsBySupplierList] 
(	

	@ShippingCost smallmoney = 0

)
RETURNS TABLE 
AS
RETURN 

With CTE As(

Select	g.ISBN, 
		g.Title,
		g.Publisher,
		g.[new net pr]								As	GuidePrice,
		dbo.QtyLeft_Guide(g.GuideNumber, g.ISBN)	As	QtyLeft,
		sl.NetPrice									As	Cost,
		sl.Discount,
		sl.LeadTimeDays,
		@ShippingCost								As ShippingCost
		

	From	tblGuides g
	Join	tblGuideHeader gh			On g.GuideNumber = gh.GuideNumber
	Join	tblPOSupplierItemList sl	On g.ISBN = sl.ISBN

	where	(DateDiff(day, GetDate(), gh.FinalShipDate) >= sl.LeadTimeDays)
		And
			(dbo.QtyLeft_Guide(g.GuideNumber, g.ISBN) > 0)
		--And
		--	((g.[New Net Pr] - ((sl.NetPrice ) + @ShippingCost)) / ((sl.NetPrice ) + @ShippingCost)) > 0
)
select  * from cte
--where ((GuidePrice - ((Cost ) + @ShippingCost)) / ((Cost ) + @ShippingCost)) > 0

Open in new window

0
Comment
Question by:Jess31
[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
  • 3
  • 2
6 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41798185
0
 
LVL 1

Author Comment

by:Jess31
ID: 41798420
I don't know. How can I force it form doing parameter sniffing?
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41798427
as explained in the link, declare a variable inside your function (declare @internalShippingCost smallmoney), set its value (set @internalShippingCost  = @ShippingCost ) and use the variable instead of the parameter.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:Jess31
ID: 41798749
Sorry, I didn't explain. But I can't get the syntax correct. When I try adding a declare it won't compile.
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 2000 total points
ID: 41798771
probably because when you created the UDF, you created it as an inline function (instead of a multi-statements function).
0
 
LVL 32

Expert Comment

by:Pawan Kumar
ID: 41799089
Use statement level compilation or use dynamic string to over come parameter sniffing problem.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

604 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