A boolean (bool) is a primitive type that can be either true or false.
CREATE person SET newsletter = false, interested = true;Many SurrealDB operators and functions return booleans.
/**[test]
[[test.results]]
value = "[{ id: person:jhn1rlhusqypyyyy0gjg, name: 'Billy', name_is_billy: true, name_is_long: false }]"
skip-record-id-key = true
*/
SELECT
name,
id,
name = "Billy" AS name_is_billy,
name.len() > 20 AS name_is_long
FROM
CREATE person SET name = "Billy";[
{
id: person:7j4t4higwb141v1v2xum,
name: 'Billy',
name_is_billy: true,
name_is_long: false
}
]Boolean values can be written in anycase.
/**[test]
[[test.results]]
value = "[{ id: person:1vyseg5ig08xn1ie3h43, interested: true, newsletter: false, very_interested: true }]"
skip-record-id-key = true
*/
CREATE person SET
newsletter = FALSE,
interested = True,
very_interested = trUE; Booleans in WHERE clauses
When 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.
/**[test]
[[test.results]]
value = "NONE"
[[test.results]]
value = "NONE"
[[test.results]]
value = "[]"
[[test.results]]
value = "[]"
[[test.results]]
value = "[{ data_length: 3, id: person:one, is_short: true, random_data: 'HI!' }]"
[[test.results]]
value = "[{ data_length: 3, id: person:one, is_short: true, random_data: 'HI!' }]"
[[test.results]]
value = "[{ data_length: 3, id: person:one, is_short: true, random_data: 'HI!' }]"
[[test.results]]
value = "[{ data_length: 3, id: person:one, is_short: true, random_data: 'HI!' }]"
*/
DEFINE FIELD data_length ON person VALUE random_data.len();
DEFINE FIELD is_short ON person VALUE random_data.len() < 10;
-- Fill up the database a bit with 10,000 records
CREATE |person:10000|
SET random_data = rand::string(1000) RETURN NONE;
-- Add one outlier with short random_data
CREATE person:one SET random_data = "HI!" 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;Boolean values vs. truthy values
All SurrealQL values are either truthy or not. While seemingly similar to booleans in that true is a truthy value and false is not, the truthiness of a value extends to all value types and is based on the existence of a concrete value as opposed to empty values, NONE, NULL, and so on. For more information and examples, see this page.