SQL Server does not maintain statistics for table variable and it always estimates 1 output row. Microsoft changed this behavior post release of SQL Server 2019. Though I am not able to find much information about those changes.
Below is my sample code. Note, that dbo.Integers just a local user table that contains 100k integers in incremental order.
declare @myTestTable table (
ID int not null,
Symbol varchar(100) not null primary key clustered,
Price decimal(9,2) null,
EntityID int not null
);
insert @myTestTable (ID, Symbol, Price, EntityID)
select ID = n.Num,
Symbol = cast(hashbytes('SHA', cast(n.Num as varchar(30))) as varchar(100)),
Price = rand(n.Num) * 1000,
EntityID = case when n.Num < 4000 then 1 else n.Num end
from dbo.Integers n
where n.Num < 5000;
select * from @myTestTable
where EntityID = 1
Here is the execution plan details.
It is clear from the above stats, that estimated number of rows 70.70 is square root of total number of rows 4999. I changed number of rows but same calculation applied.
As I was not able to find proper documentation on it. Could you please help me understand what changes Microsoft has brought in recent upgrades related to table variable statistics and estimation and if estimated rows are always square root of total number of rows in case of table variable?