SQL查询耗时太长

时间:2010-05-19 19:36:00

标签: php mysql query-optimization

我正在尝试优化下面列出的SQL查询 它基本上是一个搜索引擎代码,它根据产品名称检索产品。它还会检查产品型号以及是否启用 当我直接通过phpMyAdmin工具运行它时,大约需要1.6秒,但总共需要大约3秒才能与放入的PHP文件一起加载。

我需要添加类别搜索功能,现在崩溃了MySQL服务器,HELP!

贾斯汀

SELECT DISTINCT
    p.products_id,
    p.products_image,
    p.products_price,
    s.specials_new_products_price,
    p.products_weight,
    p.products_unit_quantity,
    pd.products_name,
    pd.products_img_alt,
    pd.products_affiliate_url
FROM
    products AS p
    LEFT JOIN
        vendors v
    ON
        v.vendors_id = p.vendors_id
    LEFT JOIN
        specials AS s
    ON
        s.products_id = p.products_id
        AND
        s.status = 1,
    categories AS c,
    products_description AS pd,
    products_to_categories AS p2c
WHERE
    (
        (
            pd.products_name LIKE '%cleaning%'
            AND
            pd.products_name LIKE '%supplies%'
        )
        OR
        (
            p.products_model LIKE '%cleaning%'
            AND
            p.products_model LIKE '%supplies%'
        )
        OR
        p.products_id = 'cleaning supplies'
        OR
        v.vendors_prefix = 'cleaning supplies'
        OR
            CONCAT (CAST (v.vendors_prefix AS CHAR), '-', CAST (p.products_id AS CHAR)) = 'cleaning supplies'
    )
    AND
    p.products_status = '1'
    AND
    c.categories_status = '1'
    AND
    p.products_id = pd.products_id
    AND
    p2c.products_id = pd.products_id
    AND
    p2c.categories_id = c.categories_id
ORDER BY
    pd.products_name

2 个答案:

答案 0 :(得分:3)

left join specials as s on s.products_id = p.products_id and s.status = 1,

你有一个全表扫描。 InnoDB不能很好地处理这种数据(tinyint,true / false)。在Oracle中,您有类似位图索引的东西,这对低选择性索引很有用。您可以在您的位置使用子查询,如下所示:

...
where 
   p.product_id IN (SELECT S.status FROM specials s WHERE s.status = 1)
AND
   (
          ( pd.products_name like '%cleaning%' and pd.products_name like '%supplies%' ) or 
...

然后,您祈祷查询缓存缓存该子查询的结果。

发布您的解释!

答案 1 :(得分:0)

select p.products_id, p.products_image, p.products_price, 
    p.products_weight, p.products_unit_quantity, 
    s.specials_new_products_price, 
    pd.products_name, pd.products_img_alt, pd.products_affiliate_url 
from products as p 
    left join vendors v ON v.vendors_id = p.vendors_id
    left join specials as s on s.products_id = p.products_id, 
    left join products_description as pd on pd.products_id = p.products_id
    left join products_to_categories as p2c on p2c.products_id = p.products_id 
    left join categories as c on c.categories_id = p2c.categories_id
where 
       (
          ( pd.products_name like '%cleaning%' and pd.products_name like '%supplies%' ) or 
          ( p.products_model like '%cleaning%' and p.products_model like '%supplies%' ) or 
          p.products_id = 'cleaning supplies' or 
          v.vendors_prefix = 'cleaning supplies' or 
          CONCAT( CAST(v.vendors_prefix as char), '-', CAST(p.products_id as char) ) = 'cleaning supplies'
       ) 
    and p.products_status = '1' 
    and s.status = 1
    and c.categories_status = '1'
group by p.products_id
order by pd.products_name

确保设置了以下索引:

create index prod_prodid_status on products(products_id, products_status);
create index vendors_vendorid on vendors(vendors_id);
create index specials_prodid_status on vendors(products_id, `status`);
create index prod_desc_prodid_prodname on products_description(products_id, products_name);
create index prod_cat_prodid_catid on products_to_categories(products_id, categories_id);
create index categories_catid_status on categories(categories_id, categories_status);

我希望名字中所有带“id”的字段都是整数