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

后端 未结 5 1502
遥遥无期
遥遥无期 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:15

    This doesn't matter on this problem because:

    1. If you add data first to the table and after it you add index. Your index generating time will be O(n*log(N)) longer (where n is a rows added). Because tree gerating time is O(N*log(N)) then if you split this into old data and new data you get O((X+n)*log(N)) this can be simply converted to O(X*log(N) + n*log(N)) and in this format you can simply see what you will wait additional.
    2. If you add index and after it put data. Every row (you have n new rows) you get longer insert additional time O(log(N)) needed to regenerate structure of the tree after adding new element into it (index column from new row, because index already exists and new row was added then index must be regenerated to balanced structure, this cost O(log(P)) where P is a index power [elements in index]). You have n new rows then finally you have n * O(log(N)) then O(n*log(N)) summary additional time.

提交回复
热议问题