Using more than one index per table is dangerous?

后端 未结 9 1985
春和景丽
春和景丽 2021-02-02 14:25

In a former company I worked at, the rule of thumb was that a table should have no more than one index (allowing the odd exception, and certain parent-tables holding references

相关标签:
9条回答
  • 2021-02-02 15:05

    So much depends on your schema and the queries that you normally run. For example: if you normally need to select above 60% of the rows of your table, indexes won't help you and it will be cheaper to table scan than to index scan and then lookup rows. Focused queries that select a small number of rows in different parts of the table or which are used for joins in queries will probably benefit from indexes. The right index in the right place can make or break a feature.

    Indexes take space so making too many indexes on a table can be counter productive for the same reasons listed above. Scanning 5 indexes and then performing row lookups may be much more expensive than simply table scanning.

    Good design is the synthesis about about knowing when to normalise and when not to. If you frequently join on a particular column, check the IO plan with the index and without. As a general rule I avoid tables with more than 20 columns. This is often a sign that the data should be normalised. More than about 5 indexes on a table and you may be using more space for the indexes than the main table, be sure that is worth it. These rules are only the lightest of guidance and so much depends on how the data will be used in queries and what your data update profile looks like.

    Experiment with your query plans to see how your solution improves or degrades with an index.

    0 讨论(0)
  • 2021-02-02 15:09

    Updating an index is once per insert (per index). Speed gain is for every select. So if you update infrequently and read often, then the extra work may be well worth it.

    If you do different selects (meaning the columns you filter on are different), then maintaining an index for each type of query is very useful. Provided you have a limited set of columns that you query often.

    But the usual advice holds: if you want to know which is fastest: profile!

    0 讨论(0)
  • 2021-02-02 15:12

    You should of course be careful not to create too many indexes per table, but only ever using a single index per table is not a useful level.

    How many indexes to use depends on how the table is used. A table that is updated often would generally have less indexes than one that is read much more often than it's updated.

    We have some tables that are updated regularly by a job every two minutes, but they are read often by queries that vary a lot, so they have several indexes. One table for example have 24 indexes.

    0 讨论(0)
提交回复
热议问题