简体   繁体   中英

MySQL SELECT query with joins takes too long

I have the following SELECT query with table joins and it taking about a minute to return 6 records:

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)."')

Any ideas what I should here?

Table structures are:

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 ;

By performing a substring on specimen.topography_index, you're asking the database to perform that calculation on every row in the specimen table before finding if the value exists in topography_index. One way to address this is to store the actual integer value that will match with topography_index, rather than a string with that value embedded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM