SQL Server CE: Multiple single-column statistics

Published Sep 06 2021 08:56 PM 977 Views
Microsoft

The SQL Server Query Optimizer is a cost-based Query Optimizer. For one specific query and tables,  SQL Server creates the query plan based on the estimated rows. Hence, understanding how SQL Server calculate the estimated rows helps you troubleshoot query plan related issues.

 

I’m going to start a series of posts talking about SQL Server CE(Legacy/New).

 

The way that SQL Server calculates the estimated row of one single predicate is straightforward and simple.

Well, it becomes a little bit complex when SQL Server handles more than one predicates. Unlike the one single predicate, SQL Server has different strategies for Legacy CE and New CE.

In Legacy CE, SQL Server assumes that data distribution on different columns are independent; In New CE, SQL Server assumes that data distribution on different columns are correlated. 

For short, the estimated rows returned by New CE is generally greater than the rows returned by Legacy CE.

 

Here are examples , adventure 2019 OLTP  database is used in this example

 

------- Data prepare-------------------------------------------

alter database [AdventureWorks2019] set compatibility_level=150

go

use [AdventureWorks2019]

go

if exists(select 1 from sys.tables where name='SalesOrderDetail')

drop table SalesOrderDetail

go

select * into SalesOrderDetail from Sales.SalesOrderDetail---- import all the data into new table SalesOrderDetail.

go

------- Data prepare-------------------------------------------

 

-----  I'll use following T-SQL query, which return 533 rows, to demonstrate

select * from SalesOrderDetail where ProductID=711 and UnitPrice=20.1865

 

 

Legacy CE

1.use trace 9481 to make sure old CE is used

select * from SalesOrderDetail where ProductID=711 and UnitPrice=20.1865 option(querytraceon 9481,recompile)--force old CE

Liwei_13-1630986877333.png

 

 

 

2.The estimated rows is 38, while the actual returned rows is 533. Let's dive into to see how the estimated row 38 calculated. Please note, if your estimated row if different than mine, please update the stats with fullscan.

update statistics SalesOrderDetail with fullscan.

 

3.Show the statistics

select *From sys.stats where object_id=object_id('SalesOrderDetail')

As where clause has two different columns, two different columns are created automatically

Liwei_14-1630986877338.png

 

 

4.The two predicates are connected by AND, with selectivity P0 and P1. The combined selectivity is:P0*P1. Let's dive into

1)Estimated row: P0*P1*Card.

        P0 is the selectivity of first column, P1 is the selectivity of second column. The order does not mater.

        Card is the total number of rows ,which is 121317 in this case.

       there are two columns in where clause: ProductID and UnitPrice

2)Selectivity of ProductID(P0): 0.02547046

ProductId:711

Estimation rows:3090

Selectivity: 3090/121317=0.02547046

 

dbcc show_statistics(SalesOrderDetail,_WA_Sys_00000005_0E8E2250)

Liwei_15-1630986877346.png

 

3)Selectivity of UnitPrice: 0.01230660

UnitPrice:20.1865

Estimation rows:1493

Selectivity :1493/121317=0.01230660

 

dbcc show_statistics(SalesOrderDetail,_WA_Sys_00000007_0E8E2250)

Liwei_16-1630986877354.png

 

 

4)Total estimation:P0*P1*'table cardinality' =0.02547046*0.01230660*121317=38.0273914872384120, is rounded down to 38.

 

5.If there are more than two predicates, just multiples the predicate:P0*P1*P1….*PN.

 

 

New CE

1.The formula of combined selectivity in New CE is totally different:: P0 *P1^(1/2) *P2^(1/4) *P3^(1/8)

2.P0,P1,P2,P3 are the selectivity of each predicate where P0<P1<P2<P3.

3.If there are more than 4 columns in the where clause, only  the first 4 are counted, the rest of them are ignored.

In practice, Please use following formula instead if there are more than 2 predicate.

Liwei_17-1630986877355.png

 

 

 

 

1.Run follow querying in New CE, which is the default setting for compatibility level 150

select * from SalesOrderDetail where ProductID=711 and UnitPrice=20.1865

Liwei_18-1630986877358.png

 

 

 

2.The estimated rows is 238,  much closer than OLD CE value 38. Let's dive into to see how the estimated row 238 calculated.

 

3.Let's see how it's calculated.

1)In this case, we have two predicates, so the combined predicate is P0*P1^(1/2)

 

2)Selectivity of ProductID: 0.02547046

ProductId:711

Estimation rows:3090

Selectivity: 3090/121317=0.02547046

 

dbcc show_statistics(SalesOrderDetail,_WA_Sys_00000005_0E8E2250)

Liwei_19-1630986877369.png

 

3)Selectivity of UnitPrice: 0.01230660

UnitPrice:20.1865

Estimation rows:1493

Selectivity :1493/121317=0.01230660

 

dbcc show_statistics(SalesOrderDetail,_WA_Sys_00000007_0E8E2250)

Liwei_20-1630986877374.png

 

 

 

4)Because selectivity of UnitPrice( 0.0125629219) is less than selectivity of ProductID(0.02547046)

P0=0.01230660

P1=0.02547046

Combined selectivity :

Liwei_23-1630986877376.png=0.01230660*sqrt(0.02547046)=0.00196406781623722

Total estimated row is:

Liwei_24-1630986877376.png

 

 =0.00196406781623722*121317=238.274815262451,is rounded down to 238.

 

 

 

Homework:

----T-SQL

use AdventureWorks2019

go

IF exists(select 1 from sys.tables where name='Product' and schema_id=schema_id('dbo'))

drop table Product

go

IF exists(select 1 from sys.tables where name='SalesOrderDetail' and schema_id=schema_id('dbo'))

drop table SalesOrderDetail

go

select * into SalesOrderDetail  from Sales.SalesOrderDetail

GO

--create five statistics

create statistics I_ProductID on SalesOrderDetail(ProductID) with fullscan

create statistics I_orderqty on SalesOrderDetail(orderqty) with fullscan

create statistics I_SpecialOfferid on SalesOrderDetail(SpecialOfferid) with fullscan

create statistics I_modifiedDate on SalesOrderDetail(modifiedDate) with fullscan

create statistics I_unitpricediscount on SalesOrderDetail(unitpricediscount) with fullscan

 

 

 

 

1.A DBA is doing some tests, he finds that the estimated rows is reduced when he puts more columns in the where clause.  For example, the estimated row of following query is 474

select *From SalesOrderDetail where  SpecialOfferid=1  and orderqty=1 and modifiedDate='2013-06-30 00:00:00.000'  and ProductId=708  option(recompile,querytraceon 2363)

The estimated row is reduced to 4 after he put another column in the where clause: unitpricediscount=0.30

Liwei_26-1630986877382.png

 

 

 

As tests continues, the customers finds that it's not always true. After he replaces the unitpricediscount=0.30  with unitpricediscount=0.00, the estimated row is still 474, not decreased. Why ?

Liwei_27-1630986877387.png

 

 

2.Please test the same query using Legacy CE, do you get the same result?

 

 

Related posts:

SQL Server CE: Multiple single-column statistics connected by OR

 

 

2 Comments
Senior Member

Thanks for this resource, Liwei!

It's definitely very helpful.

Quick question: what are the implications when upgrading from pre-2014 versions to 2014 and newer? As in, do statistics need to be recalculated? Indexes rebuilt? etc.

Microsoft

Statistics and histogram are not changed after upgrading, the way SQL Server uses the statistics and histogram are changed, and may get different Estimated rows and create different query plan based on the Estimated Row.

 

Hence need to test the workload in new CE. If there are performance degradation and it's due to CE difference, you may need to switch back to Legacy CE(query level or database level)

%3CLINGO-SUB%20id%3D%22lingo-sub-2726969%22%20slang%3D%22en-US%22%3EMultiple%20single-column%20statistics%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2726969%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20SQL%20Server%20Query%20Optimizer%20is%20a%20cost-based%20Query%20Optimizer.%20For%20one%20specific%20query%20and%20tables%2C%20%26nbsp%3BSQL%20Server%20creates%20the%20query%20plan%20based%20on%20the%20estimated%20rows.%20Hence%2C%20understanding%20how%20SQL%20Server%20calculate%20the%20estimated%20rows%20helps%20you%20troubleshoot%20query%20plan%20related%20issues.%3C%2FP%3E%0A%3CP%3EI%E2%80%99m%20going%20to%20start%20a%20series%20of%20posts%20talking%20about%20SQL%20Server%20CE(Legacy%2FNew).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20way%20that%20SQL%20Server%20calculates%20the%20estimated%20row%20of%20one%20single%20predicate%20is%20straightforward%20and%20simple.%3C%2FP%3E%0A%3CP%3EWell%2C%20it%20becomes%20a%20little%20bit%20complex%20when%20SQL%20Server%20handles%20more%20than%20one%20predicates.%20Unlike%20the%20one%20single%20predicate%2C%20SQL%20Server%20has%20different%20strategies%20for%20Legacy%20CE%20and%20New%20CE.%3C%2FP%3E%0A%3CP%3EIn%20Legacy%20CE%2C%20SQL%20Server%20assumes%20that%20data%20distribution%20on%20different%20columns%20are%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fperformance%2Fcardinality-estimation-sql-server%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Eindependent%3C%2FA%3E%3B%20In%20New%20CE%2C%20SQL%20Server%20assumes%20that%20data%20distribution%20on%20different%20columns%20are%20correlated.%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20short%2C%20the%20estimated%20rows%20returned%20by%20New%20CE%20is%20generally%20greater%20than%20the%20rows%20returned%20by%20Legacy%20CE.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20are%20examples%20%2C%20adventure%202019%20OLTP%26nbsp%3B%20database%20is%20used%20in%20this%20example%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E-------%20Data%20prepare-------------------------------------------%3C%2FP%3E%0A%3CP%3Ealter%20database%20%5BAdventureWorks2019%5D%20set%20compatibility_level%3D150%3C%2FP%3E%0A%3CP%3Ego%3C%2FP%3E%0A%3CP%3Euse%20%5BAdventureWorks2019%5D%3C%2FP%3E%0A%3CP%3Ego%3C%2FP%3E%0A%3CP%3Eif%20exists(select%201%20from%20sys.tables%20where%20name%3D'SalesOrderDetail')%3C%2FP%3E%0A%3CP%3Edrop%20table%20SalesOrderDetail%3C%2FP%3E%0A%3CP%3Ego%3C%2FP%3E%0A%3CP%3Eselect%20*%20into%20SalesOrderDetail%20from%20Sales.SalesOrderDetail----%20import%20all%20the%20data%20into%20new%20table%20SalesOrderDetail.%3C%2FP%3E%0A%3CP%3Ego%3C%2FP%3E%0A%3CP%3E-------%20Data%20prepare-------------------------------------------%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E-----%26nbsp%3B%20I'll%20use%20following%20T-SQL%20query%2C%20which%20return%20533%20rows%2C%20to%20demonstrate%3C%2FP%3E%0A%3CP%3Eselect%20*%20from%20SalesOrderDetail%20where%20ProductID%3D711%20and%20UnitPrice%3D20.1865%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ELegacy%20CE%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E1.use%20trace%209481%20to%20make%20sure%20old%20CE%20is%20used%3C%2FP%3E%0A%3CP%3Eselect%20*%20from%20SalesOrderDetail%20where%20ProductID%3D711%20and%20UnitPrice%3D20.1865%20option(querytraceon%209481%2Crecompile)--force%20old%20CE%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Liwei_13-1630986877333.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308411i41F772D1F3466AC2%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Liwei_13-1630986877333.png%22%20alt%3D%22Liwei_13-1630986877333.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E2.The%20estimated%20rows%20is%2038%2C%20while%20the%20actual%20returned%20rows%20is%20533.%20Let's%20dive%20into%20to%20see%20how%20the%20estimated%20row%2038%20calculated.%20Please%20%3CSTRONG%3Enote%3C%2FSTRONG%3E%2C%20if%20your%20estimated%20row%20if%20different%20than%20mine%2C%20please%20update%20the%20stats%20with%20fullscan.%3C%2FP%3E%0A%3CP%3Eupdate%20statistics%20SalesOrderDetail%20with%20fullscan.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E3.Show%20the%20statistics%3C%2FP%3E%0A%3CP%3Eselect%20*From%20sys.stats%20where%20object_id%3Dobject_id('SalesOrderDetail')%3C%2FP%3E%0A%3CP%3EAs%20where%20clause%20has%20two%20different%20columns%2C%20two%20different%20columns%20are%20created%20automatically%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Liwei_14-1630986877338.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308410i2345DF0C1531237C%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Liwei_14-1630986877338.png%22%20alt%3D%22Liwei_14-1630986877338.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E4.The%20two%20predicates%20are%20connected%20by%20AND%2C%20with%20selectivity%20P0%20and%20P1.%20The%20combined%20selectivity%20is%3A%3CSTRONG%3EP0*P1%3C%2FSTRONG%3E.%20Let's%20dive%20into%3C%2FP%3E%0A%3CP%3E1)Estimated%20row%3A%20P0*P1*Card.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20P0%20is%20the%20selectivity%20of%20first%20column%2C%20P1%20is%20the%20selectivity%20of%20second%20column.%20The%20order%20does%20not%20mater.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Card%20is%20the%20total%20number%20of%20rows%20%2Cwhich%20is%20121317%20in%20this%20case.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20there%20are%20two%20columns%20in%20where%20clause%3A%20ProductID%20and%20UnitPrice%3C%2FP%3E%0A%3CP%3E2)Selectivity%20of%20ProductID(P0)%3A%200.02547046%3C%2FP%3E%0A%3CP%3EProductId%3A711%3C%2FP%3E%0A%3CP%3EEstimation%20rows%3A3090%3C%2FP%3E%0A%3CP%3ESelectivity%3A%203090%2F121317%3D0.02547046%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Edbcc%20show_statistics(SalesOrderDetail%2C_WA_Sys_00000005_0E8E2250)%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Liwei_15-1630986877346.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308412i36F67E4ED408A7AF%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Liwei_15-1630986877346.png%22%20alt%3D%22Liwei_15-1630986877346.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E3)Selectivity%20of%20UnitPrice%3A%200.01230660%3C%2FP%3E%0A%3CP%3EUnitPrice%3A20.1865%3C%2FP%3E%0A%3CP%3EEstimation%20rows%3A1493%3C%2FP%3E%0A%3CP%3ESelectivity%20%3A1493%2F121317%3D0.01230660%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Edbcc%20show_statistics(SalesOrderDetail%2C_WA_Sys_00000007_0E8E2250)%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Liwei_16-1630986877354.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308415i18D7D1B64B1CDEAE%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Liwei_16-1630986877354.png%22%20alt%3D%22Liwei_16-1630986877354.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E4)Total%20estimation%3AP0*P1*'table%20cardinality'%20%3D0.02547046*0.01230660*121317%3D38.0273914872384120%2C%20is%20rounded%20down%20to%2038.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E5.If%20there%20are%20more%20than%20two%20predicates%2C%20just%20multiples%20the%20predicate%3AP0*P1*P1%E2%80%A6.*PN.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ENew%20CE%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E1.The%20%3CSTRONG%3Eformula%20of%20combined%20%3C%2FSTRONG%3Eselectivity%20in%20New%20CE%20is%20totally%20different%3A%3A%20P0%20*P1%5E(1%2F2)%20*P2%5E(1%2F4)%20*P3%5E(1%2F8)%3C%2FP%3E%0A%3CP%3E2.P0%2CP1%2CP2%2CP3%20are%20the%20selectivity%20of%20each%20predicate%20where%20P0%3CP1%3E%3CP2%3E%3CP3.%3E%3C%2FP3.%3E%3C%2FP2%3E%3C%2FP1%3E%3C%2FP%3E%0A%3CP%3E3.If%20there%20are%20more%20than%20%3CSTRONG%3E4%3C%2FSTRONG%3E%20columns%20in%20the%20where%20clause%2C%20only%26nbsp%3B%20the%20first%204%20are%20counted%2C%20the%20rest%20of%20them%20are%20ignored.%3C%2FP%3E%0A%3CP%3EIn%20practice%2C%20Please%20use%20following%20formula%20instead%20if%20there%20are%20more%20than%202%20predicate.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Liwei_17-1630986877355.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308413i82E7E2F2E49B1B42%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Liwei_17-1630986877355.png%22%20alt%3D%22Liwei_17-1630986877355.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E1.Run%20follow%20querying%20in%20New%20CE%2C%20which%20is%20the%20default%20setting%20for%20compatibility%20level%20150%3C%2FP%3E%0A%3CP%3Eselect%20*%20from%20SalesOrderDetail%20where%20ProductID%3D711%20and%20UnitPrice%3D20.1865%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Liwei_18-1630986877358.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308414i99AA32979A45F6DF%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Liwei_18-1630986877358.png%22%20alt%3D%22Liwei_18-1630986877358.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E2.The%20estimated%20rows%20is%20238%2C%26nbsp%3B%20much%20closer%20than%20OLD%20CE%20value%2038.%20Let's%20dive%20into%20to%20see%20how%20the%20estimated%20row%20238%20calculated.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E3.Let's%20see%20how%20it's%20calculated.%3C%2FP%3E%0A%3CP%3E1)In%20this%20case%2C%20we%20have%20two%20predicates%2C%20so%20the%20combined%20predicate%20is%20P0*P1%5E(1%2F2)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E2)Selectivity%20of%20ProductID%3A%200.02547046%3C%2FP%3E%0A%3CP%3EProductId%3A711%3C%2FP%3E%0A%3CP%3EEstimation%20rows%3A3090%3C%2FP%3E%0A%3CP%3ESelectivity%3A%203090%2F121317%3D0.02547046%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Edbcc%20show_statistics(SalesOrderDetail%2C_WA_Sys_00000005_0E8E2250)%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Liwei_19-1630986877369.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308417i36BC4CDD14427E28%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Liwei_19-1630986877369.png%22%20alt%3D%22Liwei_19-1630986877369.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E3)Selectivity%20of%20UnitPrice%3A%200.01230660%3C%2FP%3E%0A%3CP%3EUnitPrice%3A20.1865%3C%2FP%3E%0A%3CP%3EEstimation%20rows%3A1493%3C%2FP%3E%0A%3CP%3ESelectivity%20%3A1493%2F121317%3D0.01230660%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Edbcc%20show_statistics(SalesOrderDetail%2C_WA_Sys_00000007_0E8E2250)%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Liwei_20-1630986877374.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308418i3F5FA52D14E8796B%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Liwei_20-1630986877374.png%22%20alt%3D%22Liwei_20-1630986877374.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E4)Because%20selectivity%20of%20UnitPrice(%200.0125629219)%20is%20less%20than%20selectivity%20of%20ProductID(0.02547046)%3C%2FP%3E%0A%3CP%3EP0%3D0.01230660%3C%2FP%3E%0A%3CP%3EP1%3D0.02547046%3C%2FP%3E%0A%3CP%3ECombined%20selectivity%20%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Liwei_23-1630986877376.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308421i0183F398333AB671%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Liwei_23-1630986877376.png%22%20alt%3D%22Liwei_23-1630986877376.png%22%20%2F%3E%3C%2FSPAN%3E%3D0.01230660*sqrt(0.02547046)%3D0.00196406781623722%3C%2FP%3E%0A%3CP%3ETotal%20estimated%20row%20is%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Liwei_24-1630986877376.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308422i1EFB2C476731D9C5%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Liwei_24-1630986877376.png%22%20alt%3D%22Liwei_24-1630986877376.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3D0.00196406781623722*121317%3D238.274815262451%2Cis%20rounded%20down%20to%20238.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EHomework%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E----T-SQL%3C%2FP%3E%0A%3CP%3Euse%20AdventureWorks2019%3C%2FP%3E%0A%3CP%3Ego%3C%2FP%3E%0A%3CP%3EIF%20exists(select%201%20from%20sys.tables%20where%20name%3D'Product'%20and%20schema_id%3Dschema_id('dbo'))%3C%2FP%3E%0A%3CP%3Edrop%20table%20Product%3C%2FP%3E%0A%3CP%3Ego%3C%2FP%3E%0A%3CP%3EIF%20exists(select%201%20from%20sys.tables%20where%20name%3D'SalesOrderDetail'%20and%20schema_id%3Dschema_id('dbo'))%3C%2FP%3E%0A%3CP%3Edrop%20table%20SalesOrderDetail%3C%2FP%3E%0A%3CP%3Ego%3C%2FP%3E%0A%3CP%3Eselect%20*%20into%20SalesOrderDetail%26nbsp%3B%20from%20Sales.SalesOrderDetail%3C%2FP%3E%0A%3CP%3EGO%3C%2FP%3E%0A%3CP%3E--create%20five%20statistics%3C%2FP%3E%0A%3CP%3Ecreate%20statistics%20I_ProductID%20on%20SalesOrderDetail(ProductID)%20with%20fullscan%3C%2FP%3E%0A%3CP%3Ecreate%20statistics%20I_orderqty%20on%20SalesOrderDetail(orderqty)%20with%20fullscan%3C%2FP%3E%0A%3CP%3Ecreate%20statistics%20I_SpecialOfferid%20on%20SalesOrderDetail(SpecialOfferid)%20with%20fullscan%3C%2FP%3E%0A%3CP%3Ecreate%20statistics%20I_modifiedDate%20on%20SalesOrderDetail(modifiedDate)%20with%20fullscan%3C%2FP%3E%0A%3CP%3Ecreate%20statistics%20I_unitpricediscount%20on%20SalesOrderDetail(unitpricediscount)%20with%20fullscan%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E1.A%20DBA%20is%20doing%20some%20tests%2C%20he%20finds%20that%20the%20estimated%20rows%20is%20reduced%20when%20he%20puts%20more%20columns%20in%20the%20where%20clause.%26nbsp%3B%20For%20example%2C%20the%20estimated%20row%20of%20following%20query%20is%20474%3C%2FP%3E%0A%3CP%3Eselect%20*From%20SalesOrderDetail%20where%26nbsp%3B%20SpecialOfferid%3D1%26nbsp%3B%20and%20orderqty%3D1%20and%20modifiedDate%3D'2013-06-30%2000%3A00%3A00.000'%26nbsp%3B%20and%20ProductId%3D708%26nbsp%3B%20option(recompile%2Cquerytraceon%202363)%3C%2FP%3E%0A%3CP%3EThe%20estimated%20row%20is%20reduced%20to%204%20after%20he%20put%20another%20column%20in%20the%20where%20clause%3A%20%3CSTRONG%3Eunitpricediscount%3D0.30%20%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Liwei_26-1630986877382.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308424iC84AE39C6E49891A%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Liwei_26-1630986877382.png%22%20alt%3D%22Liwei_26-1630986877382.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20tests%20continues%2C%20the%20customers%20finds%20that%20it's%20not%20always%20true.%20After%20he%20replaces%20the%20%3CSTRONG%3Eunitpricediscount%3D0.30%26nbsp%3B%20%3C%2FSTRONG%3Ewith%20%3CSTRONG%3Eunitpricediscount%3D0.00%3C%2FSTRONG%3E%2C%20the%20estimated%20row%20is%20still%20474%2C%20not%20decreased.%20%3CSTRONG%3EWhy%20%3F%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Liwei_27-1630986877387.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308425iD6D8E8105994E24D%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Liwei_27-1630986877387.png%22%20alt%3D%22Liwei_27-1630986877387.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E2.Please%20test%20the%20same%20query%20using%20Legacy%20CE%2C%20do%20you%20get%20the%20same%20result%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2726969%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPerformance%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2727055%22%20slang%3D%22en-US%22%3ERe%3A%20SQL%20Server%20CE%3A%20Multiple%20single-column%20statistics%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2727055%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20for%20this%20resource%2C%20Liwei!%3C%2FP%3E%3CP%3EIt's%20definitely%20very%20helpful.%3C%2FP%3E%3CP%3EQuick%20question%3A%20what%20are%20the%20implications%20when%20upgrading%20from%20pre-2014%20versions%20to%202014%20and%20newer%3F%20As%20in%2C%20do%20statistics%20need%20to%20be%20recalculated%3F%20Indexes%20rebuilt%3F%20etc.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2729846%22%20slang%3D%22en-US%22%3ERe%3A%20SQL%20Server%20CE%3A%20Multiple%20single-column%20statistics%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2729846%22%20slang%3D%22en-US%22%3E%3CP%3EStatistics%20and%20histogram%20are%20not%20changed%20after%20upgrading%2C%20the%20way%20SQL%20Server%20uses%20the%20statistics%20and%20histogram%20are%20changed%2C%20and%20may%20get%20different%20Estimated%20rows%20and%20create%20different%20query%20plan%20based%20on%20the%20Estimated%20Row.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHence%20need%20to%20test%20the%20workload%20in%20new%20CE.%20If%20there%20are%20performance%20degradation%20and%20it's%20due%20to%20CE%20difference%2C%20you%20may%20need%20to%20switch%20back%20to%20Legacy%20CE(query%20level%20or%20database%20level)%3C%2FP%3E%3C%2FLINGO-BODY%3E
Co-Authors
Version history
Last update:
‎Sep 07 2021 11:34 AM
Updated by: