转自: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

原文:http://www.cnblogs.com/wangzhongqiu/p/6424827.html

mysql中signed是什么类型_mysql 中int类型字段unsigned和signed的探索相关推荐

  1. 设变量n为float类型,m为int类型,则以下能实现将n中的数值保留小数点后两位,第三位进行四舍五人运算的表达式是: A) n=(n*100+0.5)/100.0 B)m=n*100+0.5

    设变量n为float类型,m为int类型,则以下能实现将n中的数值保留小数点后两位,第三位进行四舍五人运算的表达式是: A) n=(n100+0.5)/100.0 B)m=n100+0.5 ,n= m ...

  2. int类型转换成String类型,String类型转化成int类型

    一.int类型转换成String类型 int i = 100; //方法一:使用i+""; String s1 = i+""; //方法二:String.val ...

  3. java String类型转化为Int类型

    [将String 类型转化为int 类型] 一: public class IntegerDemo {public static void main(String[] args) {String st ...

  4. mysql timestamp 类型_MySQL中“诡异”的TIMESTAMP数据类型

    注意:从5.6.4版本开始,TIME,TIMESTAMP,DATTIME这三种类型增加了对小数秒的支持,timestamp存储大小为4Bytes+小数部分:datetime存储大小为5Bytes+小数 ...

  5. mysql中的所有类型_mysql中常用的数据类型

    MySQL是一种关系数据库管理系统,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性.MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化语 ...

  6. mysql数据库中的int类型_MySQL中int(M)和tinyint(M)数值类型中M值的意义

    在一开始接触MySQL数据库时,对于int(M)及tinyint(M)两者数值类型后面的M值理解是最多能够插入数据库中的值不能大于M: 后来工作后,也是一边学习一边使用,之后的理解是其中的M的意思是插 ...

  7. mysql数据库表中的类型_MySQL数据库中表类型MyISAM与InnoDB的区别

    MyISAM 和 InnoDB 讲解 InnoDB和MyISAM是许多人在使用MySQL时最常用的两个表类型,这两个表类型各有优劣,视具体应用而定.基本的差别为:MyISAM类型不支持事务处理等高级处 ...

  8. mysql中的double类型_MySQL中float、double、decimal三个浮点类型的区别与总结!

    作者:极客小俊 一个专注于web技术的80后 我不用拼过聪明人,我只需要拼过那些懒人 我就一定会超越大部分人! CSDN@极客小俊,原创文章, B站技术分享 个人博客: cnblogs.com 前端h ...

  9. mysql decimal 类型_MySQL中decimal类型用法的简单介绍

    MySQL中支持浮点数的类型有FLOAT.DOUBLE和DECIMAL类型,DECIMAL 类型不同于FLOAT和DOUBLE,DECIMAL 实际是以串存放的.DECIMAL 可能的最大取值范围与D ...

最新文章

  1. 分享十款免费数据恢复软件
  2. 带AI无人车上云驾校,不出门练遍各大城市道路,华南理工大学团队拿下“互联网+”大赛金奖...
  3. 利用Navigation Timing测量页面加载时间
  4. Jmeter+ant运行脚本,得到HTML报告
  5. 【复习】操作系统第一章
  6. Win7system登录打开计算机,Windows7系统system文件丢失导致开机黑屏如何解决
  7. 算法系列之图--DFS
  8. 页面点击提交跳转_一个入口一次提交!“六税合一”综合申报操作指南请收好~...
  9. 实战weblogic集群之创建节点和集群
  10. linux时间界面返回,Android开发教程:游戏过程中按Home键后返回游戏界面
  11. 2020 ccf推荐中文期刊_中国计算机学会推荐中文期刊目录,让业内学者不再盲目投稿...
  12. explict关键字
  13. Mac搭建Nodejs+Express
  14. Django form模块使用心得
  15. 制定交叉编译工具_制作交叉编译工具链的方法总结(详细)
  16. 时间序列深度学习:seq2seq 模型预测太阳黑子
  17. Debug JDK源码没变量值怎么办?
  18. Yolo训练自己的数据集,将json格式转换成txt文件格式
  19. 网络协议之UDP数据包
  20. 11.0高等数学五-函数项级数收敛与一致收敛

热门文章

  1. 【赛码】回文串(python版本)
  2. 相机快门声音 设置中删除快门声音设置选项
  3. 微信jssdk录音API例子
  4. teb_local_planner编译
  5. Git学习笔记(2)- 远程仓库
  6. git 报错:remote: HTTP Basic: Access denied
  7. 数据恢复Ontrack EasyRecovery 15中文免费版2023最新
  8. 我如何在JavaScript中建立良好的发布过程
  9. 计算机大专当兵吃香吗,大专学生参军有这么多好处,现在已经有好多人后悔没参军...
  10. Tapestry携旗下蔻驰等三大品牌再赴进博会;星巴克连续第三年成为进博会支持企业 | 美通企业日报...