mysql 奇怪索引问题


   
  CREATE TABLE IF NOT EXISTS `market_order` (
  
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`marketGoods` text NOT NULL COMMENT '出售所有商品ID/商品名称',
`totalOriginalPrice` decimal(16,3) unsigned NOT NULL DEFAULT '0.000' COMMENT '订单(全部商品).原价总价格',
`totalDiscountPrice` decimal(16,3) unsigned NOT NULL DEFAULT '0.000' COMMENT '订单(全部商品).折后总价格',
`totalCollectPrice` smallint(6) unsigned NOT NULL DEFAULT '0' COMMENT '订单(全部商品).收取价格',
`clientId` smallint(6) unsigned NOT NULL DEFAULT '0' COMMENT '客户ID',
`adminId` smallint(6) unsigned NOT NULL DEFAULT '0' COMMENT '操作人员ID',
`remark` text NOT NULL COMMENT '备注',
`status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '订单出售状态(1:正常售出 0:所有商品全部退还)',
`ifUpdate` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否修改过销售单中的商品(0:没有 1:有)',
`time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '销售时间',
PRIMARY KEY (`id`),
KEY `index_time` (`time`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='销售单' AUTO_INCREMENT=9 ;

CREATE TABLE IF NOT EXISTS `admin_info` (
`id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL DEFAULT '' COMMENT '用户名',
`password` varchar(40) NOT NULL DEFAULT '' COMMENT '密码',
`groupId` smallint(6) NOT NULL DEFAULT '0' COMMENT '所属管理组',
`discounts` varchar(50) NOT NULL DEFAULT '' COMMENT '折扣使用权限',
`status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '状态(0:关闭 1:正常)',
PRIMARY KEY (`id`),
KEY `name` (`name`),
KEY `groupid` (`groupId`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='管理员信息' AUTO_INCREMENT=3 ;

CREATE TABLE IF NOT EXISTS `client_info` (
`id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
`classId` smallint(6) unsigned NOT NULL DEFAULT '0' COMMENT '客户分类id',
`company` varchar(50) NOT NULL DEFAULT '' COMMENT '公司单位',
`linkman` varchar(20) NOT NULL DEFAULT '' COMMENT '联系人',
`tel` varchar(50) NOT NULL DEFAULT '' COMMENT '联系方式',
`remark` varchar(255) NOT NULL DEFAULT '' COMMENT '备注',
`status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '状态(0:关闭 1:正常)',
`time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最后修改时间',
`sort` smallint(6) unsigned NOT NULL DEFAULT '0' COMMENT '排序',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='客户' AUTO_INCREMENT=5 ;

EXPLAIN SELECT mo . * , ci.company, ci.linkman, ci.tel, ai.name AS adminName
FROM market_order AS mo
LEFT JOIN client_info AS ci ON mo.clientId = ci.id
LEFT JOIN admin_info AS ai ON mo.adminId = ai.id
WHERE mo.time >=1356969600
AND mo.time <=1364745599
ORDER BY mo.id DESC
LIMIT 0 , 30

为什么这条sql语句不能用到 index_time 这个索引?

请输入图片描述

mysql 索引

一裤一裤... 11 years, 5 months ago

SQL最后有ORDER BY MO.ID,所以得建立联合索引才行.(time,id) BTree

哎哟-重口哦 answered 11 years, 5 months ago

Your Answer