2014-09-03 113 views
0

我與表下面的SELECT查詢連接,並將其帶到一分鐘左右返回6條記錄:MySQL的SELECT查詢與加入的時間太長

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 ; 

回答

2

通過對specimen.topography_index一個子,你問的數據庫發現,如果在topography_index存在的價值之前執行對樣品表中每一行該計算。解決這個問題的一種方法是存儲將與topography_index匹配的實際整數值,而不是嵌入該值的字符串。

+0

好的,所以如果我做了沒有子字符串的查找IN,它應該工作得更快?也就是說,將實際值放入數組中。 – IlludiumPu36 2014-09-03 03:24:31

+0

是的,現在快得多,雖然還需要幾秒鐘,也許是4秒。我只需在頁面上放置一個加載小部件。 – IlludiumPu36 2014-09-03 03:32:03