查询性能问题


Query performance issue

我正在使用mySql,下面的查询我得到性能问题:

SELECT COUNT(*)
FROM
  (SELECT company.ID
   FROM `company`
   INNER JOIN `featured_company` ON (company.ID=featured_company.COMPANY_ID)
   INNER JOIN `company_portal` ON (company.ID=company_portal.COMPANY_ID)
   INNER JOIN `job` ON company.ID = job.COMPANY_ID
   WHERE featured_company.DATE_START<='2016-09-21'
     AND featured_company.DATE_END>='2016-09-21'
     AND featured_company.PORTAL_ID=16
     AND company_portal.PORTAL_ID=16
     AND (company.IMAGE IS NOT NULL
          AND company.IMAGE<>'')
     AND job.IS_ACTIVE=1
     AND job.IS_DELETED=0
     AND job.EXPIRATION_DATE >= '2016-09-21'
     AND job.ACTIVATION_DATE <= '2016-09-21'
   GROUP BY company.ID)

用这个查询得到下面的newrelic日志(查询分析:表-提示):

featured_company

- The table was retrieved with this index: portal_date_start_end
- A temporary table was created to access this part of the query, which can cause poor performance. This typically happens if the query contains GROUP BY and ORDER BY clauses that list columns differently.
- MySQL had to do an extra pass to retrieve the rows in sorted order, which is a cause of poor performance but sometimes unavoidable.
- You can speed up this query by querying only fields that are within the index. Or you can create an index that includes every field in your query, including the primary key.
Approximately 89 rows of this table were scanned.

company_portal

- The table was retrieved with this index: PRIMARY
- Approximately 1 row of this table was scanned.

- The table was retrieved with this index: company_expiration_date
- You can speed up this query by querying only fields that are within the index. Or you can create an index that includes every field in your query, including the primary key.
- Approximately 37 rows of this table were scanned.
公司

- The table was retrieved with this index: PRIMARY
- You can speed up this query by querying only fields that are within the index. Or you can create an index that includes every field in your query, including the primary key.
- Approximately 1 row of this table was scanned.

我不知道我还可以为这个查询优化做些什么,如果你有

请提供想法

确保你有合适的索引:

  featured_company.DATE_START
  featured_company.PORTAL_ID
  job.IS_ACTIVE
  job.IS_DELETED
  job.EXPIRATION_DATE
  job.ACTIVATION_DATE

并最终公司。图片

假设id已经被索引

  company.ID
  featured_company.COMPANY_ID     
  job.COMPANY_ID 

和一个基于你不使用聚合函数的建议不要使用group,而是使用DISTINCT

  company.ID
  featured_company.COMPANY_ID     
  job.COMPANY_ID 
SELECT COUNT(*) FROM (
  SELECT DISTINCT company.ID 
  FROM `company` 
  INNER JOIN `featured_company` ON company.ID=featured_company.COMPANY_ID
  INNER JOIN `company_portal`   ON company.ID=company_portal.COMPANY_ID
  INNER JOIN `job`              ON company.ID = job.COMPANY_ID 
  WHERE featured_company.DATE_START<='2016-09-21' 
  AND featured_company.DATE_END>='2016-09-21' 
  AND featured_company.PORTAL_ID=16 
  AND company_portal.PORTAL_ID=16
  AND (company.IMAGE IS NOT NULL AND company.IMAGE<>'') 
  AND job.IS_ACTIVE=1 
  AND job.IS_DELETED=0 
  AND job.EXPIRATION_DATE >= '2016-09-21' 
  AND job.ACTIVATION_DATE <= '2016-09-21' 
)