我有一个类似-的示例表
CREATE TABLE `cdr` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`dataPacketDownLink` bigint(20) DEFAULT NULL,
`dataPacketUpLink` bigint(20) DEFAULT NULL,
`dataPlanEndTime` datetime DEFAULT NULL,
`dataPlanStartTime` datetime DEFAULT NULL,
`dataVolumeDownLink` bigint(20) DEFAULT NULL,
`dataVolumeUpLink` bigint(20) DEFAULT NULL,
`dataplan` varchar(255) DEFAULT NULL,
`dataplanType` varchar(255) DEFAULT NULL,
`createdOn` datetime DEFAULT NULL,
`deviceName` varchar(500) DEFAULT NULL,
`duration` int(11) NOT NULL,
`effectiveDuration` int(11) NOT NULL,
`hour` tinyint(4) DEFAULT NULL,
`eventDate` datetime DEFAULT NULL,
`msisdn` bigint(20) DEFAULT NULL,
`quarter` tinyint(4) DEFAULT NULL,
`validDays` int(11) DEFAULT NULL,
`dataLeft` bigint(20) DEFAULT NULL,
`completedOn` datetime DEFAULT NULL,
`evedate` date NOT NULL DEFAULT '0000-00-00',
PRIMARY KEY (`id`,`evedate`),
KEY `evedate_index` (`evedate`),
KEY `eve_hour_index` (`evedate`,`hour`),
KEY `eve_msisdn_index` (`evedate`,`msisdn`)
) ENGINE=MyISAM AUTO_INCREMENT=259341694 DEFAULT CHARSET=latin1
/*!50500 PARTITION BY RANGE COLUMNS(evedate)
(PARTITION `START` VALUES LESS THAN ('2013-09-01') ENGINE = MyISAM,
PARTITION p01 VALUES LESS THAN ('2013-09-08') ENGINE = MyISAM,
PARTITION p02 VALUES LESS THAN ('2013-09-15') ENGINE = MyISAM,
PARTITION p03 VALUES LESS THAN ('2013-09-22') ENGINE = MyISAM,
PARTITION p04 VALUES LESS THAN ('2013-09-29') ENGINE = MyISAM,
PARTITION p05 VALUES LESS THAN ('2013-10-06') ENGINE = MyISAM,
PARTITION p06 VALUES LESS THAN ('2013-10-12') ENGINE = MyISAM,
PARTITION p07 VALUES LESS THAN ('2013-10-19') ENGINE = MyISAM,
PARTITION p08 VALUES LESS THAN ('2013-10-25') ENGINE = MyISAM,
PARTITION p09 VALUES LESS THAN ('2013-10-31') ENGINE = MyISAM,
PARTITION p10 VALUES LESS THAN (MAXVALUE) ENGINE = MyISAM) */
现在看一下索引eve_hour_index(eveldate,hour)和eve_msidn_index(evelate,msisdn)的顺序。但我最后宣布了日期。
我在某个地方读到,综合指数的顺序很重要。那么是哪个顺序呢?查询中的顺序还是表中的顺序?
我是否必须将此复合索引重写为eve_hour_index(hour,evodate)和eve_msidn_index(msisdn,evidate)?
或者第一个是正确的?
谢谢。
简而言之,列的索引对象包含该列的值,这些值经过排序后可以快速找到给定的值。对于复合索引,它是类似的,但复合索引中列的值是组合和排序的,因此任何涉及复合索引中所有列的查询都要快得多。
需要注意的是,如果在列A和B上有一个复合索引(按该顺序指定),它将大大提高在A和B都有条件的查询的速度,有助于加快只在A上有条件的搜索,可能不会有助于加速只在B上有条件