During the check of New Cardinality Estimator in 2014, I found a strange thing and have one confusion let me define my problem first. I was using below code to generate the database and insert some data in SQL Server 2014.
Create database CETEST2014 go USE CETEST2014 go create table xtTraceTest ( custcode int identity(10001,1) Primary Key, balance int, state_name varchar(50), country varchar(50) ) go insert into xtTraceTest(balance,state_name,country) values(RAND(),'UP','INDIA') go 1000 insert into xtTraceTest(balance,state_name,country) values(RAND(),'HARYANA','INDIA') go 1233 insert into xtTraceTest(balance,state_name,country) values(RAND(),'KERALA','INDIA') go 677 insert into xtTraceTest(balance,state_name,country) values(RAND(),'CALABRIA','ITALY') go 1872 insert into xtTraceTest(balance,state_name,country) values(RAND(),'GOA','INDIA') go 4534 insert into xtTraceTest(balance,state_name,country) values(RAND(),'MP','INDIA') go 2763 insert into xtTraceTest(balance,state_name,country) values(RAND(),'GANGSU','CHINA') go 86 insert into xtTraceTest values(RAND(),'ASSAM','INDIA') go 123 create nonclustered index IX_xtTraceTest on xtTraceTest(country,state_name) include (balance) go --then i ran below query with include actual execution plan Select custcode,balance from xtTraceTest where country='INDIA' and state_name='GOA'
as per execution plan... estimated number of rows are 4157 and actual number of rows 4534. this execution plan was using index seek on nonclustered index..
below statistics histogram created due to automatic stats creation for non clustered index creation [under new CE]
IN My case histogram contain:
Name Updated Rows Rows Sampled Steps Density Average Key Length String Index
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IX_xtTraceTest Jun 23 2014 9:04PM 12288 12288 3 0 13.06315 YES 12288
All Density Average Length Columns
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0.3333333 5 country
0.125 9.06315 country, state_name
8.138021E-05 13.06315 country, state_name, custcode
so from histogram stats, density vector second row [0.125 9.06315 country, state_name ]
total estimated number of rows for above select query should be = density * total number of rows = 0.125 * 12288 = 1536
if you will use the same query with legacy cardinalty estimator then that will show you the estimated number of rows = 1536
so my question here is : is the histogram still created based on Legacy cardinality estimator? even i have created database on SQL Server 2014 with compatibility level 120 . however when i am running the select query then it is using new cadinality estimator and estimated number of rows are 4157 while as per the created histogram it shuold be 1536
am i missing something here or is it an issue?
thanks:
prince rastogi