转自https://www.cnblogs.com/wangzhongqiu/p/6424827.html

用法:

mysql> CREATE TABLE t ( a INT UNSIGNED, b INT UNSIGNED )

探索一:正负数问题

拿tinyint字段来举例,unsigned后,字段的取值范围是0-255,而signed的范围是-128 - 127。 那么如果我们在明确不需要负值存在的情况下,通常是不要设置signed来支持负数的。 因为只支持正数会让存储空间大一倍呢(当然我这种表达可能不准确)。 假设我们使用tinyint来存储一些状态值。 0表示删除,1表示待付款,2表示已付款,3...。 突然来个需求要加订单取消,一些有代码洁癖的人就想,那就将定义为:-1表示取消吧。 但是就因为有了-1,我们说起来应该可以从0存到255的,结果就变为了0-127。 所以一般情况下,我们不建议这样设置

字段设置为unsigned后有一个问题是:

当select a - b from t时,a为10,b为12,那么这时就会出现异常情况:ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in '(`test`.`t`.`a` - `test`.`t`.`b`)'

所以注意这种情况即可

探索二:性能问题

严格讲,在性能上是有细微的差别的。 unsigned的性能更好,当只存储正整数的情况下。 因为,当unsigned时,假设查询值在500以下的数据,那么MySQL会将范围定义为:0-500,而如果是signed,则查询范围为:-2147483648 - 500。 参考文章:http://rakesh.sankar-b.com/2010/08/25/mysql-unsigned-int-to-signed-int-performance-tips-index/

里面讲到:

Let’s say you want to know the list of customers who have purchased an item of quantity 500 or less. Following is the query you might be used to get these results:

SELECT *
FROM customer
WHERE quantity <= 500

Cool, the above query will yield you the list of customers who have purchased an item of quantity 500 or less. Right, what is the big deal, it should return fast, but consider when you have a table with millions of records then this query might be slow in returning you the results.

Yes, that is true, you can always add an “ index ” to the “quantity” field and improve the performance – exactly, this should improve the performance of processing the query much better than without an “index”.

Without “unsigned”:
Process flow, since the quantity field is an “ int ” and you have an  index  of this field, MySQL  will define the range as  -2147483648 to 500  and it will get the result based on this range.

With “unsigned”:
Process flow, since the quantity field is an “ int ” with “ unsigned ” and you have an index of this field,  MySQL  will define the range as  0 to 500  and it will get the result based on this range.

Now compare the difference yourself and tell me, for sure it will improve the performance of the your query. Since we know we never store any negative (signed values) in the quantity field and the default behavior of “ int ” is “ signed “, it’s always better to write a full-syntax while creating a table.

总的说来,设置unsigned最大的差异是字段取值范围的变化。 所以基于这点来对字段的unsigned或者signed是比较明智的决定

以上

参考文献:

http://verysimple.com/2006/10/22/mysql-data-type-optimization-tips/

http://rakesh.sankar-b.com/2010/08/25/mysql-unsigned-int-to-signed-int-performance-tips-index/

http://dev.mysql.com/doc/refman/5.7/en/numeric-type-overview.html

http://www.cnblogs.com/blankqdb/archive/2012/11/03/blank_qdb.html

【转】mysql 中int类型字段unsigned和signed的区别的更多相关文章

  1. mysql 中int类型字段unsigned和signed的探索

    转自:http://www.0791quanquan.com/news_keji/topic_816453/ 探索一:正负数问题 拿tinyint字段来举例,unsigned后,字段的取值范围是0-2 ...

  2. 关于Java读取mysql中date类型字段默认值'0000-00-00'的问题

    今天在做项目过程中,查询一个表中数据时总碰到这个问题:      java.sql.SQLException:Value '0000-00-00' can not be represented as ...

  3. MySQL中int类型的字段使用like查询方法

    方法参考自: http://stackoverflow.com/questions/8422455/performing-a-like-comparison-on-an-int-field 也就是使用 ...

  4. 解决python写入mysql中datetime类型遇到的问题

    解决python写入mysql中datetime类型遇到的问题 刚开始使用python,还不太熟练,遇到一个datetime数据类型的问题: 在mysql数据库中,有一个datetime类型的字段用于 ...

  5. mysql中整数类型后面的数字,是不是指定这个字段的长度?比如int(11),11代表11个字节吗?

    原文地址:    http://www.cnblogs.com/stringzero/p/5707467.html 原先对mysql不太理解,但也没有报错.但理解的不够深入.这次补上. 原来以为int ...

  6. mysql中整数类型后面的数字,比如int(11),11代表11个字节吗?

    原先对mysql不太理解,但也没有报错.但理解的不够深入.这次补上. 原来以为int(11)是指11个字节,int(10)就是10个字节.我错了. http://zhidao.baidu.com/li ...

  7. mysql int类型字段插入空字符串时自动转为0

    mysql int类型字段插入空字符串时自动转为0 如果不想转的话可以修改配置文件 修改 my.ini 文件. # Set the SQL mode to strictsql-mode=”STRICT ...

  8. mysql中int(M) tinyint(M)中M的作用

    原先对mysql不太理解,但也没有报错.但理解的不够深入.这次补上. 原来以为int(11)是指11个字节,int(10)就是10个字节.我错了. http://zhidao.baidu.com/li ...

  9. 扩展mybatis和通用mapper,支持mysql的geometry类型字段

    因项目中需要用到地理位置信息的存储.查询.计算等,经过研究决定使用mysql(5.7版本)数据库的geometry类型字段来保存地理位置坐标,使用虚拟列(Virtual Generated Colum ...

随机推荐

  1. 【面试笔试算法】Program 3 : Complicated Expression(网易游戏笔试题)

    时间限制:50000ms 单点时限:5000ms 内存限制:256MB 描述 在lisp语言中,表达式都用前缀法表示,例如,1 + 2 在lisp中使用(+ 1 2)来表示,其中,表达式的括号是必需的 ...

  2. Android Data Binding高级用法-Observable、动态生成Binding Class(三)

    设置View的id 虽然说Data Binding这种分层模式使得我们对数据的传递简单明了,一般情况下我们可以不设置View的id,不使用findViewById即可对View进行数据上一系列的操作, ...

  3. 苹果新的编程语言 Swift 语言进阶(九)--方法和下标

    一.方法 方法是与特定类型相关的函数.与属性一样,方法也包括实例方法和类型方法. 类.结构.枚举都能定义实例方法,用来封装或实现给定类型的一个实例相关的功能或特定任务. 类.结构.枚举也能定义与类型本 ...

  4. Oracle EBS ERP中月结年结的流程总结

    月结与年结处理,是企业财务比较特殊而重要的业务操作.在实施与推广OracleERP系统过程中,如何结合现行的会计制度与惯例,充分利用软件功能,做好相应的关账.开账工作,是困扰许多企业财务人员乃至实施顾 ...

  5. 【51】java设计模式-工厂设计模式剖析

    工厂设计设计模式的分类: 工厂模式在<Java与模式>中分为三类: 1)简单工厂模式(Simple Factory):不利于产生系列产品: 2)工厂方法模式(Factory Method) ...

  6. android的ndk开发简介-android学习之旅(93)

    环境搭建 1.安装ndk 2.安装cygwin (android是基于linux的Framework,运行的本地库是.SO,而不是.dll库,大部分都实在windows下开发,如果是linux就没这个 ...

  7. 用shell脚本挂载linux主机拷贝相应文件copy.sh

    #!/bin/sh # $1 MOUNTDIR $2 TARGETDIR $3 ERRORLOG #参数检查 if test $# -ne 3 then echo "argument che ...

  8. Map 遍历分析

    1. 阐述 对于Java中Map的遍历方式,很多文章都推荐使用entrySet,认为其比keySet的效率高很多.理由是:entrySet方法一次拿到所有key和value的集合:而keySet拿到的 ...

  9. 使用javascript中读取Xml文件做成的一个二级联动菜单

    [html] view plaincopy <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> ...

  10. Django之Apps源码学习

    先了解下官方文档的介绍 Django包含了一个已经安装应用的注册表,这个注册表存储着配置信息以及用来自省,同时也维护着可用模型的列表. 这个注册表就是apps,位于django.apps下,本质上是一 ...