简体   繁体   English

MySQL 中的 True/False 与 0/1

[英]True/False vs 0/1 in MySQL

Which is faster in a MySQL database?在 MySQL 数据库中哪个更快 Booleans, or using zero and one to represent boolean values?布尔值,还是使用零和一来表示布尔值? My frontend just has a yes/no radio button.我的前端只有一个是/否单选按钮。

Some "front ends", with the "Use Booleans" option enabled, will treat all TINYINT(1) columns as Boolean, and vice versa.某些启用了“使用布尔值”选项的“前端”会将所有 TINYINT(1) 列视为布尔值,反之亦然。

This allows you to, in the application, use TRUE and FALSE rather than 1 and 0.这允许您在应用程序中使用 TRUE 和 FALSE 而不是 1 和 0。

This doesn't affect the database at all, since it's implemented in the application.这根本不会影响数据库,因为它是在应用程序中实现的。

There is not really a BOOLEAN type in MySQL. MySQL 中并没有真正的BOOLEAN类型。 BOOLEAN is just a synonym for TINYINT(1), and TRUE and FALSE are synonyms for 1 and 0. BOOLEAN 只是 TINYINT(1) 的同义词,TRUE 和 FALSE 是 1 和 0 的同义词。

If the conversion is done in the compiler, there will be no difference in performance in the application.如果在编译器中进行转换,则应用程序的性能不会有任何差异。 Otherwise, the difference still won't be noticeable.否则,差异仍然不会很明显。

You should use whichever method allows you to code more efficiently, though not using the feature may reduce dependency on that particular "front end" vendor.您应该使用可以让您更有效地编码的任何方法,尽管不使用该功能可能会减少对特定“前端”供应商的依赖。

In MySQL TRUE and FALSE are synonyms for TINYINT(1) .在 MySQL 中TRUEFALSETINYINT(1)同义词

So therefore its basically the same thing, but MySQL is converting to 0/1 - so just use a TINYINT if that's easier for you因此,因此它基本上是相同的,但是 MySQL 正在转换为 0/1 - 如果这对您来说更容易,请使用TINYINT

PS聚苯乙烯
The performance is likely to be so minuscule (if at all), that if you need to ask on StackOverflow, then it won't affect your database :)性能可能非常小(如果有的话),如果您需要在 StackOverflow 上询问,那么它不会影响您的数据库:)

Bit is also an option if tinyint isn't to your liking.如果tinyint不符合您的喜好, Bit也是一种选择。 A few links:几个链接:

Not surprisingly, more info about numeric types is available in the manual .毫不奇怪,手册中提供有关数字类型的更多信息。

One more link: http://blog.mclaughlinsoftware.com/2010/02/26/mysql-boolean-data-type/还有一个链接: http : //blog.mclaughlinsoftware.com/2010/02/26/mysql-boolean-data-type/

And a quote from the comment section of the article above:以及上面文章评论部分的引用:

  • TINYINT(1) isn't a synonym for bit(1). TINYINT(1) 不是 bit(1) 的同义词。
  • TINYINT(1) can store -9 to 9. TINYINT(1) 可以存储 -9 到 9。
  • TINYINT(1) UNSIGNED: 0-9 TINYINT(1) 无符号:0-9
  • BIT(1): 0, 1. (Bit, literally). BIT(1): 0, 1.(位,字面意思)。

Edit: This edit (and answer) is only remotely related to the original question...编辑:此编辑(和答案)仅与原始问题有很大关系...

Additional quotes by Justin Rovang and the author maclochlainn (comment section of the linked article). Justin Rovang 和作者 maclochlainn 的其他引用(链接文章的评论部分)。

Excuse me, seems I've fallen victim to substr-ism: TINYINT(1): -128-+127 TINYINT(1) UNSIGNED: 0-255 (Justin Rovang 25 Aug 11 at 4:32 pm)对不起,我似乎已经成为 substr-ism 的受害者:TINYINT(1): -128-+127 TINYINT(1) UNSIGNED: 0-255(Justin Rovang 25 August 11 at 4:32 pm)

True enough, but the post was about what PHPMyAdmin listed as a Boolean, and there it only uses 0 or 1 from the entire wide range of 256 possibilities.确实如此,但该帖子是关于 PHPMyAdmin 列为布尔值的内容,在那里它只使用了 256 种可能性的整个范围内的 0 或 1。 (maclochlainn 25 Aug 11 at 11:35 pm) (maclochlainn 11 年 8 月 25 日晚上 11:35)

If you are into performance, then it is worth using ENUM type.如果您对性能感兴趣,那么值得使用 ENUM 类型。 It will probably be faster on big tables, due to the better index performance.由于更好的索引性能,它在大表上可能会更快。

The way of using it (source: http://dev.mysql.com/doc/refman/5.5/en/enum.html ):使用方式(来源: http : //dev.mysql.com/doc/refman/5.5/en/enum.html ):

CREATE TABLE shirts (
    name VARCHAR(40),
    size ENUM('x-small', 'small', 'medium', 'large', 'x-large')
);

But, I always say that explaining the query like this:但是,我总是说这样解释查询:

EXPLAIN SELECT * FROM shirts WHERE size='medium';

will tell you lots of information about your query and help on building a better table structure.将告诉您有关查询的大量信息,并有助于构建更好的表结构。 For this end, it is usefull to let phpmyadmin Propose a table table structure - but this is more a long time optimisation possibility, when the table is already filled with lots of data.为此,让 phpmyadmin 提出一个表表结构是有用的——但是当表已经填满了大量数据时,这是一个更长时间的优化可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM