2012-04-02 52 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行。这意味着索引是无用的,因为所有的数据已经被加载。