Sql Server int vs nvarchar的性能比较? [英] Sql Server int vs nvarchar comparison on performance?

查看:180
本文介绍了Sql Server int vs nvarchar的性能比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在设计一个表,我可以选择使用int或nvarchar(128)for a列,假设空间不是问题。我的问题是,当我使用int列搜索$ /

时,会出现演出



ID = 12324



或当我用nvarchar列搜索(Key是整个值,所以我不使用LIKE运算符) / p>

其中Key ='my str'



确定较小的数据集不重要,但让我们假设这些数据将在数百万行。

解决方案

INT将更快 - 这就是为什么:




  • SQL Server将其数据和索引组织到8K的页面

  • 如果你有一个带有INT键的索引页,那么如果你有NVARCHAR(128),并且你平均使用20个字符,那么你将获得大约2'000个INT条目

  • 每个条目的字节数,或每页大约200个条目



因此,对于相同数量的索引条目,NVARCHAR(128)是索引页的十倍。



加载和搜索索引页将会显着增加I / O操作。



所以要做的事情很简短:如果可以,总是使用INT。


For you database design/performance gurus out there.

I'm designing a table, I have the choice of either use int or nvarchar (128) for a column, assume space is not a problem. My question is which will give performance

when I search with int column

where ID = 12324

or when I search with nvarchar column (the Key is the entire value, so I'm not using LIKE operator)

where Key = 'my str'

I'm sure for smaller datasets it doesn't matter, but let's assume this data will be in the millions of rows.

解决方案

INT will be faster - here's why:

  • SQL Server organizes its data and index into pages of 8K
  • if you have an index page with INT key on it, you get roughly 2'000 INT entries
  • if you have NVARCHAR(128) and you use on average 20 characters, that's 40 bytes per entry, or roughly 200 entries per page

So for the same amount of index entries, the NVARCHAR(128) case would use ten times as many index pages.

Loading and searching those index pages will incur significantly more I/O operations.

So to make things short: if you can, always use INT .

这篇关于Sql Server int vs nvarchar的性能比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆