Solved

Query / Criteria ?

Posted on 2016-09-14
6
43 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 69

Expert Comment

by:Éric Moreau
Comment Utility
0
 

Author Comment

by:Jess31
Comment Utility
I don't know. How can I force it form doing parameter sniffing?
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Jess31
Comment Utility
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 69

Accepted Solution

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

Expert Comment

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

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

10 Experts available now in Live!

Get 1:1 Help Now