2010-10-06 86 views
1

我正在對具有唯一列的連接進行索引掃描;它聲稱正在檢查大量的行,即使它只查找一行。在mysql查詢計劃中發生意外的索引掃描

這是查詢:

select t.id, 
      t.twitter_id, 
      t.screen_name, 
      t.text  
     from tweets t 
inner join twitter_handle th on th.handle = t.screen_name 
    order by t.created_at desc 
    limit 1; 

添加/刪除限制條款並不會改變查詢計劃。我希望它會掃描許多等於limit子句中的數字的tweets的created_at索引,然後針對twitter_handle執行eq_ref查找。

根據解釋的查詢計劃,但是,是:

+----+-------------+-------+-------+---------------+-------------+---------+------+--------+----------------------------------------------+ 
| id | select_type | table | type | possible_keys | key   | key_len | ref | rows | Extra          | 
+----+-------------+-------+-------+---------------+-------------+---------+------+--------+----------------------------------------------+ 
| 1 | SIMPLE  | th | index | NULL   | handle  | 32  | NULL | 100126 | Using index; Using temporary; Using filesort | 
| 1 | SIMPLE  | t  | ref | screen_name | screen_name | 17  | func |  2 | Using where         | 
+----+-------------+-------+-------+---------------+-------------+---------+------+--------+----------------------------------------------+ 

注意號碼在連接檢查的索引掃描和ref = FUNC用於第二表的行的訂購。

這個查詢顯示在我的緩慢的查詢日誌中,我很困惑爲什麼mysql選擇這種方式執行查詢。

的架構這兩個表:

CREATE TABLE `twitter_handle` (
    `handle_id` int(11) NOT NULL AUTO_INCREMENT, 
    `handle` varchar(30) CHARACTER SET ascii NOT NULL, 
    `twitter_token_id` int(11) DEFAULT NULL, 
    `name` varchar(255) CHARACTER SET utf8 DEFAULT NULL, 
    `twitter_user_id` int(11) unsigned DEFAULT NULL, 
    `location` varchar(100) CHARACTER SET utf8 DEFAULT NULL, 
    `profile_image_url` varchar(255) CHARACTER SET utf8 DEFAULT NULL, 
    `followers_count` int(11) DEFAULT NULL, 
    `twitter_list_id` int(4) DEFAULT NULL, 
    `last_update` timestamp NULL DEFAULT CURRENT_TIMESTAMP, 
    `bio` varchar(160) CHARACTER SET utf8 DEFAULT NULL, 
    PRIMARY KEY (`handle_id`), 
    UNIQUE KEY `handle` (`handle`), 
    KEY `twitter_token_id` (`twitter_token_id`), 
    KEY `twitter_user_id` (`twitter_user_id`) 
) ENGINE=InnoDB; 

CREATE TABLE `tweets` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `twitter_id` char(15) DEFAULT NULL, 
    `screen_name` varchar(15) NOT NULL, 
    `logged_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 
    `text` char(200) NOT NULL, 
    `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `status` enum('pending','processed','ignored','pending_delete','deleted','pending_tweet','preview') NOT NULL DEFAULT 'pending', 
    `interaction_id` int(11) DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `twitter_id_UNIQUE` (`twitter_id`), 
    UNIQUE KEY `interaction_id_idx` (`interaction_id`), 
    UNIQUE KEY `interaction_id` (`interaction_id`,`status`), 
    KEY `screen_name` (`screen_name`,`created_at`), 
    KEY `status_2` (`status`,`created_at`), 
    KEY `created_at_2` (`created_at`) 
) ENGINE=InnoDB; 
+0

用於表別名和豐富的信息 – 2010-10-06 21:40:17

回答

0

的原因是在twitter_handle手柄是ASCII字符集,但在SCREEN_NAME鳴叫是latin1的!