-
-
Notifications
You must be signed in to change notification settings - Fork 710
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Speed up table counts when displaying index and database pages #2398
Comments
This code here: datasette/datasette/views/table.py Lines 1309 to 1316 in d444b6a
Currently it can take as long as the default SQL time limit, which means that if a table is huge you could wait a second or more for the table page to fail to count the rows. |
There are other things that could happen here, for example:
I probably won't implement any of the fancy options as part of this initial issue. |
What should the count timeout be? I'm inclined to add a new setting for it, to join Could call it |
We do have a count time limit elsewhere, but that's for listing tables: datasette/datasette/database.py Lines 370 to 390 in 7d8dd2a
|
Currently we have a https://datasette.io/content/assets?_sort=id&size__gt=100 To https://datasette.io/content/assets?_sort=id&_nocount=1&size__gt=100 |
I think that second one should say "Rows where size > 100", not just "where size > 100". |
Maybe add a |
Another option here: I could have the count limit to a max of 1000 using this pattern: with original as (select ...),
truncated as (select * from original limit 1001)
select count(*) from truncated; Then if that comes back as 1001 I can show Would need to change the JSON API to include a |
This tiny diff makes a HUGE difference all over the place: diff --git a/datasette/database.py b/datasette/database.py
index c761dad7..428d8703 100644
--- a/datasette/database.py
+++ b/datasette/database.py
@@ -376,7 +376,7 @@ class Database:
try:
table_count = (
await self.execute(
- f"select count(*) from [{table}]",
+ f"select count(*) from (select * from [{table}] limit 10001)",
custom_time_limit=limit,
)
).rows[0][0] |
I've decided to go with a limit of 10,000, which seems to make everything feel really snappy while still showing useful results for most databases. I'm now showing "Many rows" if the time limit was hit, ">10,000 rows" if the row count was 10001 and the actual number of rows otherwise. |
I'm going to deploy this branch to Datasette Cloud and see how it feels. |
It feels really good. I'm happy with this change. |
Original title: The query that counts all matching rows on table page should have a shorter time limit
I thought it did already, but apparently not.
The text was updated successfully, but these errors were encountered: