mysql - MySQL中索引多的缺点

标签 mysql database indexing

<分区>

情况

我有一个名为 follow 的表,这是表结构:

- follower_id
- following_id
- when // timestamp

这些是我执行的 SQL 查询:

SELECT * FROM follow WHERE follower_id = ? ORDER BY when LIMIT 0,20
SELECT * FROM follow WHERE following_id = ? ORDER BY when LIMIT 0,20
SELECT * FROM follow WHERE follower_id = ?
SELECT * FROM follow WHERE following_id = ?

这些是我的索引:

follower_id, when
following_id, when
follower_id
following_id

问题

这种方法是否正确?在数据库中拥有这个数量(或更多)的索引有什么缺点吗?

最佳答案

And is there any disadvantage of having this amount (or more than this) of indexes in DB ?

我认为这些索引数量不会影响您的性能。

但是您可能会注意到,使用 SELECT 而不是 INSERT 时索引既好又快。

索引的缺点来自[这里][1] 说:

When an index is created on the column(s), MySQL also creates a separate file that is sorted, and contains only the field(s) you're interested in sorting on.

Firstly, the indexes take up disk space. Usually the space usage isn’t significant, but because of creating index on every column in every possible combination, the index file would grow much more quickly than the data file. In the case when a table is of large table size, the index file could reach the operating system’s maximum file size.

Secondly, the indexes slow down the speed of writing queries, such as INSERT, UPDATE and DELETE. Because MySQL has to internally maintain the “pointers” to the inserted rows in the actual data file, so there is a performance price to pay in case of above said writing queries because every time a record is changed, the indexes must be updated. However, you may be able to write your queries in such a way that do not cause the very noticeable performance degradation.

[1]:已删除垃圾邮件链接

关于mysql - MySQL中索引多的缺点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20021834/

相关文章:

mysql - MS Access 主键自动编号变得困惑

c - Oracle 11g 中 rowid 的长度是固定的吗?

mysql - 发布系统的表结构

python-3.x - 获取每个 id 的所有列,其中该列等于某个值

python - 将 MATLAB 转换为 Python : too many indices return error

sql - 相同的数据,两种不同的存储方式

mysql - mysql快速查询问题

database - 使用 TypeORM Cordova 数据库将图像保存为 Blob

sql - Postgres 索引统计

php - 将多张照片插入mysql数据库