简体   繁体   English

提高MySQL查询的性能

[英]Improve performance of a MySQL query

I have a MySQL query that is currently running at 2.5 seconds and I want to trim it down to 2 seconds max. 我有一个MySQL查询,当前正在以2.5秒的速度运行,我想将其减少到最大2秒。

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: 以下是当前统计信息:

  • table content has 55k rows 表内容有55k行
  • table actions has 87k rows 表操作有87k行

For content, I have indexes on: 对于内容,我有以下索引:

  • parent_id (parent_id) parent_id(parent_id)
  • user_id (user_id) user_id(user_id)
  • type_status_date (type,status, date) type_status_date(类型,状态,日期)
  • type_status_updateddate (type, status, date_updated) type_status_updateddate(类型,状态,date_updated)
  • site_id (site_id) site_id(site_id)
  • type_status_level (type, status, level) type_status_level(类型,状态,级别)
  • type_parentid_level (type, parent_id, level) type_parentid_level(类型,parent_id,级别)

For actions, I have indexes on: 对于操作,我具有以下指标:

  • primary_id (primary_id) primary_id(primary_id)
  • site_id (site_id) site_id(site_id)
  • type (type) 类型(type)

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. 由于您正在执行“ ORDER BY c.date_updated DESC LIMIT 20”,因此您可能希望在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. 这样,mysql可以首先以相反的date_updated顺序扫描该表,并在获得20行后立即停止。

You should definitely use EXPLAIN to check your query before and after the change, to see if the optimizer selects that order. 绝对应该在更改前后使用EXPLAIN来检查查询,以查看优化程序是否选择了该顺序。 There are ways to force the use of the date_updated index if the optimizer doesn't select it naturally. 如果优化器没有自然选择它,则有多种方法可以强制使用date_updated索引。

in where clause you must have order. 在where子句中,您必须有顺序。 firsts must be a column that have a index. firsts必须是具有索引的列。

for example for index type_status_updateddate (type, status, date_updated) 例如索引type_status_updateddate(类型,状态,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). 如果列留在索引中(多列索引),mysql使用索引。 for example for index type_status_updateddate (type, status, date_updated) 例如索引type_status_updateddate(类型,状态,date_updated)

if we write where status = 'blablabla' index not using 如果我们在不使用status ='blablabla'索引的地方写

this fact do using multicolumn indexes not cool 这个事实确实使用多列索引不酷

and

c.type IN ( 'topic' )

may be replace on 可能会替换

c.type = 'topic' 

sorry for my english. 对不起我的英语不好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM