Difference between sp_spaceused and DataLength SQL Server

徘徊边缘 提交于 2019-12-09 18:41:01

问题


I have a table with single Row when i use SP_SpaceUsed N'<TableName>' it gives me data as 16 KB

and when I use dataLength something like this:-

select ClientID , 
(0 + isnull(datalength(ClientID), 1) + 
isnull(datalength(LeadID), 1) + 
isnull(datalength(Company_Name), 1) + 
isnull(datalength(Website), 1) + 
isnull(datalength(EmployeeCount), 1) + 
isnull(datalength(Revenue), 1) +
 isnull(datalength(Address), 1) + 
isnull(datalength(City), 1) + 
isnull(datalength(State), 1) + 
isnull(datalength(ZipCode), 1) + 
isnull(datalength(CountryID), 1) + 
isnull(datalength(Phone), 1) + 
isnull(datalength(Fax), 1) + 
isnull(datalength(TimeZone), 1) + 
isnull(datalength(SicNo), 1) + 
isnull(datalength(SicDesc), 1) +
 isnull(datalength(ResearchAnalysis), 1) + 
isnull(datalength(SourceID), 1) + 
isnull(datalength(BasketID), 1) + 
isnull(datalength(PipelineStatusID), 1) + 
isnull(datalength(SurveryID), 1) + 
isnull(datalength(NextCallDt), 1) + 
isnull(datalength(CurrentRecStatus), 1) +
 isnull(datalength(AssignedUserID), 1) + 
isnull(datalength(AssignedDate), 1) + 
isnull(datalength(TotValueAmt), 1) + 
isnull(datalength(Remove), 1) + 
isnull(datalength(Release), 1) + 
isnull(datalength(LegendID), 1) + 
isnull(datalength(Inserted_Date), 1) +
 isnull(datalength(Inserted_By), 1) + 
isnull(datalength(Updated_Date), 1) + 
isnull(datalength(Updated_By), 1)) 
as rowsize from TempLeadHeader order by rowsize desc

it gives rowsize 167 i guess this is in bytes

I would like to know why this difference is coming up in the result

Thanks in advance


回答1:


sp_spaceused counts the space used by pages, which are 8k blocks. Remember that a table also includes things like indexes that take up space too. not to mention that data on pages are never full unless the fill factor is 100%

datalength will tell you how many bytes your column is




回答2:


you compared 1 row against a table you would have to sum it for every row and even then it won't be the same because you are not showing header information and index data

you can also do something like this

dbcc showcontig ('TempLeadHeader') with tableresults

Then look at min, max and average recordsize columns



来源:https://stackoverflow.com/questions/768320/difference-between-sp-spaceused-and-datalength-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!