Case of using filtered statistics

Published Jan 15 2019 01:15 PM 73 Views
Microsoft
First published on MSDN on Sep 28, 2010
Technorati Tags: Performance

SQL Server 2008 introduces a new feature called filtered statistics.  When used properly, it can dramatically improve cardinality estimate.   Let’s use an example below to illustrate how cardinality estimate can be incorrect and how filtered statistics can improve this situation.


We have two tables.  Region has only 2 rows.  Sales table have 1001 rows but only 1 row has id of 0.  The rest of it have id of 1’s.


Table Region


id name
0 Dallas
1 New York

Table Sales


id detail
0 0
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 1000


Now let’s look at query “select detail from Region join Sales on Region.id = Sales.id where name='Dallas'”.    From human eye perspective, we immediately know that only one row would qualify.  If we look at Dallas there is only one row in Region table and one row in Sales table.   But from optimizer perspective, it does not know that when the query is compiled and before query is executed.  In order to know that, basically SQL would have to execute the query half way and filter out values for Dallas and take the id of 0 and  then evaluate how many rows are there in table Sales.  In other words, it would require incremental execution.


If you execute the query, you will get a plan like this.  Note that the nested loop estimated 500.5 rows but only 1 row actually was retrieved.



Now let’s see what happens if we create a statistics on Region.id but put a filter on name (“Dallas”).  Here is the statement “create statistics Region_stats_id on Region (id) where name = 'Dallas'”.


Now if you execute the same select statement (select detail from Region join Sales on Region.id = Sales.id where name='Dallas'), the cardinality estimate is correct as shown below for the nested loop join.



What happened here is the filtered statistics (create statistics Region_stats_id on Region (id) where name = 'Dallas') is used for optimization.  When SQL optimizes the query, it sees there is a statistics that matches the where clause.  It then discovers there is only 1 id of 0 and thus is able to do a correct estimate.


Correct cardinality estimate is very import for complex joins as it affects join order and join types dramatically.


Here is a complete demo:


drop table Region
go
drop table Sales
go


create table Region(id int, name nvarchar(100))
go
create table Sales(id int, detail int)
go
create clustered index d1 on Region(id)
go
create index ix_Region_name on Region(name)
go
create statistics ix_Region_id_name on Region(id, name)
go
create clustered index ix_Sales_id_detail on Sales(id, detail)
go


-- only two values in this table as lookup or dim table
insert Region values(0, 'Dallas')
insert Region values(1, 'New York')
go


set nocount on
-- Sales is skewed
insert Sales values(0, 0)
declare @i int
set @i = 1
while @i <= 1000 begin
insert Sales  values (1, @i)
set @i = @i + 1
end
go


update statistics Region with fullscan
update statistics Sales with fullscan
go


set statistics profile on
go
--note that this query will over estimate
-- it estimate there will be 500.5 rows
select detail from Region join Sales on Region.id = Sales.id where name='Dallas' option (recompile)
--this query will under estimate
-- this query will also estimate 500.5 rows in fact 1000 rows returned
select detail from Region join Sales on Region.id = Sales.id where name='New York' option (recompile)
go


set statistics profile off
go


create statistics Region_stats_id on Region (id)
where name = 'Dallas'
go
create statistics  Region_stats_id2 on Region (id)
where name = 'New York'
go


set statistics profile on
go
--now the estimate becomes accurate (1 row) because
select detail from Region join Sales on Region.id = Sales.id where name='Dallas' option (recompile)


--the estimate becomes accurate (1000 rows) because stats Region_stats_id2 is used to evaluate
select detail from Region join Sales on Region.id = Sales.id where name='New York' option (recompile)
go


set statistics profile off

%3CLINGO-SUB%20id%3D%22lingo-sub-316219%22%20slang%3D%22en-US%22%3ECase%20of%20using%20filtered%20statistics%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-316219%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Sep%2028%2C%202010%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CDIV%20id%3D%22scid%3A0767317B-992E-4b12-91E0-4F059A8CECA8%3A8c48fd17-1a07-44c9-8802-6904bc65c663%22%3ETechnorati%20Tags%3A%20%3CA%20href%3D%22http%3A%2F%2Ftechnorati.com%2Ftags%2FPerformance%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3E%20Performance%3C%2FA%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CP%3ESQL%20Server%202008%20introduces%20a%20new%20feature%20called%20filtered%20statistics.%26nbsp%3B%20When%20used%20properly%2C%20it%20can%20dramatically%20improve%20cardinality%20estimate.%26nbsp%3B%26nbsp%3B%20Let%E2%80%99s%20use%20an%20example%20below%20to%20illustrate%20how%20cardinality%20estimate%20can%20be%20incorrect%20and%20how%20filtered%20statistics%20can%20improve%20this%20situation.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EWe%20have%20two%20tables.%26nbsp%3B%20Region%20has%20only%202%20rows.%26nbsp%3B%20Sales%20table%20have%201001%20rows%20but%20only%201%20row%20has%20id%20of%200.%26nbsp%3B%20The%20rest%20of%20it%20have%20id%20of%201%E2%80%99s.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ETable%20Region%3C%2FP%3E%3CBR%20%2F%3E%3CTABLE%3E%0A%20%20%20%3CTBODY%3E%3CTR%3E%0A%20%20%20%20%3CTD%3Eid%3C%2FTD%3E%0A%20%20%20%20%3CTD%3Ename%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E0%3C%2FTD%3E%0A%20%20%20%20%3CTD%3EDallas%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E1%3C%2FTD%3E%0A%20%20%20%20%3CTD%3ENew%20York%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%3C%2FTBODY%3E%3C%2FTABLE%3E%3CBR%20%2F%3E%3CP%3ETable%20Sales%3C%2FP%3E%3CBR%20%2F%3E%3CTABLE%3E%0A%20%20%20%3CTBODY%3E%3CTR%3E%0A%20%20%20%20%3CTD%3Eid%3C%2FTD%3E%0A%20%20%20%20%3CTD%3Edetail%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E0%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E0%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E1%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E1%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E1%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E2%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E1%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E3%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E1%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E4%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E1%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E5%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E1%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E6%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E1%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E7%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E1%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E8%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E1%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E9%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E1%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E10%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E1%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E11%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E1%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E12%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E1%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E13%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E1%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E14%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E1%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E15%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E1%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E16%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%E2%80%A6%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%E2%80%A6%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E1%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E1000%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%3C%2FTBODY%3E%3C%2FTABLE%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ENow%20let%E2%80%99s%20look%20at%20query%20%E2%80%9Cselect%20detail%20from%20Region%20join%20Sales%20on%20Region.id%20%3D%20Sales.id%20where%20name%3D'Dallas'%E2%80%9D.%26nbsp%3B%26nbsp%3B%26nbsp%3B%20From%20human%20eye%20perspective%2C%20we%20immediately%20know%20that%20only%20one%20row%20would%20qualify.%26nbsp%3B%20If%20we%20look%20at%20Dallas%20there%20is%20only%20one%20row%20in%20Region%20table%20and%20one%20row%20in%20Sales%20table.%26nbsp%3B%26nbsp%3B%20But%20from%20optimizer%20perspective%2C%20it%20does%20not%20know%20that%20when%20the%20query%20is%20compiled%20and%20before%20query%20is%20executed.%26nbsp%3B%20In%20order%20to%20know%20that%2C%20basically%20SQL%20would%20have%20to%20execute%20the%20query%20half%20way%20and%20filter%20out%20values%20for%20Dallas%20and%20take%20the%20id%20of%200%20and%26nbsp%3B%20then%20evaluate%20how%20many%20rows%20are%20there%20in%20table%20Sales.%26nbsp%3B%20In%20other%20words%2C%20it%20would%20require%20incremental%20execution.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EIf%20you%20execute%20the%20query%2C%20you%20will%20get%20a%20plan%20like%20this.%26nbsp%3B%20Note%20that%20the%20nested%20loop%20estimated%20500.5%20rows%20but%20only%201%20row%20actually%20was%20retrieved.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66505i68EFE21D75D66BCA%22%20%2F%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ENow%20let%E2%80%99s%20see%20what%20happens%20if%20we%20create%20a%20statistics%20on%20Region.id%20but%20put%20a%20filter%20on%20name%20(%E2%80%9CDallas%E2%80%9D).%26nbsp%3B%20Here%20is%20the%20statement%20%E2%80%9Ccreate%20statistics%20Region_stats_id%20on%20Region%20(id)%20where%20name%20%3D%20'Dallas'%E2%80%9D.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ENow%20if%20you%20execute%20the%20same%20select%20statement%20(select%20detail%20from%20Region%20join%20Sales%20on%20Region.id%20%3D%20Sales.id%20where%20name%3D'Dallas')%2C%20the%20cardinality%20estimate%20is%20correct%20as%20shown%20below%20for%20the%20nested%20loop%20join.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66506i0D5104AD9924CAC7%22%20%2F%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EWhat%20happened%20here%20is%20the%20filtered%20statistics%20(create%20statistics%20Region_stats_id%20on%20Region%20(id)%20where%20name%20%3D%20'Dallas')%20is%20used%20for%20optimization.%26nbsp%3B%20When%20SQL%20optimizes%20the%20query%2C%20it%20sees%20there%20is%20a%20statistics%20that%20matches%20the%20where%20clause.%26nbsp%3B%20It%20then%20discovers%20there%20is%20only%201%20id%20of%200%20and%20thus%20is%20able%20to%20do%20a%20correct%20estimate.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ECorrect%20cardinality%20estimate%20is%20very%20import%20for%20complex%20joins%20as%20it%20affects%20join%20order%20and%20join%20types%20dramatically.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EHere%20is%20a%20complete%20demo%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Edrop%20table%20Region%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20drop%20table%20Sales%20%3CBR%20%2F%3E%20go%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Ecreate%20table%20Region(id%20int%2C%20name%20nvarchar(100))%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20create%20table%20Sales(id%20int%2C%20detail%20int)%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20create%20clustered%20index%20d1%20on%20Region(id)%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20create%20index%20ix_Region_name%20on%20Region(name)%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20create%20statistics%20ix_Region_id_name%20on%20Region(id%2C%20name)%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20create%20clustered%20index%20ix_Sales_id_detail%20on%20Sales(id%2C%20detail)%20%3CBR%20%2F%3E%20go%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E--%20only%20two%20values%20in%20this%20table%20as%20lookup%20or%20dim%20table%20%3CBR%20%2F%3E%20insert%20Region%20values(0%2C%20'Dallas')%20%3CBR%20%2F%3E%20insert%20Region%20values(1%2C%20'New%20York')%20%3CBR%20%2F%3E%20go%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Eset%20nocount%20on%20%3CBR%20%2F%3E%20--%20Sales%20is%20skewed%20%3CBR%20%2F%3E%20insert%20Sales%20values(0%2C%200)%20%3CBR%20%2F%3E%20declare%20%40i%20int%20%3CBR%20%2F%3E%20set%20%40i%20%3D%201%20%3CBR%20%2F%3E%20while%20%40i%20%26lt%3B%3D%201000%20begin%20%3CBR%20%2F%3E%20insert%20Sales%26nbsp%3B%20values%20(1%2C%20%40i)%20%3CBR%20%2F%3E%20set%20%40i%20%3D%20%40i%20%2B%201%20%3CBR%20%2F%3E%20end%20%3CBR%20%2F%3E%20go%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Eupdate%20statistics%20Region%20with%20fullscan%20%3CBR%20%2F%3E%20update%20statistics%20Sales%20with%20fullscan%20%3CBR%20%2F%3E%20go%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Eset%20statistics%20profile%20on%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20--note%20that%20this%20query%20will%20over%20estimate%20%3CBR%20%2F%3E%20--%20it%20estimate%20there%20will%20be%20500.5%20rows%20%3CBR%20%2F%3E%20select%20detail%20from%20Region%20join%20Sales%20on%20Region.id%20%3D%20Sales.id%20where%20name%3D'Dallas'%20option%20(recompile)%20%3CBR%20%2F%3E%20--this%20query%20will%20under%20estimate%20%3CBR%20%2F%3E%20--%20this%20query%20will%20also%20estimate%20500.5%20rows%20in%20fact%201000%20rows%20returned%20%3CBR%20%2F%3E%20select%20detail%20from%20Region%20join%20Sales%20on%20Region.id%20%3D%20Sales.id%20where%20name%3D'New%20York'%20option%20(recompile)%20%3CBR%20%2F%3E%20go%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Eset%20statistics%20profile%20off%20%3CBR%20%2F%3E%20go%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Ecreate%20statistics%20Region_stats_id%20on%20Region%20(id)%20%3CBR%20%2F%3E%20where%20name%20%3D%20'Dallas'%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20create%20statistics%26nbsp%3B%20Region_stats_id2%20on%20Region%20(id)%20%3CBR%20%2F%3E%20where%20name%20%3D%20'New%20York'%20%3CBR%20%2F%3E%20go%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Eset%20statistics%20profile%20on%20%3CBR%20%2F%3E%20go%20%3CBR%20%2F%3E%20--now%20the%20estimate%20becomes%20accurate%20(1%20row)%20because%20%3CBR%20%2F%3E%20select%20detail%20from%20Region%20join%20Sales%20on%20Region.id%20%3D%20Sales.id%20where%20name%3D'Dallas'%20option%20(recompile)%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E--the%20estimate%20becomes%20accurate%20(1000%20rows)%20because%20stats%20Region_stats_id2%20is%20used%20to%20evaluate%20%3CBR%20%2F%3E%20select%20detail%20from%20Region%20join%20Sales%20on%20Region.id%20%3D%20Sales.id%20where%20name%3D'New%20York'%20option%20(recompile)%20%3CBR%20%2F%3E%20go%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Eset%20statistics%20profile%20off%3C%2FP%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-316219%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Sep%2028%2C%202010%20Technorati%20Tags%3A%20PerformanceSQL%20Server%202008%20introduces%20a%20new%20feature%20called%20filtered%20statistics.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-316219%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPeformance%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Jan 15 2019 01:15 PM
Updated by: