Mysql查询运行时间太长

时间:2013-06-14 16:27:34

标签: mysql

查询耗时太长,12秒。 wp_podsrel - 25000条记录,wp_pods_bars - 1200

SELECT SQL_CALC_FOUND_ROWS DISTINCT `t`.* 
FROM `wp_pods_bars` AS `t` LEFT JOIN `wp_podsrel` AS `rel_city` 
    ON ( `rel_city`.`field_id` = 13918 AND `rel_city`.`item_id` = `t`.`id` ) 
    OR ( `rel_city`.`related_field_id` = 13918 AND `rel_city`.`related_item_id` = `t`.`id` ) 

表的结构

CREATE TABLE `wp_pods_bars` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(128) DEFAULT NULL,
  `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `description` longtext,
  `keywords` longtext,
  `seo_title` varchar(128) DEFAULT NULL,
  `seo_description` varchar(128) DEFAULT NULL,
  `main_homepage_feature` tinyint(1) DEFAULT '0',
  `main_homepage_priority` decimal(12,2) DEFAULT NULL,
  `favourite` tinyint(1) DEFAULT '0',
  `slug` varchar(200) DEFAULT NULL,
  `address` longtext,
  `location` varchar(255) DEFAULT NULL,
  `website` varchar(128) DEFAULT NULL,
  `longtitude` varchar(128) DEFAULT NULL,
  `latitude` varchar(128) DEFAULT NULL,
  `phone` varchar(128) DEFAULT NULL,
  `opening_hours` varchar(128) DEFAULT NULL,
  `hide` tinyint(1) DEFAULT '0',
  `best` tinyint(1) DEFAULT '0',
  `what_to_order` longtext,
  `gay_bar` tinyint(1) DEFAULT '0',
  `opening_soon` tinyint(1) DEFAULT '0',
  `opening_date` varchar(255) DEFAULT NULL,
  `opening_date_2` date NOT NULL DEFAULT '0000-00-00',
  PRIMARY KEY (`id`),
  KEY `hide` (`hide`),
  KEY `opening_soon` (`opening_soon`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1497 ;

CREATE TABLE `wp_pod_rel` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `pod_id` bigint(15) unsigned DEFAULT NULL,
  `sister_pod_id` bigint(15) unsigned DEFAULT NULL,
  `field_id` int(10) unsigned DEFAULT NULL,
  `tbl_row_id` bigint(15) unsigned DEFAULT NULL,
  `weight` int(10) unsigned DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `field_pod_idx` (`field_id`,`pod_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=88681 ;

有什么方法可以优化它吗?感谢

1 个答案:

答案 0 :(得分:1)

首先,你为什么要使用不同的引擎? 第二,尝试将您的查询拆分为

 SELECT SQL_CALC_FOUND_ROWS DISTINCT `t`.* 
 FROM `wp_pods_bars` AS `t` LEFT JOIN `wp_pod_rel` AS `rel_city` 
 ON (`rel_city`.`item_id` = `t`.`id` ) WHERE  `rel_city`.`field_id` = 13918

 SELECT SQL_CALC_FOUND_ROWS DISTINCT `t`.* 
 FROM `wp_pods_bars` AS `t` LEFT JOIN `wp_pod_rel` AS `rel_city` 
 ON (`rel_city`.`related_item_id` = `t`.`id` ) WHERE `rel_city`.`related_field_id` = 13918

看看结果是什么。