查询效果问题

时间:2016-09-21 06:49:40

标签: php mysql

正在使用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 log(查询分析: 表 - 提示):

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.

我不知道我可以为此查询优化做些什么,如果您有

请提供意见

1 个答案:

答案 0 :(得分:1)

请确保您拥有适当的索引:

  featured_company.DATE_START
  featured_company.PORTAL_ID

  job.IS_ACTIVE
  job.IS_DELETED
  job.EXPIRATION_DATE
  job.ACTIVATION_DATE

最终      company.IMAGE

假设id已被索引

  company.ID
  featured_company.COMPANY_ID     
  job.COMPANY_ID 

以及基于您不使用聚合函数的事实的建议不要使用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' 
)