ringa_lee2017-04-11 09:57:404楼
数据库没这么笨的。
例如这个表:
CREATE TABLE `user_customer` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID号',
`uid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '人员ID号',
`cid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '客户ID号',
PRIMARY KEY (`id`),
KEY `uid_cid` (`uid`,`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=18227 DEFAULT CHARSET=utf8
假设共有1千万条数据,uid > 1500的有40万条,cid > 1000000的有100万条,那么下面两个SQL那个快?
select count(1) from user_customer where uid > 1500 and cid > 1000000;
select count(1) from user_customer where cid > 1000000 and uid > 1500;
是一样快的,因为数据库知道有uid_cid这个索引,会调整成先筛uid再筛cid的。
当然,建不同的索引会有不同的效率,但是索引定了,查询条件的顺序是没影响的,例如组合索引的顺序改成cid,uid,那样查询效率是不同的。