首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用联接的MySQL选择查询花费的时间过长

使用联接的MySQL选择查询花费的时间过长
EN

Stack Overflow用户
提问于 2014-09-03 02:57:49
回答 1查看 72关注 0票数 0

我有以下带有表联接的SELECT查询,返回6条记录大约需要一分钟时间:

代码语言:javascript
复制
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)."')

知道我在这里该怎么做吗?

表结构是:

代码语言:javascript
复制
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 ;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-03 03:14:14

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25635540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档