clerk / clerk/clerk-sdk-python

`clerk.users.count` with arguments can be very slow for large number of users

Open
#176 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
186
Forks
47
Avg merge
18h 43m
Merged PRs (30d)
1

Description

Repro

import time
import clerk_backend_api
from clerk_backend_api import Clerk

print(clerk_backend_api.__version__) # 3.1.11 (latest)

clerk_api_key = "xxxxx"
clerk = Clerk(bearer_auth=clerk_api_key)

start_all = time.perf_counter()
all_users = clerk.users.count().total_count
end_all = time.perf_counter()

print(f"{all_users = } (took {end_all - start_all:.3f} seconds)")
# all_users = 926354 (took 0.342 seconds)

created_before_ms = int(time.time() * 1000)

start_before = time.perf_counter()
users_before_ts = clerk.users.count(created_at_before=created_before_ms).total_count
end_before = time.perf_counter()

print(f"{users_before_ts = } (took {end_before - start_before:.3f} seconds)")
# users_before_ts = 926354 (took 436.951 seconds)

As you can see, adding a predicate to users.count changes the response time from sub 1 second to ~7 minutes.

I appreciate the "COUNT *" fast path, but 7 minutes feels very long in the other case.
Is it possible that performance can be improved here at all?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided Python reproduction and the clerk.users.count entry point, comparing the unfiltered call with the created_at_before argument. Trace how the argumented count request is handled and define done as a substantially shorter response time for large user sets without changing the count result.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend, performance
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.