简体   繁体   English

如何优化mysql查询的性能

[英]How to optimize mysql query for performance

I have five tables with join . 我有五个表join This query takes more than 5 sec to execute. 该查询需要5秒钟以上的时间才能执行。 I have four queries like the one below, so my PHP page takes more than 30 sec to load. 我有以下四个查询,因此我的PHP页面加载时间超过30秒。 I need to improve the performance. 我需要提高性能。 Could you please help me? 请你帮助我好吗? I have struggled for a week. 我已经挣扎了一个星期。 I added indexing. 我添加了索引。

MY QUERY 我的查询

SELECT f.SortField , f.id 
FROM tbl_store_brands AS sb 
INNER JOIN tbl_products AS p
    ON sb.categoryID = p.pkCategory AND sb.brandID = p.pkBrand 
INNER JOIN ModelPrice AS mp ON (p.id = mp.product_id) 
INNER JOIN Brand_Data AS bd ON bd.pkID = p.pkBrand+0 
INNER JOIN tbl_filters AS f
    ON (
        p.pkID = f.pkID and
        p.pkCategory = f.Category AND
        p.cgSlug = 'bathroom-fixtures'
    ) 
WHERE mp.Available = 1
GROUP BY f.SortField
ORDER BY if (
    f.SortField = 'CAPACITY' OR
    f.SortField = 'WIDTH' OR
    f.SortField = 'HEIGHT' OR
    f.SortField = 'DEPTH', 4, f.ValueDisplayOrder
) ASC

MY Query EXPLANATION 我的查询说明 在此处输入图片说明

Few modification can be done, to improve you query performance. 可以进行一些修改,以提高查询性能。 1. Add index for ValueDisplayOrder, this will improve the sorting performance. 1.为ValueDisplayOrder添加索引,这将提高排序性能。 2. Avoid in query per functioning. 2.避免按功能查询。 Remove the "p.pkBrand+0", this add the execution process time. 删除“ p.pkBrand + 0”,这将增加执行过程的时间。 If possible remove the if clause from order by and do the ordering in your PHP code. 如果可能,请从order by中删除if子句,然后在您的PHP代码中进行排序。 3. Move the "p.cgSlug = 'bathroom-fixtures'" code to on clause of product table join to narrow down the inner join result. 3.将“ p.cgSlug ='bathroom-fixtures'”代码移动到产品表联接的on子句中,以缩小内部联接结果的范围。

Your modified query will look like: 您修改后的查询将如下所示:

SELECT f.SortField , f.id 
FROM tbl_store_brands AS sb 
INNER JOIN tbl_products AS p
    ON sb.categoryID = p.pkCategory AND sb.brandID = p.pkBrand AND  p.cgSlug = 'bathroom-fixtures'
INNER JOIN ModelPrice AS mp ON (p.id = mp.product_id) 
INNER JOIN Brand_Data AS bd ON bd.pkID = p.pkBrand
INNER JOIN tbl_filters AS f
    ON (
        p.pkID = f.pkID and
        p.pkCategory = f.Category

    ) 
WHERE mp.Available = 1
GROUP BY f.SortField
ORDER BY f.ValueDisplayOrder
 ASC

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

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