Monday, February 22, 2016

SQL Server Histogram and SqlCmd non-visual view of Cache

SqlCmd command line is non-visual ( so could not see Index scan vs seek or Table Scan) so may have to use SQL Management Studio.

About statistics:
(1) Statistics have Index info, Density Vector and Histogram. sp_helpstatistics N'dbo.mytable',N'ALL'
(2) DBCC_Statistics tname, idxname can use idxname from sp_helpindex. sp_helpstatistic must use ALL or tell us no stats.
(3) Histogram tell us EQ_ROWS, RANGE_ROWS, AVG_RANGE_ROWS. It is generated by Server automatically or on demand
(4) @var used in statement => unknown to histogram and  server ends up using density vector
(5) Graphic execution plan actual vs est two => how well histogram get used good or bad.
(6) Histogram updates
        Set Auto_create_statistics on (20% + 500 changes trigger updates)
        update_statistics
        sp_update_stat, sp_auto_statistics tname, sp_create_statistics
         
SqlCmd non-visual View Cache may have a simple and quick view of how SQL Server understand your statement.

(1) @var in ad hoc batch => unknown and no plan
(2) Forced Cache plan in exec sp_executesql @execStr,"@v int", 1234
(3) select * from member where lastName like '%e%' => non parameterized unsafe, pollution of plan.

here is the stored proc to view Cache

create proc dbo.SeeCache(@str NVARCHAR(4000))
as
    select st.text,qs.execution_count,qs.query_plan_hash
    from sys.dm_exec_query_stats as [qs]
      cross apply sys.dm_exec_sql_text
       (qs.sql_handle) as st
      cross apply sys.dm_exec_query_plan
       (qs.plan_handle) as p
where st.text like @str

so SeeCache '%lastname%' would output sql text some hash pattern and how many time plan get used.

No comments:

Post a Comment