mysql - 唯一索引的索引大小为 0,行数为 1.5M

标签 mysql sql indexing size

我一直在寻找一个好的解释,但我还没有找到令我满意的东西。

我有这张 table

表格邮件

CREATE TABLE `emails` (
  `UrlId` int(11) NOT NULL,
  `EmailAddress` varchar(100) NOT NULL,
  UNIQUE KEY `UrlId_UNIQUE` (`UrlId`,`EmailAddress`),
  CONSTRAINT `FK_UrlId_EmailAdress` FOREIGN KEY (`UrlId`) REFERENCES `urls` (`UrlId`) 
  ON DELETE CASCADE ON UPDATE NO ACTION
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

当我运行这个查询时

SELECT count(*) '# of tables',
concat(round(sum(table_rows)/1000000,2),'M') '# of rows',
concat(round(sum(data_length)/(1024*1024*1024),2),'G') 'Size of data',
concat(round(sum(index_length)/(1024*1024*1024),2),'G') 'Size of index',
concat(round(sum(data_length+index_length)/(1024*1024*1024),2),'G') 'Total size',
round(sum(index_length)/sum(data_length),2) 'Index fraction'
FROM information_schema.TABLES WHERE table_name='emails';

我得到了这个结果

# of tables, # of rows, Size of data, Size of index, Total size, Index fraction
1            1.52M      0.17G         0.00G          0.17G       0.00

该表有超过 150 万行,但索引大小为 0。

然后我有这张 table

CREATE TABLE `search_tags` (
  `TagId` int(11) NOT NULL,
  `UrlId` int(11) NOT NULL,
  UNIQUE KEY `Unique_UrlId_SearchTagId` (`UrlId`,`TagId`),
  UNIQUE KEY `Unique_SearchTagId_UrlId` (`TagId`,`UrlId`),
  CONSTRAINT `SearchTagId` FOREIGN KEY (`TagId`) REFERENCES `search_tag` (`SearchTagId`) 
  ON DELETE CASCADE ON UPDATE NO ACTION,
  CONSTRAINT `UrlId` FOREIGN KEY (`UrlId`) REFERENCES `urls` (`UrlId`) 
  ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

当我对该表运行检查查询时,我得到了这个

# of tables, # of rows, Size of data, Size of index, Total size, Index fraction
1            77.06M     3.01G         1.36G          4.37G       0.45

如您所见,我在 search_tags 表上有 2 个唯一索引。这个确实显示了索引大小。

为什么 emails 中的索引大小为 0?实际数据是否也是索引,因为它是唯一的?为什么 search_tags 有索引大小?是因为我有双索引并且在唯一数据的一侧创建了一个索引吗?还是我离题太远了?

编辑

我决定创建一个只有 1 个唯一索引的 search_tags 测试表。并且结果符合预期。

# table_name       # of rows  Size of data  Size of index  Total size  Index fraction
search_tags        74.96M     3.01G         1.36G          4.37G       0.45
search_tags_test   67.92M     1.81G         0.00G          1.81G       0.00

我想可以肯定地说,覆盖整个表列的唯一索引,整个数据成为索引,因此索引大小为 0。

然而,我过去曾说过,“可以肯定地说”......并且 100% 肯定我的假设,后来发现我不正确。我只是想得到其他人的证实。

最佳答案

似乎是the InnoDB engine的描述的一部分:

InnoDB tables arrange your data on disk to optimize common queries based on primary keys. Each InnoDB table has a primary key index called the clustered index that organizes the data to minimize I/O for primary key lookups.

关于mysql - 唯一索引的索引大小为 0,行数为 1.5M,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19746457/

相关文章:

mysql - 我的查询每次返回相同的(date2)

sql - 优化我的 SQL 查询——选择正确的索引

php - 如何使用 PHP 从 MySQL 查询中按升序排列值?

php - Yii CDbCriteria 选择带有 AS 别名

Python - 如何根据 DF 和其中的数据制作 SQL "Create Table"语句?

mysql - 如何使用级联?我的不工作

sql-server - 在 T-SQL 中创建新索引时需要重新索引吗?

python - 创建表后向 SQLAlchemy 模型添加索引

MySQL 问题 - 我想删除任何括号内的所有文本

远程服务器上的 mysqldump