Skip to content

Commit

Permalink
faster array facet
Browse files Browse the repository at this point in the history
and more correct. progress on #34, see also simonw/datasette#2008
  • Loading branch information
cldellow committed Jan 29, 2023
1 parent 4dfed2b commit d086c46
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions datasette_ui_extras/facet_patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,17 @@ async def ArrayFacet_facet_results(self):
# https://github.com/simonw/datasette/issues/448
facet_sql = """
with inner as ({sql}),
deduped_array_items as (
select
distinct j.value,
inner.{col}
from
json_each([inner].{col}) j
join inner
)
with_ids as (select row_number() over () as row_number, {col} as array from inner),
array_items as (select row_number, each.value from json_each(with_ids.array) each, with_ids)
select
value as value,
count(*) as count
count(distinct row_number) as count
from
deduped_array_items
array_items
group by
value
order by
count(*) desc, value limit {limit}
count(distinct row_number) desc, value limit {limit}
""".format(
col=escape_sqlite(column), sql=self.sql, limit=facet_size + 1
)
Expand Down Expand Up @@ -94,4 +88,5 @@ async def ArrayFacet_facet_results(self):

return facet_results, facets_timed_out


facets.ArrayFacet.facet_results = ArrayFacet_facet_results

0 comments on commit d086c46

Please sign in to comment.