Boolean values can be used to mark whether a field is true
or false
.
CREATE person SET newsletter = false, interested = true;
Many SurrealDB operators and functions return booleans.
CREATE person SET name = "Billy", name_is_billy = name = "Billy", name_is_long = string::len(name) > 10;
Response[ { "id": "person:8ii7v7f54a2m1rla1y6b", "name": "Billy", "name_is_billy": true, "name_is_long": false } ]
Boolean values can be written in anycase.
CREATE person SET newsletter = FALSE, interested = True, very_interested = trUE;
WHERE
clausesWhen performing a query on the database, accessing a record’s ID directly or using a record range allows performance to be significantly sped up by avoiding the table scan which is used when a WHERE
clause is included.
However, if a WHERE
clause is unavoidable, performance can still be improved by simplifying the portion after the clause as much as possible. As a boolean is the simplest possible datatype, having a boolean field that can be used in a WHERE
clause can significantly improve performance compared to a more complex operation.
-- Fill up the database a bit with 10,000 records CREATE |person:10000| SET random_data = rand::string(1000), data_length = random_data.len(), is_short = data_length < 10 RETURN NONE; -- Add one outlier CREATE person:one SET random_data = "HI!", data_length = random_data.len(), is_short = data_length < 10 RETURN NONE; -- Function call + compare operation: slowest SELECT * FROM person WHERE random_data.len() < 10; -- Compare operation: much faster SELECT * FROM person WHERE data_length < 10; -- Boolean check: even faster SELECT * FROM person WHERE is_short; -- Direct record access: almost instantaneous SELECT * FROM person:one;