admin管理员组文章数量:1277382
I've got a MySQL database of several million rows which is exposed via a PHP web app. I want to let users explore this data using a wide variety of filters including ones which search in TEXT and VARCHAR columns.
These filtered queries are run with a limit of 100 per page. Even with multiple filters applied the results appear pretty quickly (< 0.1s per 100).
However, I would like to display a count of the total number of rows obtained from the user's selected filters. This could be hundreds of thousands of rows. Displaying them 100 at a time is very fast, but running a COUNT
without the LIMIT 100
is extremely slow (up to 30 seconds).
I can't really optimise the data for filtering or use cached result sets because of the sheer number of possible combinations users might select.
Is there a MySQL approach which might let me get an approximation of the total number of filtered rows without a very expensive COUNT?
If not, what's the most user-friendly approach here? I'd prefer to give the users some kind of approximate figure rather than none at all, if that's possible.
I've got a MySQL database of several million rows which is exposed via a PHP web app. I want to let users explore this data using a wide variety of filters including ones which search in TEXT and VARCHAR columns.
These filtered queries are run with a limit of 100 per page. Even with multiple filters applied the results appear pretty quickly (< 0.1s per 100).
However, I would like to display a count of the total number of rows obtained from the user's selected filters. This could be hundreds of thousands of rows. Displaying them 100 at a time is very fast, but running a COUNT
without the LIMIT 100
is extremely slow (up to 30 seconds).
I can't really optimise the data for filtering or use cached result sets because of the sheer number of possible combinations users might select.
Is there a MySQL approach which might let me get an approximation of the total number of filtered rows without a very expensive COUNT?
If not, what's the most user-friendly approach here? I'd prefer to give the users some kind of approximate figure rather than none at all, if that's possible.
Share Improve this question edited Feb 24 at 6:30 Shadow 34.3k10 gold badges65 silver badges74 bronze badges asked Feb 24 at 5:27 WackGetWackGet 2,9754 gold badges40 silver badges54 bronze badges 10 | Show 5 more comments1 Answer
Reset to default 0When you're searching a mess of text columns (or varchar columns) with wildcard query predicates such as column LIKE '%searchterm%'
what you want is very slow. Inherently. Unfortunately.
Using the nonstandard SQL_CALC_FOUND_ROWS extension in MySQL / MariaDB looks tempting. But it is going to perform just as badly as counting the rows
What can you do?
- Use
LIMIT 101
instead ofLIMIT 100
and make your UI offer a link saying More... if it gets 101 results. In general, this means you change your user interface so it doesn't make any claims about the total number of rows to be found. - Figure out whether you can use FULLTEXT search for the application.
- Switch to PostgreSQL and use their trigram matching indexes.
- Use some sort of external text search server.
- Build your own trigram indexing in MySQL. I did this once to solve a very specific problem. It worked, but it is a big pain in the, umm, neck to develop and maintain.
本文标签:
版权声明:本文标题:sql - How do you get a reasonably accurate estimate of the total number of rows in a heavily-filtered query when COUNT without a 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741292187a2370617.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
SELECT COUNT(*) .. WHERE blah blah blah
under discussion. – Rick James Commented Feb 24 at 7:08SHOW CREATE TABLE
and a sampleSELECT COUNT(*)...
, perhaps we can devise a not-too-kludgy speedup. – Rick James Commented Feb 24 at 7:09