I'm doing tests to better understand those minimal technical details about tables, pages, and etc.
I'm using sp_spaceused to better understand this with INT values ( I will do a lot of more tests with other datatypes, But I think the logic behind each datatype is the same).
OK. First I created a simple table with id int
.
create table Aliens( id int)
Inserting only 1 value (Insert into table value (1)
), I have this:
| name | rows | reserved | data | index_size | Unused |
|------|------|----------|------|------------|--------|
| id | 1 | 16 KB | 8 KB | 8 KB | 0 KB |
OK. I can understand that we have here USED 8KB
because this is the size of a page. correct? But why does SQL server reserves 16KB for this?
Then, I delete this row, and the result is the same:
| name | rows | reserved | data | index_size |
|------|------|----------|------|------------|
| id | 1 | 16 KB | 8 KB | 8 KB |
This is because I didn't rebuild the table. After rebuilding the table:
| name | rows | reserved | data | index_size | unused |
|------|------|----------|------|------------|--------|
| id | 0 | 72 KB | 8 KB | 8 KB | 56 KB |
Why now I have more reserved ( 72KB )? I deleted data and rebuilded. Now, is my table bigger? Is this because of log? and why is unused 56KB? 72-56 is 16. Is this because DATA and INDEX are separated? my table has no PK, nor indexes.
Now, when I insert 286 values ( all 1's, with go 286):
| name | rows | reserved | data | index_size | unused |
|------|------|----------|-------|------------|--------|
| id | 286 | 24 KB | 16 KB | 8 KB | 0 KB |
Now I can see data = 16kb because There's so much information, that the SQL is now using 2 pages. But why index_size is still 8kb?
and How about NULL VALUES? according to This question here, null values on int field takes 4 bytes. a Char(10) takes 10 bytes. So, null values store the same amount as the datatype itself? Unless we are using sparse columns.
Thanks. any additional information would be appreciated.I think it's time to improve my knowledge with this details.
Int ID
table, and I'm inserting the value = 1. and @Forrest, I have here 3 version of sql servers ( 2008/2012 in production and I have 2014 for test purposes ). And good to know, I will implement these tests with this DMV on other versions too.