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

这是我创建的视图

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 '%%'


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

提前致谢。

最佳答案

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

关于mysql - 多个表的 View 使站点变慢,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29151149/

10-10 10:56