Skip to content

Commit 34e2589

Browse files
committed
Add array-based record id advice
1 parent 582d141 commit 34e2589

File tree

1 file changed

+84
-0
lines changed
  • src/content/doc-surrealql/datamodel

1 file changed

+84
-0
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.

0 commit comments

Comments
 (0)