SQL索引 - char和int之间的差异 [英] SQL Index - Difference Between char and int

查看:392
本文介绍了SQL索引 - char和int之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Sql Server 2005数据库上有一个表。
表的主键字段是代码编号。

I have a table on Sql Server 2005 database. The primary key field of the table is a code number.

作为标准,代码必须包含4位数字。例如:1234,7834,...

As a standard, the code must contain exactly 4 numeric digits. For example: 1234, 7834, ...

在有效选择操作方面,您是否建议字段类型为char(4)或int或numeric(4) 。
对任何类型的表进行索引会不同于其他类型?

Do you suggest that field type to be char(4) or int or numeric(4) in terms of effective select operation. Would indexing the table on any type of these differ from any other?

推荐答案

经常使用整数/标识列数据库表中的主键有多种原因。主键列必须是唯一的,不应该是可更新的,并且实际上应该没有意义。这使得标识列成为一个非常好的选择,因为服务器将为您获取下一个值,它们必须是唯一的,并且整数相对较小且可用(与GUID相比)。

Integer / Identity columns are often used for primary keys in database tables for a number of reasons. Primary key columns must be unique, should not be updatable, and really should be meaningless. This makes an identity column a pretty good choice because the server will get the next value for you, they must be unique, and integers are relatively small and useable (compared to a GUID).

一些数据库架构师会争辩说其他数据类型应该用于主键值,并且双方都可以令人信服地论证无意义和不可更新的标准。无论如何,整数/标识字段非常方便,许多数据库设计者发现它们为引用完整性提供了合适的键值。

Some database architects will argue that other data types should be used for primary key values and the "meaningless" and "not updatable" criteria can be argued convincingly on both sides. Regardless, integer / identity fields are pretty convenient and many database designers find that they make suitable key values for referential integrity.


  1. 最佳选择主键是整数数据类型,因为整数值的处理速度比字符数据类型值快。在处理之前,需要将字符数据类型(作为主键)转换为ASCII等效值。

  2. 如果将整数作为主键,则基于主键获取记录将更快因为这意味着更多的索引记录将出现在单个页面上。所以总搜索时间减少了。连接也会更快。但是这将适用于您的查询使用聚簇索引搜索而不扫描,如果只使用一个表。如果扫描没有额外的列将意味着一个数据页上的更多行。

希望这对您有帮助!

这篇关于SQL索引 - char和int之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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