2014-09-28 71 views
0

我還有一個問題,當我用內部查詢JOIN慢MySQL查詢與內部聯接0846秒

此查詢

SELECT * 
FROM `engine4_product_file` INNER JOIN 
    `engine4_file` 
    ON engine4_product_file.fid = engine4_file.id 
WHERE engine4_product_file.pid IN (3347,3346,3345,3343,3342,3337) and 
     engine4_file.active = 1 AND 
     engine4_file.ext IN ('jpg','gif','png','jpeg') 

這創建表engine4_product_file

CREATE TABLE `engine4_product_file` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `fid` int(11) NOT NULL, 
    `pid` int(11) NOT NULL, 
    PRIMARY KEY (`id`), 
    KEY `engine4_product_file` (`fid`), 
    KEY `pid` (`pid`) 
) ENGINE=InnoDB AUTO_INCREMENT=6549 DEFAULT CHARSET=latin1 

,這創造table engine4_file

CREATE TABLE `engine4_file` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `uid` int(11) NOT NULL, 
    `name` longtext CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL, 
    `url` longtext CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL, 
    `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 
    `active` int(11) NOT NULL DEFAULT '1', 
    `size` int(11) DEFAULT NULL, 
    `ext` varchar(10) DEFAULT NULL, 
    `folder` int(11) NOT NULL DEFAULT '0', 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=48801 DEFAULT CHARSET=latin1 

這個解釋

id select_type  table type possible_keys key  key_len  ref  rows Extra 
1 SIMPLE engine4_product_file range engine4_product_file,pid pid  4 NULL 30 Using where 
1 SIMPLE engine4_file eq_ref PRIMARY  PRIMARY  4 akafine_social2.engine4_product_file.fid 1 Using where 
+2

無論你的表有相同的名字?哪個是哪個?還有什麼'的問題,我只看到30行,真的很慢嗎? – Mihai 2014-09-28 16:56:21

+0

我覺得有一個cut'n'paste錯誤,因爲你添加了兩次相同的表。 – 2014-09-28 17:01:16

+0

我只想獲得爲這個表添加索引的最佳方式 – GoldenFingers 2014-09-30 00:00:03

回答

0

更改您的WHERE條件

WHERE engine4_file.active = 1 AND 
     engine4_file.ext IN ('jpg','gif','png','jpeg') AND 
    engine4_product_file.pid IN (3347,3346,3345,3343,3342,3337) 

添加一個索引

ALTER TABLE engine4_file ADD KEY (active,ext)