多个表的视图使站点变慢

时间:2015-03-19 17:25:24

标签: mysql codeigniter join view group-by

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

这是我创建的视图

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 个答案:

答案 0 :(得分:1)

确保已链接并用于过滤的所有字段都已编制索引。这将加快查询速度,1000倍。