Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T/F: Using IF statements in a procedure produces multiple plans

In responses to this question, KM said

if you are on or above SQL Server 2005, you can use IFs to have multiple queries in the same procedure and each will have an query plan saved for it (equiv to a procedure for each on older versions), see the article in my answer or this link to proper section: sommarskog.se/dyn-search-2005.html#IF

HLGEM added

You can do that in earlier versions of SQL Server as well.

I read that section of Sommarskog's excellent article but did not see anything about multiple plans.

In later research, I read a quote here from Gert Drapers:

Because SQL Server allows only one execution plan per stored procedure ...

I don't know the date of that original article or the version of SQL Server he is referring to.

Does anyone have a reliable reference that discusses this or, better yet, a test that proves that this is true?

like image 675
Rob Garrison Avatar asked Dec 09 '22 19:12

Rob Garrison


2 Answers

Update #2:

I have one more step to add that ought to make everything clearer. After generating plan information, run the following statement (with the correct plan handle) to look at the ShowPlan XML.

DECLARE @val as VARBINARY(64)
-- NOTE: Replace the Hex string with the current plan_handle !
SET @val = CONVERT(VARBINARY(64), 0x05001300045A3D02B801BE11000000000000000000000000)
SELECT * FROM sys.dm_exec_query_plan(@val)

Looking at the generated XML reveals that there are 2 QueryPlan elements, 2 or more StmtSimple/StmtCond elements, and only 1 Batch overall. As gbn mentions- there is a difference between "Execution Plans" and "Query Plans". This seems to make it crystal clear what parts we are actually viewing in all the sys.dm_ queries.

sys.dm_exec_query_stats at MSDN, SQL 2008

sys.dm_exec_query_plan at MSDN, SQL 2008

So with all this information, the plan_handle returned is the Execution Plan; and the parts are Query Plan items.

--

Update:

After Andrew's comment, I retested and indeed the query plan handles are the same. And, just to be sure- I created a duplicate of the stored procedure, changing only the name, and reran my test against that stored procedure. This also caused a new set of query parts to be generated, that shared the same plan handle.

So, gbn's answer appears to be correct, at least with what I've tested here. Interesting stuff.

--

The latter quote from Gert Drapers appears to be false- here is my test. I'm using SQL 2005 here. In my test, I see 2 query plans generated for different parts of the same stored procedure.

First, I created two tables, tblTag1 and tblTagWithGUID. I made them somewhat similar so that my stored procedure can alternate between either table and return results with an identical result table layout.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
-- Table #1, tblTag1
CREATE TABLE [dbo].[tblTag1](
  [id] [int] IDENTITY(1,1) NOT NULL,
  [createDate] [datetime] NOT NULL CONSTRAINT [DF_tblTag1_createDate]  DEFAULT (getdate()),
  [someTag] [varchar](100) NOT NULL,
 CONSTRAINT [PK_tblTag1] PRIMARY KEY CLUSTERED 
(
  [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
-- Table #2, tblTagWithGUID
CREATE TABLE [dbo].[tblTagWithGUID](
  [id] [int] IDENTITY(1,1) NOT NULL,
  [createDate] [datetime] NOT NULL CONSTRAINT [DF_tblTagWithGUID_createDate]  DEFAULT (getdate()),
  [someTag] [varchar](100) NOT NULL,
  [someGUID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_tblTagWithGUID_someGUID]  DEFAULT (newid()),
 CONSTRAINT [PK_tblTagWithGUID] PRIMARY KEY CLUSTERED 
(
  [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

Second, the stored procedure. In the stored procedure I select from one or the other table depending on the argument.

CREATE PROCEDURE spLoadTags
@Pick AS BIT = NULL
AS
BEGIN

IF @Pick = 0
SELECT id, createDate, someTag FROM tblTag1

IF @Pick = 1
SELECT id, createDate, someTag FROM tblTagWithGUID

END

I added some data to each table- then ran the stored procedure a few dozen times with 0 or 1 as the argument.

Next, I ran this query to check up on the generated query plans. I'm sorry if anyone is offended with my sloppiness here- this isn't production code, of course.

WITH PlanData AS
(
SELECT
(SELECT SUBSTRING(text, statement_start_offset/2 + 1,
(CASE WHEN statement_end_offset = -1 
THEN LEN(CONVERT(nvarchar(MAX),text)) * 2 
ELSE statement_end_offset 
END - statement_start_offset)/2)
FROM sys.dm_exec_sql_text(sql_handle) WHERE [text] like '%SELECT id, createDate, someTag FROM tblTag%') AS query_text,
plan_handle
FROM sys.dm_exec_query_stats  
)
SELECT 
DISTINCT
execution_count, 
PlanData.query_text, 
sys.dm_exec_query_stats.plan_handle
FROM sys.dm_exec_query_stats, PlanData 
WHERE 
sys.dm_exec_query_stats.plan_handle = PlanData.plan_handle and 
PlanData.query_text IS NOT NULL
ORDER BY execution_count DESC

When I run this query, I see a bunch of plans, but because I've run the stored procedure a few dozen times, the distinct parts end up at the top.

execution_count query_text  plan_handle
96  SELECT id, createDate, someTag FROM tblTag1 0x05001200045A3D02B8613E13000000000000000000000000
96  SELECT id, createDate, someTag FROM tblTagWithGUID  0x05001200045A3D02B8613E13000000000000000000000000

I've only included those two rows, but hopefully this is straightforward enough where someone else can verify my results. You may see other rows if you are using the SQL management tool like I am; presumably caused by browsing tables or other activity.

like image 133
meklarian Avatar answered Mar 07 '23 09:03

meklarian


We're all correct :-)

  • A "query plan" has at most 2 entries in cache: one serial and one parallel

  • Each user has their own "Execution context" that runs the plan

  • Plans differ if objects are not qualified

So, what you may think is a plan is not because tables are not qualified with schema (which is the same in SQL Server 2000 , 2005 and 2008)

From MSDN/BOL "Execution Plan Caching and Reuse"

Edit:

"Obtaining Statement-Level Query Plans" from MS blog

like image 34
gbn Avatar answered Mar 07 '23 09:03

gbn