• Home
  • About Us
  • Contact Us
  • Privacy Policy
  • Special Offers
Business Intelligence Info
  • Business Intelligence
    • BI News and Info
    • Big Data
    • Mobile and Cloud
    • Self-Service BI
  • CRM
    • CRM News and Info
    • InfusionSoft
    • Microsoft Dynamics CRM
    • NetSuite
    • OnContact
    • Salesforce
    • Workbooks
  • Data Mining
    • Pentaho
    • Sisense
    • Tableau
    • TIBCO Spotfire
  • Data Warehousing
    • DWH News and Info
    • IBM DB2
    • Microsoft SQL Server
    • Oracle
    • Teradata
  • Predictive Analytics
    • FICO
    • KNIME
    • Mathematica
    • Matlab
    • Minitab
    • RapidMiner
    • Revolution
    • SAP
    • SAS/SPSS
  • Humor

Memory optimized table variable and cardinality estimate

May 23, 2017   BI News and Info

In a previous blog, I talked about memory optimized table consumes memory until end of the batch.   In this blog, I want to make you aware of cardinality estimate of memory optimized table as we have had customers who called in for clarifications.  By default memory optimized table variable behaves the same way as disk based table variable. It will have 1 row as an estimate.   In disk based table variable, you can control estimate by using option (recompile) at statement level (see this blog) or use trace flag 2453. 

You can control the same behavior using the two approaches on memory optimized table variable if you use it in an ad hoc query or inside a regular TSQL stored procedure.  The behavior will be the same. This little repro will show the estimate is correct with option (recompile).

create database IMOLTP
go
ALTER DATABASE imoltp ADD FILEGROUP imoltp_mod CONTAINS MEMORY_OPTIMIZED_DATA 
go
ALTER DATABASE imoltp ADD FILE (name=’imoltp_mod1′, filename=’c:\sqldata\imoltp_mod2′) TO FILEGROUP imoltp_mod 
go

use IMOLTP
go

CREATE TYPE dbo.test_memory AS TABLE
(c1 INT NOT NULL INDEX ix_c1,
c2 CHAR(10))
WITH (MEMORY_OPTIMIZED=ON);


go

DECLARE @tv dbo.test_memory
set nocount on

declare @i int
set @i = 1
while @i < 10000
begin
    insert into @tv values (@i, ‘n’)
    set @i = @i + 1
end
set statistics xml on
–this will work and the etimate will be correct
select * from @tv t1 join @tv t2 on t1.c1=t2.c1 option (recompile, querytraceon 2453)
set statistics xml off
go

image thumb152 Memory optimized table variable and cardinality estimate

But the problem occurs when you use it inside a natively compiled stored procedure.  In this case, it will always estimate 1 row.  You can’t change it because natively compiled procedure doesn’t allow statement level recompile.  If you try to create a natively compiled procedure, you will get errors that disallow you to create the procedure.


create procedure test
with native_compilation, schemabinding 
as  
begin atomic with 
(transaction isolation level = snapshot, 
language = N’English’) 

DECLARE @tv dbo.test_memory
declare @i int
set @i = 1
while @i < 10000
begin
    insert into @tv values (@i, ‘n’)
    set @i = @i + 1
end
–you can’t add TF 3453 or recompile
select t1.c1, t2.c1 from @tv t1 join @tv t2 on t1.c1=t2.c1 option (recompile, querytraceon 2453)
end 
go


Msg 10794, Level 16, State 45, Procedure test, Line 17 [Batch Start Line 39]
The query hint ‘RECOMPILE’ is not supported with natively compiled modules.
Msg 10794, Level 16, State 45, Procedure test, Line 17 [Batch Start Line 39]
The query hint ‘QUERYTRACEON’ is not supported with natively compiled modules.

So that is the solution?   For natively compiled procedure, here are some advices

1. limit number of rows inserted into the memory optimized table variable.

2. if you are joining with memory optimized table variable that has lots of rows, consider use a schema_only memory optimized table

3. if  you know your data, you can potentially re-arrange the join and use option (force order) to get around it.

Jack Li |Senior Escalation Engineer | Microsoft SQL Server

twitter| pssdiag |Sql Nexus

Let’s block ads! (Why?)

CSS SQL Server Engineers

cardinality, Estimate, Memory, optimized, table, Variable
  • Recent Posts

    • Cashierless tech could detect shoplifting, but bias concerns abound
    • Misunderstood Loyalty
    • Pearl with a girl earring
    • Dynamics 365 Monthly Update-January 2021
    • Researchers propose Porcupine, a compiler for homomorphic encryption
  • Categories

  • Archives

    • January 2021
    • December 2020
    • November 2020
    • October 2020
    • September 2020
    • August 2020
    • July 2020
    • June 2020
    • May 2020
    • April 2020
    • March 2020
    • February 2020
    • January 2020
    • December 2019
    • November 2019
    • October 2019
    • September 2019
    • August 2019
    • July 2019
    • June 2019
    • May 2019
    • April 2019
    • March 2019
    • February 2019
    • January 2019
    • December 2018
    • November 2018
    • October 2018
    • September 2018
    • August 2018
    • July 2018
    • June 2018
    • May 2018
    • April 2018
    • March 2018
    • February 2018
    • January 2018
    • December 2017
    • November 2017
    • October 2017
    • September 2017
    • August 2017
    • July 2017
    • June 2017
    • May 2017
    • April 2017
    • March 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    • October 2016
    • September 2016
    • August 2016
    • July 2016
    • June 2016
    • May 2016
    • April 2016
    • March 2016
    • February 2016
    • January 2016
    • December 2015
    • November 2015
    • October 2015
    • September 2015
    • August 2015
    • July 2015
    • June 2015
    • May 2015
    • April 2015
    • March 2015
    • February 2015
    • January 2015
    • December 2014
    • November 2014
© 2021 Business Intelligence Info
Power BI Training | G Com Solutions Limited