Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"is not null" vs boolean MySQL - Performance

I have a column that is a datetime, converted_at.

I plan on making calls that check WHERE converted_at is not null very often. As such, I'm considering having a boolean field converted. Is their a significant performance difference between checking if a field is not null vs if it is false?

Thanks.

like image 966
acco Avatar asked Jan 25 '12 20:01

acco


People also ask

IS NOT NULL SQL performance?

NOT NULL vs NULL performance is negligible and as per this article from 2016 (SQL SERVER), performance shouldn't be a consideration when deciding NOT NULL vs NULL. Even though that field will default to 'N', a command could still set it to NULL if nulls were allowed.

Can boolean be NULL in MySQL?

It depends on the usage and requirements of your project. It's even possible that some of your boolean should be nullable, while others should be NOT NULL , in the same database! Save this answer.

Does MySQL support boolean data type?

MySQL does not have a boolean (or bool) data type. Instead, it converts boolean values into integer data types (TINYINT). When you create a table with a boolean data type, MySQL outputs data as 0, if false, and 1, if true.

IS NOT NULL operator in MySQL?

The NOT NULL constraint enforces a column to NOT accept NULL values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.


2 Answers

If things are answerable in a single field you favour that over to splitting the same thing into two fields. This creates more infrastructure, which, in your case is avoidable.

As to the nub of the question, I believe most database implementation, MySQL included, will have an internal flag which is boolean anyways for representing the NULLability of a field.

You should rely that this is done for you correctly.

As to performance, the bigger question should be on profiling the typical queries that you run on your database and where you created appropriate indexes and analyze table on to improve execution plans and which indexes are used during queries. This question will have a far bigger impact to performance.

like image 161
Stephen Quan Avatar answered Oct 15 '22 07:10

Stephen Quan


Using WHERE converted_at is not null or WHERE converted = FALSE will probably be the same in matters of query performance.

But if you have this additional bit field, that is used to store whether the converted_at field is Null or not, you'll have to somehow maintain integrity (via triggers?) whenever a new row is added and every time the column is updated. So, this is a de-normalization. And also means more complicated code. Moreover, you'll have at least one more index on the table (which means a bit slower Insert/Update/Delete operations).

Therefore, I don't think it's good to add this bit field.

If you can change the column in question from NULL to NOT NULL (possibly by normalizing the table), you may get some performance gain (at the cost/gain of having more tables).

like image 43
ypercubeᵀᴹ Avatar answered Oct 15 '22 09:10

ypercubeᵀᴹ