0

我有一个相当简单的查询,由于某种原因没有使用任何索引:

CREATE TABLE `events_self` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`date` int(10) unsigned NOT NULL,
`username` varchar(16) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`event` enum('status_update','follow_hashtag','make_hashtag','avatar','placeholder_3') CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`obj_id` varchar(140) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`inline` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
PRIMARY KEY (`id`,`date`),
KEY `obj_id` (`obj_id`),
KEY `user_date` (`username`,`date`),
KEY `event` (`event`),
KEY `x` (`event`,`inline`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


INSERT INTO `events_self` (`id`, `date`, `username`, `event`, `obj_id`, `inline`) VALUES (1, 1358359266, 'aaa', 'make_hashtag', '1', 'scene');

SELECT inline FROM events_self  WHERE obj_id = 1;

EXPLAIN SELECT inline FROM events_self  WHERE obj_id = 1;


+----+-------------+-------------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table       | type | possible_keys | key  | key_len | ref  | rows    | Extra       |
+----+-------------+-------------+------+---------------+------+---------+------+---------+-------------+
|  1 | SIMPLE      | events_self | ALL  | obj_id        | NULL | NULL    | NULL | 1610702 | Using where |
+----+-------------+-------------+------+---------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec)

服务器版本:5.3.2-MariaDB-beta-mariadb102~lenny-log (MariaDB - http://mariadb.com/ )

我应该保持表/查询不变还是最好强制使用索引?

4

1 回答 1

3

试试这个

EXPLAIN SELECT inline FROM events_self  WHERE obj_id = "1";

由于您发送了整数,因此 MySql 不会将索引视为类型不同。

于 2013-01-17T11:38:40.843 回答