2012-04-02 27 views
0

我正在處理一個大的MySQL問題。看看這些查詢。當我執行JOIN時,MySQL不使用索引

CREATE TABLE `Users_Coupons_Views` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, 
`user_id` bigint(20) NOT NULL, 
`coupon_id` bigint(20) unsigned NOT NULL, 
`pos` int(10) unsigned DEFAULT NULL, 
`insert_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 
PRIMARY KEY (`id`), 
KEY `idx_cid` (`user_id`,`coupon_id`), 
KEY `idx_coupon_id` (`coupon_id`) 
) ENGINE=MyISAM AUTO_INCREMENT=35423 DEFAULT CHARSET=utf8 

CREATE TABLE `Track_Clicks_Processed` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, 
`track_id` bigint(20) unsigned NOT NULL, 
`user_id` bigint(20) unsigned NOT NULL, 
`ip` bigint(16) unsigned DEFAULT NULL, 
`coupon_id` bigint(20) unsigned NOT NULL DEFAULT '0', 
`merchant_id` int(11) NOT NULL DEFAULT '-1', 
`pos` int(11) NOT NULL DEFAULT '-1', 
`city` varchar(32) NOT NULL, 
`src` varchar(32) NOT NULL, 
`medium` varchar(32) NOT NULL, 
`kw` varchar(32) NOT NULL, 
`ref` varchar(255) NOT NULL, 
`user_agent` varchar(512) NOT NULL, 
`insert_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 
`ingr` tinyint(1) NOT NULL DEFAULT '0', 
`lang` char(2) NOT NULL DEFAULT 'it', 
PRIMARY KEY (`id`), 
KEY `idx_user_id` (`user_id`), 
KEY `idx_coupon_id` (`coupon_id`), 
KEY `idx_insert_time` (`insert_time`), 
KEY `idx_track_id` (`track_id`), 
KEY `idx_A1` (`track_id`), 
KEY `idx_cid` (`track_id`,`coupon_id`) 
) ENGINE=MyISAM AUTO_INCREMENT=2997731 DEFAULT CHARSET=utf8 

查詢:

EXPLAIN SELECT V.coupon_id, V.user_id, V.insert_time 
FROM Track_Clicks_Processed AS C 
JOIN Users_Coupons_Views AS V ON (C.coupon_id = V.coupon_id 
AND C.track_id = V.user_id) 

這裏的結果:

id select_type  table type possible_keys key  key_len  ref  rows Extra 
1 SIMPLE V ALL  idx_cid,idx_coupon_id NULL NULL NULL 17711 
1 SIMPLE C ref  idx_coupon_id,idx_track_id,idx_A1,idx_cid idx_cid 16 yoodeal.V.user_id,yoodeal.V.coupon_id 2 Using where; Using index 

它不使用第一表的索引!爲什麼?

回答

0

正如你在解釋查詢中看到的,MySQL從第一個表中選擇ALL行。這意味着索引是無用的,因爲所有的數據已經被加載。