情况
我有一个名为 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]:已删除垃圾邮件链接