Solved

Query / Criteria ?

Posted on 2016-09-14
6
49 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
  • 3
  • 2
6 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41798185
0
 

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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 500 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 28

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

821 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