sql - mysql查询计划中的意外索引扫描

标签 sql mysql query-optimization

我正在对具有唯一列的连接进行索引扫描;它声称正在检查大量行,即使它只查找一行。

这是查询:

    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;

添加/删除限制子句不会更改查询计划。我希望它会在推文的 created_at 索引上扫描等于 limit 子句中的行数的行,然后针对 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                                  | 
+----+-------------+-------+-------+---------------+-------------+---------+------+--------+----------------------------------------------+

请注意 100126index 扫描检查的行数和连接顺序中第二个表的 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;

最佳答案

原因是 twitter_handle 中的 handle 是 charset ascii,而 tweets 中的 screen_name 是 latin1!

关于sql - mysql查询计划中的意外索引扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3876941/

相关文章:

php - MYSQL对当前行计数子查询

MySql 查询花费大量时间

mysql - 仅获取给定列值的一行

mysql - 合并两个表中的列,然后与另一个表连接

sql - 如何在 SQL 的特定行中找到最少的非空列?

sql - 从表中选择几个 max()

mysql - 使用MySQL计算存在和不存在的数量

Mysql - 加速 200 万行的选择查询

php - 如何优化我当前的Mysql查询

sql - 什么时候在 SQL Server 2005 中限制联接是有利的?