php - 搜索查询速度慢,基于 View 的记录集。如何加快速度?

标签 php mysql search

我有一个基于 MySQL 中 View 的记录集,我用它来返回搜索结果,但它非常慢(始终是 21 秒!)。在相同环境中进行类似搜索只需不到一秒。

我担心 View 会减慢速度,因为我有四个左连接和一个子查询来使相关数据在搜索中可用。

使用 View 时是否有加速查询的一般指导?我研究过索引,但似乎 MySQL 的 View 中不允许这样做。

预先感谢您的任何建议。

创建 View 的代码:

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   

最佳答案

您需要确保底层表上有索引,而不是 View 上。 View 应该使用这样的表。

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

您还应该在所有表上拥有看起来像主键的主键索引。 。 。 categories(categoryid) , producttype(typeid) , watchbrands(brandid) ,和(我认为)productfeatures(productid, featureid)。

关于php - 搜索查询速度慢,基于 View 的记录集。如何加快速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16672326/

相关文章:

php - mysql 子查询有多于一列

javascript - jQuery 代码阻止将产品插入数据库

如果计数为零,MySQL 执行子查询

MySQL - 带连接的 GROUP_CONCAT

html5搜索输入事件

c# - 将搜索字符串转换为与全文兼容的搜索字符串?

php - WideImage 无法在我的服务器上运行

php - JS 文件在 WordPress 中调用两次

php - 网站链接在 google chrome 上不起作用,但在 firefox 上工作正常

java - 我可以自定义 Android searchable.xml 布局吗?