I have a MySQL query that is currently running at 2.5 seconds and I want to trim it down to 2 seconds max.
Here is the query:
SELECT SQL_CALC_FOUND_ROWS Distinct(c.id)
FROM `content` c
INNER JOIN `actions` AS action
ON c.parent_id = action.primary_id
WHERE 1 = 1
AND c.site_id IN ( 1, 2 )
AND c.type IN ( 'topic' )
AND c.status = 'visible'
AND ( c.lock = 0
OR c.site_id = 1 )
AND ( c.level IN ( 0 )
OR ( c.level IN ( 10 )
AND action.user_id = 123
AND action.type IN ( 'group_follow' ) ) )
ORDER BY c.date_updated DESC
LIMIT 20
Here are the current stats:
For content, I have indexes on:
For actions, I have indexes on:
Any help and suggestions would be much appreciated.
Thanks all!
Since you're doing a "ORDER BY c.date_updated DESC LIMIT 20", you probably want an index on c.date_updated. That way, mysql can scan that table first in reverse date_updated
order and stop as soon as it gets the 20 rows.
You should definitely use EXPLAIN
to check your query before and after the change, to see if the optimizer selects that order. There are ways to force the use of the date_updated
index if the optimizer doesn't select it naturally.
in where clause you must have order. firsts must be a column that have a index.
for example for index type_status_updateddate (type, status, date_updated)
if we write
where status = 'blabla' and type = '1'
index not using nut
where type='1' and status='blala'
use index.
mysql use index if column is left in index(in multi column index). for example for index type_status_updateddate (type, status, date_updated)
if we write where status = 'blablabla' index not using
this fact do using multicolumn indexes not cool
and
c.type IN ( 'topic' )
may be replace on
c.type = 'topic'
sorry for my english.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.