带连接的MySQL SELECT查询耗时太长

时间:2014-09-03 02:57:50

标签: mysql sql

我有以下SELECT查询和表连接,大约需要一分钟才能返回6条记录:

SELECT * FROM specimen, topography_index, morphology, specimen_image_lookup, image 
WHERE 
SUBSTRING(specimen.topography_index, 2, 2) = topography_index.topography_index_code 
AND 
morphology.morphology_code = specimen.snop_code 
AND 
specimen_image_lookup.specimen_fk = specimen.specimen_pk 
AND 
image.image_pk = specimen_image_lookup.image_fk 
AND 
specimen.topography_index, 2, 2) IN('".implode("','",$system)."')

我应该在这里找到什么想法?

表结构是:

CREATE TABLE `specimen` (
  `specimen_pk` int(4) NOT NULL AUTO_INCREMENT,
  `number` varchar(20) NOT NULL,
  `unit_number` varchar(10) NOT NULL,
  `topography_index` varchar(5) NOT NULL DEFAULT '',
  `snop_axis` char(1) NOT NULL,
  `snop_code` varchar(4) NOT NULL,
  `example` int(2) NOT NULL,
  `gender` char(1) NOT NULL,
  `age` varchar(3) NOT NULL DEFAULT 'NA',
  `clinical_history` text NOT NULL,
  `specimen` text NOT NULL,
  `macroscopic` text NOT NULL,
  `microscopic` text NOT NULL,
  `conclusion` text NOT NULL,
  `comment` text NOT NULL,
  `room` char(1) NOT NULL,
  `position` varchar(8) NOT NULL,
  `created` datetime NOT NULL,
  `created_by` int(3) NOT NULL,
  `updated` datetime NOT NULL,
  `updated_by` int(3) NOT NULL,
  PRIMARY KEY (`specimen_pk`),
  FULLTEXT KEY `clinical_history` (`clinical_history`),
  FULLTEXT KEY `specimen` (`specimen`),
  FULLTEXT KEY `macroscopic` (`macroscopic`),
  FULLTEXT KEY `microscopic` (`microscopic`),
  FULLTEXT KEY `conclusion` (`conclusion`),
  FULLTEXT KEY `comment` (`comment`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=500 ;

CREATE TABLE `topography_index` (
  `topography_index_pk` int(3) NOT NULL AUTO_INCREMENT,
  `topography_index_code` varchar(2) DEFAULT NULL,
  `topography_index_nomen` text,
  PRIMARY KEY (`topography_index_pk`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=138 ;


CREATE TABLE `specimen_image_lookup` (
  `specimen_image_lookup_pk` int(8) NOT NULL AUTO_INCREMENT,
  `specimen_fk` int(4) NOT NULL,
  `image_fk` int(4) NOT NULL,
  PRIMARY KEY (`specimen_image_lookup_pk`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=141 ;

CREATE TABLE `morphology` (
  `morphology_pk` int(6) NOT NULL AUTO_INCREMENT,
  `morphology_code` varchar(4) NOT NULL,
  `morphology_nomen` varchar(120) NOT NULL,
  PRIMARY KEY (`morphology_pk`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2295 ;

CREATE TABLE `image` (
  `image_pk` int(4) NOT NULL AUTO_INCREMENT,
  `image_title` varchar(80) NOT NULL,
  `image_description` text NOT NULL,
  `image_thumbnail` varchar(100) NOT NULL,
  `image_small` varchar(100) NOT NULL,
  `image_large` varchar(100) NOT NULL,
  `created` datetime NOT NULL,
  `created_by` int(3) NOT NULL,
  `updated` datetime NOT NULL,
  `updated_by` int(3) NOT NULL,
  PRIMARY KEY (`image_pk`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=138 ;

1 个答案:

答案 0 :(得分:2)

通过在samples.topography_index上执行子字符串,您要求数据库在样本表中的每一行上执行该计算,然后查找topography_index中是否存在该值。解决此问题的一种方法是存储与topography_index匹配的实际整数值,而不是嵌入该值的字符串。