Solved

Query / Criteria ?

Posted on 2016-09-14
6
52 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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…

737 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