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.