Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Number field search is slow #5970

Open
jwj61 opened this issue Mar 25, 2024 · 12 comments
Open

Number field search is slow #5970

jwj61 opened this issue Mar 25, 2024 · 12 comments
Labels
number fields Number fields slow query Queries that time out or take longer than they should
Milestone

Comments

@jwj61
Copy link
Member

jwj61 commented Mar 25, 2024

From the feedback page, the following search times out:

https://www.lmfdb.org/NumberField/?degree=2&sort_order=h&sort_order=h

@edgarcosta
Copy link
Member

lmfdb=# EXPLAIN ANALYZE SELECT "class_group", "coeffs", "degree", "r2", "disc_abs", "disc_sign", "galois_label", "label", "ramps", "used_grh", "cm", "is_galois", "torsion_order", "regulator", "rd", "grd", "monogenic", "num_ram" FROM "nf_fields" WHERE "degree" = 2 ORDER BY "class_number", "degree", "disc_abs", "disc_sign", "iso_number" LIMIT 50;
                                                                               QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=5938.35..6215.52 rows=50 width=232) (actual time=101555.912..101555.929 rows=50 loops=1)
   ->  Incremental Sort  (cost=5938.35..7476412.54 rows=1347641 width=232) (actual time=101555.910..101555.920 rows=50 loops=1)
         Sort Key: class_number, disc_abs, disc_sign, iso_number
         Presorted Key: class_number
         Full-sort Groups: 1  Sort Method: quicksort  Average Memory: 42kB  Peak Memory: 42kB
         Pre-sorted Groups: 1  Sort Method: top-N heapsort  Average Memory: 38kB  Peak Memory: 38kB
         ->  Index Scan using nf_fields_class_number_id on nf_fields  (cost=0.56..7390705.86 rows=1347641 width=232) (actual time=0.189..101423.677 rows=177169 loops=1)
               Filter: (degree = 2)
               Rows Removed by Filter: 10112321
 Planning Time: 3.294 ms
 Execution Time: 101556.213 ms
(11 rows)

@edgarcosta
Copy link
Member

I will manually add an index on the right sort statement, and see if things improve

@edgarcosta
Copy link
Member

fixed

lmfdb=# EXPLAIN ANALYZE SELECT "class_group", "coeffs", "degree", "r2", "disc_abs", "disc_sign", "galois_label", "label", "ramps", "used_grh", "cm", "is_galois", "torsion_order", "regulator", "rd", "grd", "monogenic", "num_ram" FROM "nf_fields" WHERE "degree" = 2 ORDER BY "class_number", "degree", "disc_abs", "disc_sign", "iso_number" LIMIT 50;
                                                                      QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.56..277.11 rows=50 width=232) (actual time=0.076..0.129 rows=50 loops=1)
   ->  Index Scan using nf_fields_cl_di_di_is on nf_fields  (cost=0.56..7460175.48 rows=1348802 width=232) (actual time=0.075..0.123 rows=50 loops=1)
         Filter: (degree = 2)
         Rows Removed by Filter: 25
 Planning Time: 0.601 ms
 Execution Time: 0.156 ms
(6 rows)

@jwj61 jwj61 reopened this Apr 24, 2024
@jwj61
Copy link
Member Author

jwj61 commented Apr 24, 2024

The feedback page reports that this similar search times out:

https://www.lmfdb.org/NumberField/?degree=8&cm_field=yes&sort_order=h&sort_order=h

@AndrewVSutherland AndrewVSutherland added the number fields Number fields label May 15, 2024
@roed314
Copy link
Contributor

roed314 commented Nov 8, 2024

This kind of thing is always going to be whack-a-mole. We don't have the disc space to support all possible combinations of filtering and sorting. Is it worth changing the gunicorn timeout threshold so that we users can get some kind of slow answer via sequential scan when we don't have the right index? I don't know what consequences this would have, but I assume that we don't hit the timeout all that often, since it would be showing up in the flasklog otherwise.

@AndrewVSutherland
Copy link
Member

Recording the URL from #6039 here for testing: https://www.lmfdb.org/NumberField/?degree=8&subfield=4.4.2304.1.

I agree that allowing a longer timeout would be desirable, but we should discuss this with @edgarcosta. The ideal solution might be to have the error message offer a button you can click to retry with a longer timeout, rather than changing the timeout across the board (this would help protect us from bots).

@edgarcosta
Copy link
Member

I do not see an easy way to increase the timeout interactively.
At the moment, we have two timeouts 30s timeouts in place:

  • the load balancing level
  • and gunicorn
    These are both outside the app environment.

If we ever figure out how to do #6221, perhaps we can tackle this by informing the user that the query is taking longer than expected (which perhaps avoids the timeout). Still, we would need some interaction from the user to prevent abuse.

The right approach is to make the user aware of:

  • beta.lmfdb.org, where the database is in a much nicer machine, or the code
  • and that they can run long queries locally

@edgarcosta
Copy link
Member

Another alternative, is to list the results unsurted, as many queries become sequential due to the sorting.

@AndrewVSutherland
Copy link
Member

Hmm, how would that work? Is this something that could be triggered on timeout?

@roed314
Copy link
Contributor

roed314 commented Nov 8, 2024

On its own #6221 won't solve the problem, since the user can't get to the search results page where they're able to download results. But we could probably add a link to the error message (if we got #6221 working).

@edgarcosta
Copy link
Member

I agree, but both suffer from the technical issue of not knowing how to keep the connections alive without the timeouts kicking in.

@AndrewVSutherland , our timeout for searches is shorter than the load balancer/gunicorn timeouts, so the users get a flask error informing them of the problem.
Many of these queries fail, as the results are not rare enough, and since we asked for a limited number (and sorted), the planner prefers to sequential scan the table, as it expects to find the first X fairly quickly. This is not so much an issue if we do not ask for the results to be sorted, so we could simply offer an option to present unsorted results.

@AndrewVSutherland
Copy link
Member

@edgarcosta I like this idea, maybe it would be worth trying it out to see how well it does on some of our hard queries.

@roed314 roed314 added this to the v1.3 milestone Nov 13, 2024
@roed314 roed314 added the slow query Queries that time out or take longer than they should label Nov 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
number fields Number fields slow query Queries that time out or take longer than they should
Projects
None yet
Development

No branches or pull requests

4 participants