首页 / MYSQL / MySQL Boolean类型的坑
MySQL Boolean类型的坑
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MySQL Boolean类型的坑,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2776字,纯文字阅读大概需要4分钟。
内容图文
MySQL中,Boolean只是 tinyint(1) 的别名,也就是说,MySQL中并没有真正的bool类型。
而SQLAlchemy生成SQL的时候并没有检测到 这一点,这就导致一个问题,当使用 bool 类型作为查询条件时,用不上索引,从而导致扫表的行为:
> SELECT COUNT(*) FROM message WHERE message.is_national = 1 AND message.updated_at > '2020-01-01 00:00:00' AND message.deleted_at IS NULL; +----------+ | COUNT(*) | +----------+ | 0 | +----------+ 1 row in set Time: 0.018s > SELECT COUNT(*) FROM message WHERE message.is_national is true AND message.updated_at > '2020-01-01 00:00:00' AND message.deleted_at IS NULL; +----------+ | COUNT(*) | +----------+ | 0 | +----------+ 1 row in set Time: 2.162ssql
注意观察第一行和第二行的时间,很明显第二行没有用上索引,我们来看看 EXPLAIN 的结果便知道了:
> EXPLAIN SELECT COUNT(*) FROM message WHERE message.is_national = 1 AND message.updated_at > '2020-01-01 00:00:00' AND message.de leted_at IS NULL; | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | | 1 | SIMPLE | message | ref | ix_message_updated_at,idx_updated_at_is_national,ix_message_is_national | ix_message_is_national | 1 | const | 1 | Using where |explain> EXPLAIN SELECT COUNT(*) FROM message WHERE message.is_national is true AND message.updated_at > '2020-01-01 00:00:00' AND messag
e.deleted_at IS NULL;
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
| 1 | SIMPLE | message | ALL | ix_message_updated_at,idx_updated_at_is_national | <null> | <null> | <null> | 一个很大的数字 | Using whe
re |
mysql文档给出的解释
java.lang.Boolean if the configuration property tinyInt1isBit is set to true (the default) and the storage size is 1, or java.lang.Integer if not.
要注意下面这个提示
The ResultSet.getObject()
method uses the type conversions between MySQL and Java types, following the JDBC specification where appropriate. The values returned by ResultSetMetaData.GetColumnTypeName()
and ResultSetMetaData.GetColumnClassName()
are shown in the table below. For more information on the JDBC types, see the reference on the java.sql.Types class.
文档地址:https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-type-conversions.html
解决方案:
1.使用ifnull(column, 0)处理该字段,个人测试过可以;
2.在JDBC的URL增加 tinyInt1isBit=false参数,注意参数名区分大小写,否则不生效(默认为true)
即:jdbc:mysql://${ucmha.proxy1_2.host}/${db.mysql.db}?tinyInt1isBit=false
3.避免使用长度为1的tinyint类型字段存储数字格式的数据;
参考资料:
- https://jiajunhuang.com/articles/2020_03_06-mysql_boolean_tinyint_index.md.html
- https://dev.mysql.com/doc/refman/8.0/en/numeric-type-syntax.html
- https://www.jianshu.com/p/6885cad1cb14/
- https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-type-conversions.html
内容总结
以上是互联网集市为您收集整理的MySQL Boolean类型的坑全部内容,希望文章能够帮你解决MySQL Boolean类型的坑所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。