mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
New _nocount=1 option, used to speed up CSVs - closes #1353
This commit is contained in:
parent
8bde6c5461
commit
fd368d3b2c
5 changed files with 34 additions and 5 deletions
|
|
@ -263,12 +263,19 @@ class DataView(BaseView):
|
||||||
|
|
||||||
async def as_csv(self, request, database, hash, **kwargs):
|
async def as_csv(self, request, database, hash, **kwargs):
|
||||||
stream = request.args.get("_stream")
|
stream = request.args.get("_stream")
|
||||||
# Do not calculate facets:
|
# Do not calculate facets or counts:
|
||||||
if not request.args.get("_nofacet"):
|
extra_parameters = [
|
||||||
|
"{}=1".format(key)
|
||||||
|
for key in ("_nofacet", "_nocount")
|
||||||
|
if not request.args.get(key)
|
||||||
|
]
|
||||||
|
if extra_parameters:
|
||||||
if not request.query_string:
|
if not request.query_string:
|
||||||
new_query_string = "_nofacet=1"
|
new_query_string = "&".join(extra_parameters)
|
||||||
else:
|
else:
|
||||||
new_query_string = request.query_string + "&_nofacet=1"
|
new_query_string = (
|
||||||
|
request.query_string + "&" + "&".join(extra_parameters)
|
||||||
|
)
|
||||||
new_scope = dict(
|
new_scope = dict(
|
||||||
request.scope, query_string=new_query_string.encode("latin-1")
|
request.scope, query_string=new_query_string.encode("latin-1")
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -697,7 +697,11 @@ class TableView(RowTableShared):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if count_sql and filtered_table_rows_count is None:
|
if (
|
||||||
|
count_sql
|
||||||
|
and filtered_table_rows_count is None
|
||||||
|
and not request.args.get("_nocount")
|
||||||
|
):
|
||||||
try:
|
try:
|
||||||
count_rows = list(await db.execute(count_sql, from_sql_params))
|
count_rows = list(await db.execute(count_sql, from_sql_params))
|
||||||
filtered_table_rows_count = count_rows[0][0]
|
filtered_table_rows_count = count_rows[0][0]
|
||||||
|
|
|
||||||
|
|
@ -386,6 +386,9 @@ Special table arguments
|
||||||
``?_nofacet=1``
|
``?_nofacet=1``
|
||||||
Disable all facets and facet suggestions for this page, including any defined by :ref:`facets_metadata`.
|
Disable all facets and facet suggestions for this page, including any defined by :ref:`facets_metadata`.
|
||||||
|
|
||||||
|
``?_nocount=1``
|
||||||
|
Disable the ``select count(*)`` query used on this page - a count of ``None`` will be returned instead.
|
||||||
|
|
||||||
``?_trace=1``
|
``?_trace=1``
|
||||||
Turns on tracing for this page: SQL queries executed during the request will
|
Turns on tracing for this page: SQL queries executed during the request will
|
||||||
be gathered and included in the response, either in a new ``"_traces"`` key
|
be gathered and included in the response, either in a new ``"_traces"`` key
|
||||||
|
|
|
||||||
|
|
@ -1683,6 +1683,15 @@ def test_nofacet(app_client, nofacet):
|
||||||
assert response.json["facet_results"] != {}
|
assert response.json["facet_results"] != {}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("nocount,expected_count", ((True, None), (False, 15)))
|
||||||
|
def test_nocount(app_client, nocount, expected_count):
|
||||||
|
path = "/fixtures/facetable.json"
|
||||||
|
if nocount:
|
||||||
|
path += "?_nocount=1"
|
||||||
|
response = app_client.get(path)
|
||||||
|
assert response.json["filtered_table_rows_count"] == expected_count
|
||||||
|
|
||||||
|
|
||||||
def test_expand_labels(app_client):
|
def test_expand_labels(app_client):
|
||||||
response = app_client.get(
|
response = app_client.get(
|
||||||
"/fixtures/facetable.json?_shape=object&_labels=1&_size=2"
|
"/fixtures/facetable.json?_shape=object&_labels=1&_size=2"
|
||||||
|
|
|
||||||
|
|
@ -175,3 +175,9 @@ def test_table_csv_stream_does_not_calculate_facets(app_client):
|
||||||
response = app_client.get("/fixtures/simple_primary_key.csv?_trace=1")
|
response = app_client.get("/fixtures/simple_primary_key.csv?_trace=1")
|
||||||
soup = Soup(response.text, "html.parser")
|
soup = Soup(response.text, "html.parser")
|
||||||
assert "select content, count(*) as n" not in soup.find("pre").text
|
assert "select content, count(*) as n" not in soup.find("pre").text
|
||||||
|
|
||||||
|
|
||||||
|
def test_table_csv_stream_does_not_calculate_counts(app_client):
|
||||||
|
response = app_client.get("/fixtures/simple_primary_key.csv?_trace=1")
|
||||||
|
soup = Soup(response.text, "html.parser")
|
||||||
|
assert "select count(*)" not in soup.find("pre").text
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue