多个表的视图使网站变慢

Posted

技术标签:

【中文标题】多个表的视图使网站变慢【英文标题】:view of multiple tables makes site slow 【发布时间】:2015-03-19 17:25:24 【问题描述】:

我开发了一个门户,其中有多个表格。 为了获得搜索结果,我创建了一个视图,该视图从多个表中获取数据。搜索时加载数据需要花费太多时间。我不知道如何解决它。请帮帮我。

这是我创建的视图

CREATE VIEW `salontrip`.`view_search_result` AS (
SELECT
  `salontrip`.`tbl_salon_info`.`salon_id`           AS `salon_id`,
  `salontrip`.`tbl_salon_info`.`salon_name`         AS `salon_name`,
  `salontrip`.`tbl_salon_info`.`land_mark`          AS `land_mark`,
  `salontrip`.`tbl_salon_info`.`salon_address`      AS `salon_address`,
  `salontrip`.`tbl_salon_info`.`gender`             AS `gender`,
  `salontrip`.`tbl_salon_city`.`city_name`          AS `city_name`,
  `salontrip`.`tbl_salon_locality`.`local_name`     AS `local_name`,
  `salontrip`.`tbl_salon_cashback`.`cashback_title` AS `cashback_title`,
  `salontrip`.`tbl_salon_cashback`.`satrt_date`     AS `cashback_start_date`,
  `salontrip`.`tbl_salon_cashback`.`end_date`       AS `cashback_end_date`,
  (SUM(`salontrip`.`tbl_salon_rating`.`rating_value`) / COUNT(`salontrip`.`tbl_salon_rating`.`rating_value`)) AS `avg_rating`,
  GROUP_CONCAT(DISTINCT `view_salon_categories`.`sub_cat_name` SEPARATOR ', ') AS `categories`,
  GROUP_CONCAT(DISTINCT `salontrip`.`tbl_salon_services`.`service_cat` SEPARATOR ', ') AS `service_category`,
  CONCAT('',IFNULL(GROUP_CONCAT(DISTINCT `salontrip`.`tbl_salon_brands`.`brand_name` SEPARATOR '    '),'')) AS `brands`,
  MIN(`salontrip`.`tbl_salon_services`.`price_value`) AS `min_price`,
  MAX(`salontrip`.`tbl_salon_services`.`price_value`) AS `max_price`
FROM (((((((`salontrip`.`tbl_salon_info`
         JOIN `salontrip`.`tbl_salon_city`
           ON ((`salontrip`.`tbl_salon_city`.`city_id` = `salontrip`.`tbl_salon_info`.`city_id`)))
        JOIN `salontrip`.`tbl_salon_locality`
          ON ((`salontrip`.`tbl_salon_locality`.`local_id` = `salontrip`.`tbl_salon_info`.`local_id`)))
       LEFT JOIN `salontrip`.`view_salon_categories`
         ON ((`view_salon_categories`.`salon_id` = `salontrip`.`tbl_salon_info`.`salon_id`)))
      LEFT JOIN `salontrip`.`tbl_salon_services`
        ON ((`salontrip`.`tbl_salon_services`.`salon_id` = `salontrip`.`tbl_salon_info`.`salon_id`)))
     LEFT JOIN `salontrip`.`tbl_salon_brands`
       ON ((`salontrip`.`tbl_salon_brands`.`salon_id` = `salontrip`.`tbl_salon_info`.`salon_id`)))
    LEFT JOIN `salontrip`.`tbl_salon_cashback`
      ON ((`salontrip`.`tbl_salon_cashback`.`salon_id` = `salontrip`.`tbl_salon_info`.`salon_id`)))
   LEFT JOIN `salontrip`.`tbl_salon_rating`
     ON ((`salontrip`.`tbl_salon_rating`.`salon_id` = `salontrip`.`tbl_salon_info`.`salon_id`)))
WHERE (`salontrip`.`tbl_salon_info`.`is_trashed` = 0)
GROUP BY `salontrip`.`tbl_salon_info`.`salon_id`)

这是之前运行的查询。

SELECT *
FROM (`view_search_result`)
WHERE (`salon_name` like '%%' or `service_category` like '%%' or `brands` like '%%' or `local_name` like '%%' or `salon_address` like '%%' or `land_mark` like '%%')
AND  `city_name`  LIKE '%Gurgaon%'
AND  `local_name`  LIKE '%%'
AND  `service_category`  LIKE '%%'
AND  `brands`  LIKE '%%'
AND  `categories`  LIKE '%%'

任何解决问题的想法将不胜感激。

提前致谢。

【问题讨论】:

【参考方案1】:

确保所有链接并用于过滤的字段都已编入索引。这将使查询速度提高 1000 倍。

【讨论】:

感谢@PrestonDocks。效果很好,我错过了这个。

以上是关于多个表的视图使网站变慢的主要内容,如果未能解决你的问题,请参考以下文章

如何改进包含存储过程使用的多个自联接的视图

UIButton 图层样式使应用程序变慢

具有多个计算的Mysql查询变慢

依赖于多个表的可更新视图

创建具有多个表的视图?

在 viewwillappear 中添加自定义导航栏会使导航变慢?