0

我有一个基于 MySQL 视图的记录集,用于返回搜索结果,但速度非常慢(始终为 21 秒!)。在同一环境中进行类似的搜索需要不到一秒钟的时间。

我担心是视图会减慢速度,因为我有四个左连接和一个子查询,以便在搜索中提供相关数据。

使用视图时是否有任何关于加快查询速度的一般指导?我已经研究过索引,但似乎 MySQL 在视图中是不允许的。

在此先感谢您的任何建议。

创建我的视图的代码:

CREATE VIEW vproducts2 AS  
SELECT products2.productid, products2.active, products2.brandid,
    products2.createddate, products2.description, products2.inventorynum,
    products2.onhold, products2.price, products2.refmodnum, products2.retail,
    products2.sefurl, products2.series, products2.sold,
    `producttype`.`type` AS type, categories.category AS category,  
    `watchbrands`.`brand` AS brand, productfeatures.productfeaturevalue AS size,  
    (SELECT productimages.image
        FROM productimages
        WHERE productimages.productid = products2.productid
        LIMIT 1
    ) AS pimage  
FROM products2  
    LEFT JOIN producttype ON producttype.typeid = products2.typeid  
    LEFT JOIN categories ON categories.categoryid = products2.categoryid  
    LEFT JOIN watchbrands ON watchbrands.brandid = products2.brandid  
    LEFT JOIN productfeatures ON productfeatures.productid = products2.productid
        AND productfeatures.featureid = 1   
4

1 回答 1

0

您需要确保在基础表上而不是视图上有索引。视图应该使用这样的表。

尖叫的第一个索引是 on productimages(productid, productimage)。这将加速select子句中的子查询。

对于所有表上的主键,您还应该有主键索引。. . categories(categoryid), producttype(typeid),watchbrands(brandid)和(我认为)productfeatures(productid,featureid)。

于 2013-05-21T15:07:18.787 回答