Skip to content

Commit

Permalink
Stop counting at 10,000 rows when listing tables, refs #2398
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Aug 21, 2024
1 parent bc46066 commit dc1d152
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
5 changes: 4 additions & 1 deletion datasette/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@


class Database:
# For table counts stop at this many rows:
count_limit = 10000

def __init__(
self,
ds,
Expand Down Expand Up @@ -376,7 +379,7 @@ async def table_counts(self, limit=10):
try:
table_count = (
await self.execute(
f"select count(*) from [{table}]",
f"select count(*) from (select * from [{table}] limit {self.count_limit + 1})",
custom_time_limit=limit,
)
).rows[0][0]
Expand Down
2 changes: 1 addition & 1 deletion datasette/templates/database.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ <h2 id="tables">Tables</h2>
<div class="db-table">
<h3><a href="{{ urls.table(database, table.name) }}">{{ table.name }}</a>{% if table.private %} 🔒{% endif %}{% if table.hidden %}<em> (hidden)</em>{% endif %}</h3>
<p><em>{% for column in table.columns %}{{ column }}{% if not loop.last %}, {% endif %}{% endfor %}</em></p>
<p>{% if table.count is none %}Many rows{% else %}{{ "{:,}".format(table.count) }} row{% if table.count == 1 %}{% else %}s{% endif %}{% endif %}</p>
<p>{% if table.count is none %}Many rows{% elif table.count == count_limit + 1 %}&gt;{{ "{:,}".format(count_limit) }} rows{% else %}{{ "{:,}".format(table.count) }} row{% if table.count == 1 %}{% else %}s{% endif %}{% endif %}</p>
</div>
{% endif %}
{% endfor %}
Expand Down
3 changes: 2 additions & 1 deletion datasette/views/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ async def database_actions():
"show_hidden": request.args.get("_show_hidden"),
"editable": True,
"metadata": metadata,
"count_limit": db.count_limit,
"allow_download": datasette.setting("allow_download")
and not db.is_mutable
and not db.is_memory,
Expand Down Expand Up @@ -272,7 +273,7 @@ class QueryContext:
async def get_tables(datasette, request, db):
tables = []
database = db.name
table_counts = await db.table_counts(5)
table_counts = await db.table_counts(100)
hidden_table_names = set(await db.hidden_table_names())
all_foreign_keys = await db.get_all_foreign_keys()

Expand Down

0 comments on commit dc1d152

Please sign in to comment.