2012-04-02 14 views
0

Ich habe mit einem großen MySQL-Problem zu tun. Sehen Sie sich diese Abfragen an.MySQL verwendet den Index nicht, wenn ich ein JOIN

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 

Abfrage:

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) 

Hier ist das Ergebnis:

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 

Es ist nicht den Index für die erste Tabelle mit! Warum?

Antwort

0

Wie Sie in Ihrer EXPLAIN-Abfrage sehen können, wählt MySQL ALL Zeilen aus der ersten Tabelle. Dies bedeutet, dass ein Index nutzlos ist, da alle Daten bereits geladen sind.

Verwandte Themen