dousi1906 2013-05-21 14:17
浏览 148
已采纳

搜索查询慢,记录集基于视图。 如何加快速度?

I have a recordset based on a view in MySQL that I use to return search results but it is painfuilly slow (consistently 21 seconds!). A similar search in the same environment takes under a second.

I fear that it is the view that is slowing things down since I have four left joins and one subquery in there to make related data available in the search.

Is there any general guidance for speeding up a query when using a view? I have researched indexing but it seems that is not allowed in MySQL in views.

Thanks in advance for any suggestions.

The code to create my 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   
  • 写回答

1条回答 默认 最新

  • dongyan4157 2013-05-21 15:07
    关注

    You need to ensure that you have indexes on the underlying tables, not on the view. The view should use such tables.

    The first index that screams out is on productimages(productid, productimage). This will speed up the subquery in the select clause.

    You should also have primary key indexes for what look like primary keys on all the tables . . . categories(categoryid), producttype(typeid), watchbrands(brandid), and (I think) productfeatures(productid, featureid).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)