转自:http://www.0791quanquan.com/news_keji/topic_816453/

探索一:正负数问题

拿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

unsigned int mysql_mysql 中int类型字段unsigned和signed的探索相关推荐

  1. server sql 中类型为bit应该插入什么值_关于SQL Server中bit类型字段增删查改的一些事...

    前言 本文主要给大家介绍了关于SQL Server中bit类型字段增删查改的一些事,话说BIT类型字段之前,先看"诡异"的一幕,执行Update成功,但是查询出来的结果依然是1,而 ...

  2. C# WebAPI中DateTime类型字段在使用微软自带的方法转json格式后默认含T的解决办法...

    C# WebAPI中DateTime类型字段在使用微软自带的方法转json格式后默认含T的解决办法 原文:C# WebAPI中DateTime类型字段在使用微软自带的方法转json格式后默认含T的解决 ...

  3. ORACLE中date类型字段的处理

    ORACLE中date类型字段的处理 (1)在英文版本的ORACLE中默认日期格式为'DD-MON-YY',例如'01-JAN-98' 在汉化的中文版本中ORACLE默认日期格式为'日-月-年',例如 ...

  4. mysql int tinyint_MySQL中int(M)和tinyint(M)数值类型中M值的意义

    首先,我们创建一个数据表test: mysql> CREATE TABLE test( -> id1 int(1), -> id2 tinyint(1) ->); 我们给id1 ...

  5. smallint mysql_mysql中int、bigint、smallint 和 tinyint的区别与长度的含义

    最近使用mysql数据库的时候遇到了多种数字的类型,主要有int,bigint,smallint和tinyint.其中比较迷惑的是int和smallint的差别.今天就在网上仔细找了找,找到如下内容, ...

  6. mysql int zerofill_Mysql 中int[M]—zerofill-阿里云开发者社区

    我们在定义数字类型的数据类型的时候,往往考虑该数字类型的数据能否装的下该字段的最大值,如状态位的字段:tinyint,表的主键:int,或者bigint,今天在看到开发同学提交表结构设计文档中看到数值 ...

  7. java int stack_java中int算法的有趣现象

    今天无意中发现一个怪事,当时没理解,后来跟网友讨论了才知道原理,是关于int值的加法算法,两段代码如下: 代码1: @Testpublic voidtest1() {int stackLength = ...

  8. int signed in mysql_【转】mysql 中int类型字段unsigned和signed的区别

    转自https://www.cnblogs.com/wangzhongqiu/p/6424827.html 用法: mysql> CREATE TABLE t ( a INT UNSIGNED, ...

  9. mysql中signed是什么类型_mysql 中int类型字段unsigned和signed的探索

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

最新文章

  1. 秋天六食物吃出好皮肤
  2. 2016年宜昌楼市将迎来史上最激烈一战
  3. 【Java Web前端开发】HTML表单和CSS部分
  4. 关于vmware 桥接 不识别无线网卡 问题解决方案
  5. 把度分秒的经纬度转换成十进制的经纬度的工具类
  6. Java LocalDate类| lengthOfYear()方法和示例
  7. 能代表个人水平的c语言程序,求全国高等学校计算机水平考试级C程序设计的免费..._报关水平_帮考网...
  8. ZooKeeper(3.4.5) - 配置伪集群模式
  9. 开启本地git权限_Git入门使用和常见操作
  10. win7 64位专业版读不出来移动硬盘的解决办法
  11. 计算机桌面亮度调节,电脑屏幕亮度怎么调?笔记本屏幕亮度调节方法图解教程 电脑维修技术网...
  12. MaprRduce v2 在 java 代码中远程提交作业到 Yarn 的配置项
  13. 一台pc计算机系统启动不了,电脑装系统引导不进去怎么办
  14. 阿里云ACP云计算错题集1-40
  15. (4)量子态矢与算子(算符)
  16. Python爬虫实战(爬取豆瓣电影)
  17. 寻仙手游维护公告服务器停服更新,寻仙手游新服更新内容汇总 新坐骑黑鸾幽煌上线...
  18. java仿qq好友列表_QQ好友列表树形列表java代码实现代码
  19. (九:2020.08.27)CVPR 2019 追踪之论文纲要(译)
  20. 考研线性代数知识导图

热门文章

  1. drive es 软件兼容_某知名软件被完美修改!对不住了!
  2. android开发app初始化,安卓快速开发框架(一)XBaseAndroid初始化使用
  3. java jlabel里面加button_在Java Swing中通过JLabel覆盖JButton?
  4. linux脚本ls输出到变量中,bash – 将命令输出的错误消息存储到shell变量中
  5. react textarea 空格为什么不换行_你需要的 React + TypeScript 50 条规范和经验
  6. ubuntu php7 memcache,linux上安装php7 memcache扩展
  7. php考勤分析,php考勤系统
  8. 修改场景默认pawn的方法
  9. a5d27 第1级bootloader启动问题
  10. a20_v2.0_k70运行在xhda20开发板