Reputation: 85
In search pages, users have access to first 1000 results. Currently I use following
SELECT COUNT(*) as count FROM (SELECT * FROM jobs WHERE MATCH (title, company, state, city) AGAINST ('senior software engineer san fransisco california') LIMIT 1000) AS a
in order to create pagination.
Is there any faster alternative?
PS: Without any limit, some searches have 10 results, some have 500,000 results.
PS 2: I actually wanted to get exact or rough number of results but it is much slower then the SQL I currently use.
Upvotes: 1
Views: 98
Reputation: 26549
One way of getting faster results is to upgrade your underlying system.
Below are two of the most popular options that I have seen:
Sphinx has the added bonus of not affecting your SQL that much but instead adding a new storage engine to mysql in adding to the sphinx damon (if I'm not mistaken).
More information:
Comparison of full text search engine - Lucene, Sphinx, Postgresql, MySQL?
Upvotes: 1
Reputation: 76
Try to use keyword SQL_CALC_FOUND_ROWS
:
SELECT SQL_CALC_FOUND_ROWS * FROM jobs WHERE MATCH (title, company, state, city) AGAINST ('senior software engineer san fransisco california') LIMIT 1000
Then, retrieve estimated row count using
SELECT FOUND_ROWS();
Although performance of this method is questionable, it may do the job for you.
Also, I'd consider creating additional table containing partial results, being rebuilt from time to time.
Upvotes: 0