Skip to content

Commit 302611e

Browse files
authored
More record range advice (#1270)
1 parent 17274a2 commit 302611e

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

src/content/doc-surrealql/datamodel/ids.mdx

+84
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,90 @@ Not only is the `START 50 LIMIT 2` query more performant, but the entire `num` f
551551
]
552552
```
553553

554+
### Move exact matches in array-based record IDs to the front
555+
556+
Take the following `event` records which can be queried as a perfomant record range.
557+
558+
```surql
559+
CREATE event:[d'2025-05-05T08:00:00Z', user:one, "debug"] SET info = "Logged in";
560+
CREATE event:[d'2025-05-05T08:10:00Z', user:one, "debug"] SET info = "Logged out";
561+
CREATE event:[d'2025-05-05T08:01:00Z', user:two, "debug"] SET info = "Logged in";
562+
```
563+
564+
The ordering of the ID in this case is likely not ideal, because the first item in the array, a `datetime`, will be the first to be evaluated in a range scan. A query such as the one below on a range of dates will effectively ignore the second and third parts of the ID.
565+
566+
```surql
567+
SELECT * FROM event:[d'2025-05-05', user:one, "debug"]..[d'2025-05-06', user:one, "debug"];
568+
569+
-- Same result! user name and "debug" are irrelevant
570+
-- SELECT * FROM event:[d'2025-05-05']..[d'2025-05-06'];
571+
```
572+
573+
```surql title="Output"
574+
[
575+
{
576+
id: event:[
577+
d'2025-05-05T08:00:00Z',
578+
user:one,
579+
'debug'
580+
],
581+
info: 'Logged in'
582+
},
583+
{
584+
id: event:[
585+
d'2025-05-05T08:01:00Z',
586+
user:two,
587+
'debug'
588+
],
589+
info: 'Logged in'
590+
},
591+
{
592+
id: event:[
593+
d'2025-05-05T08:10:00Z',
594+
user:one,
595+
'debug'
596+
],
597+
info: 'Logged out'
598+
}
599+
]
600+
```
601+
602+
Instead, the parts of the array that are more likely to be exactly matched (such as `user:one` and `"debug"`) should be moved to the front.
603+
604+
```surql
605+
CREATE event:[user:one, "debug", d'2025-05-05T08:00:00Z'] SET info = "Logged in";
606+
CREATE event:[user:one, "debug", d'2025-05-05T08:10:00Z'] SET info = "Logged out";
607+
CREATE event:[user:two, "debug", d'2025-05-05T08:01:00Z'] SET info = "Logged in";
608+
```
609+
610+
Using this format, queries can now be performed for a certain user and logging level, over a range of datetimes.
611+
612+
```surql
613+
-- Only returns events for user:one and "debug"
614+
SELECT * FROM event:[user:one, "debug", d'2025-05-05']..[user:one, "debug", d'2025-05-06'];
615+
```
616+
617+
```surql title="Output"
618+
[
619+
{
620+
id: event:[
621+
user:one,
622+
'debug',
623+
d'2025-05-05T08:00:00Z'
624+
],
625+
info: 'Logged in'
626+
},
627+
{
628+
id: event:[
629+
user:one,
630+
'debug',
631+
d'2025-05-05T08:10:00Z'
632+
],
633+
info: 'Logged out'
634+
}
635+
]
636+
```
637+
554638
### Auto-incrementing IDs
555639

556640
While SurrealDB does not use auto-incrementing IDs by default, this behaviour can be achieved in a number of ways. One is to use the [`record::id()`](/docs/surrealql/functions/database/record#recordid) function on the latest record, which returns the latter part of a record ID (the '1' in the record ID `person:1`). This can then be followed up with the [`type::thing()`](/docs/surrealql/functions/database/type#typething) function to create a new record ID.

src/content/doc-surrealql/functions/database/type.mdx

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ These functions can be used for generating and coercing data to specific data ty
8383
</tr>
8484
<tr>
8585
<td scope="row" data-label="Function"><a href="#typerange"><code>type::range()</code></a></td>
86-
<td scope="row" data-label="Description">Converts a value into a record range</td>
86+
<td scope="row" data-label="Description">Converts a value into a range</td>
8787
</tr>
8888
<tr>
8989
<td scope="row" data-label="Function"><a href="#typerecord"><code>type::record()</code></a></td>
@@ -454,7 +454,7 @@ RETURN type::point([ 51.509865, -0.118092 ]);
454454

455455
<Since v="v2.0.0" />
456456

457-
The `type::range` function converts a value into a record range. It accepts a single argument, either a range or an array with two values. If the argument is an array, it will be converted into a range, similar to [casting](/docs/surrealql/datamodel/casting).
457+
The `type::range` function converts a value into a [range](docs/surrealql/datamodel/ranges). It accepts a single argument, either a range or an array with two values. If the argument is an array, it will be converted into a range, similar to [casting](/docs/surrealql/datamodel/casting).
458458

459459
```surql title="API DEFINITION"
460460
type::range(range | array) -> range<record>
@@ -485,7 +485,7 @@ RETURN type::range([1,9,4]);
485485
The `type::record` function converts a value into a record.
486486

487487
```surql title="API DEFINITION"
488-
type::record(record | string, option<string>) -> range<record>
488+
type::record(record | string, option<string>) -> record
489489
```
490490

491491
The following example shows this function, and its output, when used in a [`RETURN`](/docs/surrealql/statements/return) statement:

0 commit comments

Comments
 (0)