I'm using UUIDv7 as the primary key in my Laravel application (HasVersion7Uuids). Currently, I'm filtering article from the last 30 days like this:
$qry->where('created_at', '>=', now()->subDays(30));
Since UUIDv7 is time-ordered, would it be more efficient to filter using the `id` field instead? If so, what's the best way to implement this?
I'm considering something like:
$thirtyDaysAgo = now()->subDays(30);
$oldestAllowedId = Uuid::uuid7($thirtyDaysAgo)->toString();
$qry->where('id', '>=', $oldestAllowedId);
I'm looking to optimize my app's performance and ensure I'm using UUIDv7 to its full potential.
You're on point with your consideration because now you can avoid the need for an additional index on created_at
. In addition, by using the id
column (UUIDv7), you also reduce the number of index lookups needed by directly comparing with $oldestAllowedId
. This should also speed up the query especially on large datasets.