简体   繁体   English

我应该选择什么来获得更好的脚本性能和数据库大小,NULL或0?

[英]What should I choose for better script performance and database size, NULL or 0?

In my INNODB mySQL database I have some columns named like active, verified, disabled among others like name, surname. 在我的INNODB mySQL数据库中,我有一些名为active,verified,disabled等列,如name,surname。

To explain it better 更好地解释它

Column      Type         Null  Default
expires     int(10)      Yes   NULL
verified    tinyint(1)   Yes   NULL
disabled    tinyint(1)   Yes   NULL

When a user logins into my page, I use PHP and check for example 当用户登录我的页面时,我使用PHP并检查例如

if ($row['disabled']) { }

to know if he is disabled ( NULL or 1 ). 知道他是否被disabledNULL1 )。 (there are only these 2 possibilities). (只有这两种可能性)。

For now, I set them as NULL but I thought if it is better to use 0 instead, knowing that 0 is empty in PHP too. 现在,我将它们设置为NULL,但我认为如果使用0代替它更好,因为知道PHP中的0也是空的。

Concluding, my questions are two. 最后,我的问题是两个。

  1. Does thousands NULL records grow without a reason the DB instead of thousands 0 records? 成千上万的NULL记录是否增长而没有DB而不是数千条0记录的原因?
  2. Does NULL or 0 affects PHP execution's performance differently ? NULL0是否会以不同的方式影响PHP执行的性能? If so, what is the best combination of mySQL and PHP to have for the requested checks as above? 如果是这样,mySQL和PHP的最佳组合是什么,如上所述的请求检查?

Update 更新

On my question number 1, my question is if NULL is actually null of size, otherwise is +3 bytes right? 在我的问题1,我的问题是如果NULL实际上是null的大小,否则是+3字节对吗?

In mysql, NULL values mean that a particular field has no value in it or it is empty. 在mysql中,NULL值表示特定字段中没有值或者为空。 Ideally, if you have a field like Name and Address and don't want to assign values to them all the time, you can state that it's default value is NULL. 理想情况下,如果您有一个名称和地址等字段,并且不想一直为它们赋值,则可以声明它的默认值为NULL。

But if you're talking about only two possibilities like a True or False item, it's best to go for the Boolean field that is most space efficient. 但是如果你只谈论两种可能性,比如True或False项,那么最好选择空间效率最高的布尔字段。

Going further, for fields like Booleans (eg: disabled) and dates (eg: DateJoined) it's not advisable to keep them as null or unassigned. 更进一步,对于像布尔(例如:禁用)和日期(例如:DateJoined)这样的字段,不建议将它们保留为空或未分配。 You can bump into problems in the future if you had to interface with another language like VB. 如果你不得不与VB之类的其他语言接口,你可能会遇到问题。 For example, if you try to read an assigned date field via VB, you'll see that your app crashes. 例如,如果您尝试通过VB读取指定的日期字段,您将看到您的应用程序崩溃。 But through PHP, this is not a problem. 但是通过PHP,这不是问题。 To be on the safe side, it's best to assign a default true or false (1 or 0) value for such fields. 为了安全起见,最好为这些字段分配默认的true或false(1或0)值。

And finally, to CHECK for null values, the normal relational operators are not suitable. 最后,要检查空值,正常的关系运算符是不合适的。 Rather, you should use the IS NULL or IS NOT NULL operator instead. 相反,您应该使用IS NULLIS NOT NULL运算符。

More info here: http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html 更多信息: http//dev.mysql.com/doc/refman/5.0/en/working-with-null.html

Does thousands NULL records grow without a reason the DB instead of thousands 0 records? 成千上万的NULL记录是否增长而没有DB而不是数千条0记录的原因?

Yes, allowing NULL in a column grows the size of the database, after that though storing NULL's does not take up extra space. 是的,允许列中的NULL会增加数据库的大小,之后存储NULL不会占用额外的空间。 If a column is defined as NOT NULL, it takes up less space. 如果列定义为NOT NULL,则占用的空间更少。
An exception is varchar(), if you have varchar() column that allows NULL, and many rows are NULL, it might take up less space. varchar()是一个例外,如果你有允许NULL的varchar()列, 并且许多行是NULL,它可能占用更少的空间。

Should I change the way I make the PHP checks for better perfomance ? 我应该改变我检查PHP的方式以获得更好的性能吗?

I doubt it will matter much, you should not the checking 1000's of records in PHP anyways. 我怀疑它会有多重要,你不应该在PHP中检查1000的记录。
You should be doing: 你应该这样做:

SELECT * FROM  a WHERE disabled = '1'  

Or 要么

SELECT COUNT(*) FROM a WHERE disabled IS NULL

And besides, you can always do 此外,你可以随时做

SELECT IFNULL(disabled,0) as disabled FROM ....

Don't try to do the DB's job in PHP. 不要试图在PHP中执行DB的工作。

One word of warning..... 一句警告.....

If a row has two values, putting an index on it will likely not help, unless one of the two values is rare. 如果一行有两个值,那么在它上面放一个索引可能没有用,除非这两个值中的一个很少见。
This is called low cardinality of a database column. 这称为数据库列的低基数。
For more info see here: Does it make sense to use an index that will have a low cardinality? 有关详细信息,请参阅此处: 使用具有低基数的索引是否有意义?

If one of the two values is rare, make sure you test for that value, not the other way round. 如果这两个值中的一个很少,请确保测试该值,而不是相反。

To recap 回顾一下
Define your table as: 将表定义为:

Column      Type         Null?  Default  Comment
id          integer      No     Auto_inc  Primary Key 
expires     datetime     No     NULL      Must be supplied
verified    boolean      No     0         boolean = tinyint(1)  
disabled    boolean      No     1         -- but the intend is clearer.

You can even use a BEFORE INSERT TRIGGER to have MySQL automagically set the expires field to NOW() + 30 days. 您甚至可以使用BEFORE INSERT TRIGGER让MySQL自动将expires字段设置为NOW()+ 30天。

This question has absolutely nothing to do with performance and data size. 这个问题与性能和数据大小完全无关
Default value should be chosen by sense, not quantity. 应根据意义而不是数量来选择默认值。
It is as if you asked, what fuel will be be better for your car - gasoline or uranium. 就好像你问过,什么燃料对你的车更好 - 汽油或铀。 The fuel intended to use with your car! 打算与你的车一起使用的燃料!

NULL and 0 are different values, with different meaning. NULL和0是不同的值,具有不同的含义。

  • NULL means there is NO value. NULL表示没有值。 And will affect some queries, where clause for example. 会影响一些查询,例如where子句。
  • 0 means perfectly valid value, equal to 0 0表示完全有效的值,等于0

So, you have to choose one suits your logic , not some groundless fears of consuming enormous amount of space - whole 1 megabyte per 1000000 users. 所以,你必须选择一个适合你的逻辑 ,而不是一些消耗大量空间的无端担忧 - 每100万用户整个1兆字节。

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

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