SQL 查询耗时过长

Posted

技术标签:

【中文标题】SQL 查询耗时过长【英文标题】:SQL Query taking too long 【发布时间】:2010-05-19 19:35:59 【问题描述】:

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

我需要添加一个类别搜索功能,现在这导致 mysql 服务器崩溃,求助!

贾斯汀

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

【问题讨论】:

你能发布EXPLAIN your_query的输出吗? 如果你有很多行,那么你应该使用 Mysql 索引。谷歌一下它们,它可以使查询更快。 没有搜索部分的查询需要多长时间;是搜索还是连接减慢了速度? 【参考方案1】:
left join specials as s on s.products_id = p.products_id and s.status = 1,

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

...
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 
...

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

发表你的解释!

【讨论】:

【参考方案2】:
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”的字段都是整数

【讨论】:

以上是关于SQL 查询耗时过长的主要内容,如果未能解决你的问题,请参考以下文章

PostgreSQL 查询耗时过长

使用 HAVING 查询耗时过长

Mongodb 查询执行耗时过长

Mongodb 查询耗时过长

PostGIS 查询耗时过长。 >400ms

由于 where 子句中的 <>,查询耗时过长