Sequence vs identity

后端 未结 6 900
南方客
南方客 2020-11-29 20:20

SQL Server 2012 introduced Sequence as a new feature, same as in Oracle and Postgres. Where sequences are preferred over identities? And why do we need sequence

6条回答
  •  眼角桃花
    2020-11-29 20:33

    Although sequences provide more flexibility than identity columns, I didn't find they had any performance benefits.

    I found performance using identity was consistently 3x faster than using sequence for batch inserts.

    I inserted approx 1.5M rows and performance was:

    • 14 seconds for identity
    • 45 seconds for sequence

    I inserted the rows into a table which used sequence object via a table default:

    NEXT VALUE for for

    and also tried specifying sequence value in select statement:

    SELECT NEXT VALUE for ,  from 

    Both were the same factor slower than the identity method. I used the default cache option for the sequence.

    The article referenced in Arion's first link shows performance for row-by-row insert and difference between identity and sequence was 16.6 seconds to 14.3 seconds for 10,000 inserts.

    The Caching option has a big impact on performance, but identity is faster for higher volumes (+1M rows)

    See this link for an indepth analysis as per utly4life's comment.

    提交回复
    热议问题