You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: src/content/doc-surrealql/datamodel/ids.mdx
+84
Original file line number
Diff line number
Diff line change
@@ -551,6 +551,90 @@ Not only is the `START 50 LIMIT 2` query more performant, but the entire `num` f
551
551
]
552
552
```
553
553
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
+
554
638
### Auto-incrementing IDs
555
639
556
640
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.
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).
458
458
459
459
```surql title="API DEFINITION"
460
460
type::range(range | array) -> range<record>
@@ -485,7 +485,7 @@ RETURN type::range([1,9,4]);
485
485
The `type::record` function converts a value into a record.
0 commit comments