Updating a table from a join


20-Jun-2017 15:53

As mentioned before, if you rebuild all your indexes then all the index statistics will also be updated by scanning all the rows on the table.Then you just need to update your column statistics by running an UPDATE STATISTICS WITH FULLSCAN, COLUMNS statement.So depending on your maintenance jobs and scripts several scenarios can exist.

Some other more complicated scenarios include when you have jobs which rebuild your indexes based on their fragmentation level.

REBUILD statement, will update only index statistics with the equivalent of using WITH FULLSCAN.

Rebuilding indexes does not update any column statistics.

One of the questions I was asked recently while speaking at the Los Angeles SQL Server Professionals Group and the Orange County SQL Server User Group (*) was regarding the order in which jobs like rebuilding indexes or updating statistics should be executed as part of the database maintenance activities.

In general you should consider the following important points, focusing in the fact that there are two kinds of statistics: index and column statistics.

Added)); USE [master] GO CREATE DATABASE [My School] GO USE [My School] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Course]([Course ID] [nvarchar](10) NOT NULL, [Year] [smallint] NOT NULL, [Title] [nvarchar](100) NOT NULL, [Credits] [int] NOT NULL, [Department ID] [int] NOT NULL, CONSTRAINT [PK_Course] PRIMARY KEY CLUSTERED ( [Course ID] ASC, [Year] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Department]([Department ID] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](50) NOT NULL, [Budget] [money] NOT NULL, [Start Date] [datetime] NOT NULL, [Administrator] [int] NULL, CONSTRAINT [PK_Department] PRIMARY KEY CLUSTERED ( [Department ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY] GO INSERT [dbo].[Course] ([Course ID], [Year], [Title], [Credits], [Department ID]) VALUES (N' C1045', 2012, N' Calculus', 4, 7) INSERT [dbo].[Course] ([Course ID], [Year], [Title], [Credits], [Department ID]) VALUES (N' C1061', 2012, N' Physics', 4, 1) INSERT [dbo].[Course] ([Course ID], [Year], [Title], [Credits], [Department ID]) VALUES (N' C2021', 2012, N' Composition', 3, 2) INSERT [dbo].[Course] ([Course ID], [Year], [Title], [Credits], [Department ID]) VALUES (N' C2042', 2012, N' Literature', 4, 2) SET IDENTITY_INSERT [dbo].[Department] ON INSERT [dbo].[Department] ([Department ID], [Name], [Budget], [Start Date], [Administrator]) VALUES (1, N' Engineering', 350000.0000, CAST(0x0000999C00000000 AS Date Time), 2) INSERT [dbo].[Department] ([Department ID], [Name], [Budget], [Start Date], [Administrator]) VALUES (2, N' English', 120000.0000, CAST(0x0000999C00000000 AS Date Time), 6) INSERT [dbo].[Department] ([Department ID], [Name], [Budget], [Start Date], [Administrator]) VALUES (4, N' Economics', 200000.0000, CAST(0x0000999C00000000 AS Date Time), 4) INSERT [dbo].[Department] ([Department ID], [Name], [Budget], [Start Date], [Administrator]) VALUES (7, N' Mathematics', 250024.0000, CAST(0x0000999C00000000 AS Date Time), 3) SET IDENTITY_INSERT [dbo].[Department] OFF ALTER TABLE [dbo].[Course] WITH CHECK ADD CONSTRAINT [FK_Course_Department] FOREIGN KEY([Department ID]) REFERENCES [dbo].[Department] ([Department ID]) GO ALTER TABLE [dbo].[Course] CHECK CONSTRAINT [FK_Course_Department] GO using System; using System.