2016-09-21 138 views
1

正在使用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. 

我不明白什麼想法,我還能爲這個查詢優化做的,請提供思路,如果你有

回答

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' 
)