SQL Server doesn't have the statistics for the table variable?

后端 未结 2 997
伪装坚强ぢ
伪装坚强ぢ 2021-01-16 00:48

I found the MSDN says that \"table variables don\'t have statistics\"

https://msdn.microsoft.com/en-us/library/dd535534(v=sql.100).aspx

Also the following ar

2条回答
  •  误落风尘
    2021-01-16 00:56

    No SQL Server doesn't maintain statistics for table variables.

    There are three indexes on the table variable in your question and SQL Server inserts a corresponding three rows to sysidxstats

    Contains a row for each index or statistics for tables and indexed views

    These rows do have the 2 flag set in the status column indicating they are for statistics and so are returned by sys.stats but there is no corresponding statistics object.

    You can see this using the query here (requires connecting via the DAC)

    SELECT name,
           imageval
    FROM   tempdb.sys.stats AS s
           INNER JOIN tempdb.sys.sysobjvalues AS o
                   ON s.object_id = o.objid
                      AND s.stats_id = o.subobjid
    WHERE  s.object_id = @table_variable_id; 
    

    Which returns

    +--------------------------------+----------+
    |              name              | imageval |
    +--------------------------------+----------+
    | PK__#AD773B9__FFEE74513158C9C9 | NULL     |
    | IX_Indate                      | NULL     |
    | UQ__#AD773B9__DD5A978A62C2C478 | NULL     |
    +--------------------------------+----------+
    

    The imageval which would contain the actual statistics is NULL.

    This is nothing new. You also see the same in 2008 (though only for the PK and UQ as the inline index definition cannot be used there).

    The maintenance of rowcounts is done separately and does not need a full blown statistics object with histogram. And again this is not new.

提交回复
热议问题