简体   繁体   English

应该按哪个顺序(表列或查询)创建复合索引?

[英]In which order(table column or query) composite index should made?

I have a sample table like - 我有一个示例表,例如-

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) */

Now look at the order of index eve_hour_index(evedate,hour) and eve_msisdn_index(evedate,msisdn). 现在查看索引eve_hour_index(evedate,hour)和eve_msisdn_index(evedate,msisdn)的顺序。 But I declared evedate in the last. 但是我最后宣布了依法特。

I have read somewhere that order in composite index is important. 我读过某个地方,复合索引的顺序很重要。 So which order? 那么哪个命令? order in query or order in table? 查询顺序还是表顺序?

Do I have to re-write this compound index as eve_hour_index(hour,evedate) and eve_msisdn_index(msisdn,evedate)? 我是否必须将此复合索引重新编写为eve_hour_index(hour,evedate)和eve_msisdn_index(msisdn,evedate)?

Or the first one is correct? 还是第一个是正确的?

Thank you. 谢谢。

In a nutshell, an index object for a column contains the values of that column sorted so that a given value can quickly be found. 简而言之,列的索引对象包含已排序的该列的值,以便可以快速找到给定值。 For a composite index, it is similar, but the values for the columns in the composite index are combined and sorted so any queries involving all of the columns in the composite index are much faster. 对于组合索引,它是相似的,但是组合索引中列的值被合并并排序,因此涉及组合索引中所有列的任何查询都快得多。

A note is that if you have a composite index on columns A and B (specified in that order), it will greatly improve speed on queries that have conditions on both A and B, could help speed on queries that just have conditions on A, and probably won't help speed on queries that just have conditions on B. 请注意,如果您在A和B列上有一个复合索引(按此顺序指定),它将大大提高同时具有A和B条件的查询的速度,可以帮助加快仅具有A条件的查询的速度,可能对仅满足条件B的查询没有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM