Skip to content

perf: avoid redundant COUNT queries for repeated table names #600

Open

Description

@yeongseon

Source-observed improvement

server.table_row_counts(table_names=...) loops over every element in the requested list and issues a separate SELECT COUNT(*) for each matching table. Therefore ["users", "users", " USERS "] can produce three identical COUNT queries for the same table even with the existing request limit. Repeated COUNT calls can be expensive; issue #578 covers explaining their cost to users, but not this same-request redundant execution.

The duplicate-query behavior follows the source code and was not benchmarked against a large live table.

Scoped improvement proposal

  • Cache the COUNT result within one tool invocation by canonical resolved owner-qualified table name, so repeated names do not trigger extra DB queries. Do not cache across invocations or assume row counts are stable over time.
  • To preserve existing response/API behavior, keep one output entry per requested name, in the original order, unless a maintainer explicitly chooses a documented deduplicated output contract.
  • Continue canonical SQL quoting, unknown/ambiguous table error handling and per-connection routing. Do not change the default no-argument behavior or global table/query limits.
  • Add a fake database test that verifies only one COUNT is executed for duplicates and that the entries reuse its result; also test different owners' same table name remain separate.

Acceptance

  • Duplicate explicit names do not multiply COUNT operations, while output order, error entries, total_tables and truncated remain as documented.
  • Named connections never share cached counts or metadata from another connection; cache is request-local.
  • Offline tests and required final-head CI pass; no real table COUNT benchmark is necessary for basic correctness.

No public write API, schema permission changes, or new tool are in scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions