如何提高mysql查询性能?

时间:2016-06-10 07:06:45

标签: mysql

我有这个mysql查询,但它需要10秒才能返回数据....我应该如何改进它...?

select `vw_org_products`.*, 
(CASE WHEN prod_status_code = 'SELL' THEN 1000 ELSE
        (select count(1) from org_product_prices
           where org_product_id = vw_org_products.id
           and base_price>0 and deleted_at is null
          and prod_status_code = 'SELL' 
          and price_date = (select max(p2.price_date)
                                                                                from org_product_prices p2
                                                                               where p2.deleted_at is null
                                                                                 and p2.num_units = org_product_prices.num_units
                                                                                 and p2.org_product_id = org_product_prices.org_product_id
                                                                                 and p2.sell_organisation_id = org_product_prices.sell_organisation_id)
                                                            )
                                                        END) as base_sort_rank, (select count(access_key) from access_history
                                                                                where vw_org_products.id = access_history.access_key
                                                                                and access_history.created_at >= DATE_SUB(CURDATE(), INTERVAL 90 DAY)
                                                                                and access_history.access_type = "OrgProduct.View"
                                                                                and access_history.product_id = vw_org_products.product_id 
                                                                                GROUP BY  access_history.product_id) as views from `vw_org_products` 
                                                                                where `vw_org_products`.`deleted_at` is null and `vw_org_products`.`product_id` = "1" 
                                                                                and `prod_status_code` in ("COMINGSOON","MFG_DISCONTINUED","OUTOFSTOCK","PREBOOK","SELL") 
                                                                                order by `base_sort_rank` desc, `views` desc, `updated_at` desc limit 1

谢谢!

0 个答案:

没有答案