Is it better to create an index before filling a table with data, or after the data is in place?

后端 未结 5 1518
遥遥无期
遥遥无期 2020-12-07 19:37

I have a table of about 100M rows that I am going to copy to alter, adding an index. I\'m not so concerned with the time it takes to create the new table, but will the crea

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-07 20:16

    It is probably better to create the index after the rows are added. Not only will it be faster, but the tree balancing will probably be better.

    Edit "balancing" probably is not the best choice of terms here. In the case of a b-tree, it is balanced by definition. But that does not mean that the b-tree has the optimal layout. Child node distribution within parents can be uneven (leading to more cost in future updates) and the tree depth can end up being deeper than necessary if the balancing is not performed carefully during updates. If the index is created after the rows are added, it is will more likely have a better distribution. In addition, index pages on disk may have less fragmentation after the index is built. A bit more information here

提交回复
热议问题