Sample Header Ad - 728x90

why sql server is not reusing plan in my adhoc query?

0 votes
2 answers
112 views
Seems my understanding of query plan is not correct. If I run this query first time and check the query plan cache. I see one plan cached. declare @codes varchar(max)='BHVD7,BHVDE,BHVDF' SELECT OFST010020 AS [OFST010020], OFST730010 AS [OFST730010], Identifier AS [Identifier] FROM STATIC_FUNDSHARECLASSUNIT_NEXT as f WITH(NOLOCK) Inner join STATIC_FUNDSHARECLASSUNITEXTENSION_NEXT as fex WITH(NOLOCK) on fex.Identifier2 = f.Identifier WHERE OFST900174 in (select value from string_split(@codes,',')) If I just change the code paramter and run the query again in the same SSMS window, for example declare @codes varchar(max)='BHVD7,BHVDF' SELECT OFST010020 AS [OFST010020], OFST730010 AS [OFST730010], Identifier AS [Identifier] FROM STATIC_FUNDSHARECLASSUNIT_NEXT as f WITH(NOLOCK) Inner join STATIC_FUNDSHARECLASSUNITEXTENSION_NEXT as fex WITH(NOLOCK) on fex.Identifier2 = f.Identifier WHERE OFST900174 in (select value from string_split(@codes,',')) I thought the query plan would have 1 row and the useCount would be 2. But in the query plan cache, I actually saw two plans. Each plan's sql text is actually the full text (starting with declare @codes). Am I doing something wrong or ad hoc query doesn't really re-use plan even parameter is involved?
Asked by jerry xu (63 rep)
Nov 5, 2024, 09:38 PM
Last activity: Nov 6, 2024, 03:27 AM