mysql 中int类型字段unsigned和signed的探索
2018-06-17 23:25:05来源:未知 阅读 ()
转自: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
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- MySQL replace函数怎么替换字符串语句 2020-03-09
- PHP访问MySQL查询超时怎么办 2020-03-09
- mysql登录时闪退 2020-02-27
- MySQL出现1067错误号 2020-02-27
- mysql7.x如何单独安装mysql 2020-02-27
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash