Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql为小值集提供多个索引_Mysql_Indexing - Fatal编程技术网

mysql为小值集提供多个索引

mysql为小值集提供多个索引,mysql,indexing,Mysql,Indexing,例如,我有一个记录数据的表 TABLE IF NOT EXISTS `message_log` ( `id` int(8) NOT NULL AUTO_INCREMENT, `user_id` int(8) unsigned NOT NULL, `channel` tinyint(8) unsigned NOT NULL DEFAULT '0', `type` tinyint(4) NOT NULL DEFAULT '0', `message` varchar(255) NOT NULL, `t

例如,我有一个记录数据的表

TABLE IF NOT EXISTS `message_log` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`user_id` int(8) unsigned NOT NULL,
`channel` tinyint(8) unsigned NOT NULL DEFAULT '0',
`type` tinyint(4) NOT NULL DEFAULT '0',
`message` varchar(255) NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`confirmed` datetime DEFAULT NULL,
PRIMARY KEY (`id`,`user_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1
/*!50100 PARTITION BY HASH (user_id)
PARTITIONS 11 */ AUTO_INCREMENT=4569
(注意——我知道应该是InnoDB,但这不是问题所在)

关于搜索,可以在用户id、用户id和频道或用户id和频道以及类型上进行搜索。 用户id可以是任何数字。 频道是1-20。 类型为1-3


现在,用户id似乎是一个明显的索引候选对象,但我是否需要索引频道和类型,因为它们是相对较小的一组可能性?

在这种情况下,当您基于
用户id
频道和
类型进行搜索时,您可以使用覆盖索引(复合)这将是有效的

如果表具有多列索引,则 优化器可以使用索引查找行

因此,您可以将索引添加为

alter table message_log add index search_idx(user_id,channel,type)

你主要是读还是写这张表?您可能可以在用户id、频道上添加组合索引。可能80%插入10%更新10%选择。可能您甚至不需要其他索引(除非某些选择很慢),因为添加索引会降低插入和更新的速度。