SQL Server: Adding a column increases table size more than expected -


to migrate new schema, doing 2 things table:

  1. changing varchar column nvarchar
  2. adding bigint column

to test how affects size, generated 180,000 rows. varchar column changing set null on every row.

here results of sp_spaceused (all sizes in kb):

          rows     reserved     data   index_size  unused           ----     --------     ----   ----------  ------ before    180000     110976     43168       67288     520 after     180000     120320     52536       67296     488 

so data grew more 9mb. have expected grow 1.4 mb, because added 8-byte numeric column.

if create table , generate data, size looks more expect:

          rows     reserved     data   index_size  unused           ----     --------     ----   ----------  ------         180000       117760    46184        71352     224 

so there adding column causes sp_spaceused report more data in use added?

no 1 explained why happened. dave gugg gave great hint.

sql stores rows in pages. (that 8k, not exactly) , row must fit 1 page (not really, given long varchars can stored on separate page using pointer - yours null, lets ignore now.)

the pages allocated in clusters.

when inserted rows had nice fresh set of pages , clusters , rows neat.

as make change, particularly adding column, sql must update every row.

updating row requires changing 1 or more pages - because changed row may no longer fit on same page. in fact, because of way rollback works, think sql likes move changed rows new page.

so, tightly arranged rows got distributed , took many more pages , clusters. on time many changes, averages out. not every update adds pages - adding column making each row larger , less fit prior neighbors.

we cannot more specific because did not provide schema. if had, we'd have sense of expected rows per page before , after change.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -