2

I came across an issue, i have the following query:

SELECT 
  DISTINCT 
  p.id, 
  p.title, 
  p.description
FROM 
  (
    SELECT 
      `post_id`, 
      `s_type` 
    FROM 
      `search` 
    WHERE 
      MATCH(
        keyword_data_one, keyword_data_two, keyword_data_three, keyword_data_four
      ) AGAINST(
        '+prize -bell' IN BOOLEAN MODE
      )
  ) AS s 
  JOIN posts AS p ON p.id = s.post_id 
WHERE 
  s.s_type = '0' 
  ORDER BY p.id DESC
LIMIT 
  0, 20

It works but according 800.000 records on each table (posts and search) query time it's way big (results ex: 20 total, Query took 2.1060 seconds).

If i remove ORDER BY p.id DESC the query time it's way less and the query it is executed faster. (20 total, Query took 0.0016 seconds).

My question is if i can rewrite somehow this query to make it faster like this but having the sort option.

I have composite index created on search table, also indexes on the posts table.

SQL Fiddle here https://www.db-fiddle.com/f/rbbY8fBGePECuy6nuJdcus/0

Again, on fiddle example it is returning results same fast for both situation with sort or without but not with 800k records.

9
  • 1
    Please post your table schema. I dbfiddle with sample data would be even nicer. Just a few rows should be enough Commented Dec 6, 2022 at 13:35
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Dec 6, 2022 at 13:36
  • SQL Fiddle: db-fiddle.com/f/rbbY8fBGePECuy6nuJdcus/0
    – alexfsk
    Commented Dec 6, 2022 at 14:24
  • How many rows does this query returns without LIMIT?
    – Akina
    Commented Dec 6, 2022 at 15:35
  • @Akina Depends by keywords. (180k results, 60k, 25k, etc). I'm talking about the real database content. Not about the fiddle example. That's just to see the structure and query. I'm using limit and offset to make the pagination.
    – alexfsk
    Commented Dec 6, 2022 at 15:42

1 Answer 1

0

Let's start by moving WHERE s_type = '0' into the inner query. That may drastically decrease the number of rows in s, leading to all sorts of speedups.

Can there be multiple rows, hence your use of DISTINCT? Perhaps that can be eliminated or moved into the inner query?

To explain why ORDER BY makes such a difference...

  1. fetch rows (in your query, it involves a subquery and a join)
  2. filter on s_type
  3. note that result from the above steps is being stored in a [potentially big - 180K rows!] temp table
  4. sort that temp table
  5. peel of the first 20.

Without the ORDER BY:

  1. fetch rows (in your query, it involves a subquery and a join)
  2. filter on s_type
  3. STOP when 20 rows have been found.

That is, no temp table, no sort.

Imagine that you are the user. And you are paginating through 180K rows, 20 rows per page. How many pages would you get through before you threw a brick through the monitor?

You should not "paginate" through huge datasets; you should design the search mechanism some other way.

1
  • You're right, there is a way to limit the query somehow to prevent returning all those 180k all at once? I mean doing something to speedup the temp table. I can't even use it right now because it would kill the server if it would have concurent searches. It works well only if i do tests alone, if i put it public, it's killing the server.
    – alexfsk
    Commented Dec 6, 2022 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.