Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Why is trivial plan creating statistics

$
0
0
Hi all As far as I understand a trivial plan is a plan that there is no need or a way to optimize it. The most known example for a trivial plan is running a simple select statement that has no group by or order by and the table has no indexes at all. What I don't understand is why is the server creating statistics if a query is a trivial query and the server can not optimize it in any case. Bellow is a script that shows the the server does create statistics for a trivial plan.[code]--creating a new table and inserting data into itWITH MyCTE as ( SELECT TOP 10000 ROW_NUMBER() OVER (ORDER BY message_id) as RowNum FROM sys.messages )SELECT RowNumINTO MyTableFROM MyCTEgo--check that there are no statistics for this tableSELECT * FROM sys.stats WHERE object_id = OBJECT_ID('dbo.MyTable')go--running a query on the new table. Be sure to include the actual query plan--and check in the plan's properties. You'll see that this plan is a trivial planSELECT RowNumFROM MyTableWHERE RowNum = 1go--Now the table does have statisticsSELECT * FROM sys.stats WHERE object_id = OBJECT_ID('dbo.MyTable')goDROP TABLE dbo.MyTable[/code]Thank you for your answersAdi

Viewing all articles
Browse latest Browse all 3145

Trending Articles