+ -
当前位置:首页 → 问答吧 → 这个查询为什么没有使用索引?

这个查询为什么没有使用索引?

时间:2011-09-13

来源:互联网

表结构:
SQL code

Create Table: CREATE TABLE `sina_weibo_status` (
  `sid` bigint(20) NOT NULL DEFAULT '0',
  `text` varchar(255) DEFAULT NULL,
  `source` varchar(255) DEFAULT NULL,
  `truncated` int(1) DEFAULT NULL ,
  `thumbnail_pic` varchar(255) DEFAULT NULL,
  `bmiddle_pic` varchar(255) DEFAULT NULL,
  `original_pic` varchar(255) DEFAULT NULL,
  `uid` bigint(20) DEFAULT NULL COMMENT,
  `retweeted_status` bigint(20) DEFAULT '0',
  `in_reply_to_status_id` bigint(20) DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `aid` int(10) NOT NULL COMMENT,
  `category` int(10) NOT NULL,
  `status` tinyint(4) NOT NULL DEFAULT '0',
  `retweeted_num` int(11) NOT NULL DEFAULT '0',
  `rank` double NOT NULL DEFAULT '0',
  `created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_date` datetime DEFAULT NULL,
  PRIMARY KEY (`sid`),
  KEY `sina_weibo_status_category_fk1` (`category`),
  KEY `sina_weibo_status_rank_index` (`rank`),
  KEY `sina_weibo_status_updated_date_index` (`updated_date`),
  KEY `sina_weibo_status_uid` (`uid`),
  KEY `sina_weibo_status_retweeted_status` (`retweeted_status`),
  CONSTRAINT `sina_weibo_status_ibfk_1` FOREIGN KEY (`category`) REFERENCES `category` (`id`) ON DELETE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8


查询语句:
SQL code

mysql> explain SELECT * FROM `sina_weibo_status` `t` WHERE ((category='11') AND (status='0')) AND (retweeted_status='0') ORDER BY rank desc LIMIT 20 \G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: t
         type: index_merge
possible_keys: sina_weibo_status_category_fk1,sina_weibo_status_retweeted_status
          key: sina_weibo_status_category_fk1,sina_weibo_status_retweeted_status
      key_len: 4,9
          ref: NULL
         rows: 275502
        Extra: Using intersect(sina_weibo_status_category_fk1,sina_weibo_status_retweeted_status); Using where; Using filesort



排序没有使用索引,但是rank定义索引了,这是怎么回事?

作者: zcy_dr   发布时间: 2011-09-13

你的category、status、retweeted_status有多少种值,在
category、status、retweeted_status、rank上建立复合索引试试

作者: wwwwb   发布时间: 2011-09-13

创建索引如下。

create index xxx on `sina_weibo_status`( category, status, retweeted_status, rank);

另外它也利用了索引,并不是没用。 intersect(sina_weibo_status_category_fk1,sina_weibo_status_retweeted_status);

作者: ACMAIN_CHM   发布时间: 2011-09-13