• Hi Tao, did you post the correct version of your code?

    I copied your code above and did a find/replace on #SequenceTable and SeqNumber swapping these for my table name and sequence field respectively, then ran it.  It was certainly quick, very impressive, 16 seconds to produce 142874 identified breaks in the sequence from a table containing 3,827,625 rows. 

    First few rows of output are as below

    StartGapEndGapFirstAvailableLastAvailableAvailableCount
    0NULL1NULLNULL
    0NULL1NULLNULL
    0NULL1NULLNULL
    0NULL1NULLNULL
    0NULL1NULLNULL
    0NULL1NULLNULL
    582246210758225621063882
    780258008678026800852060

    My original query over the same table takes 44 seconds and produces the following first few rows.

    StartGapEndGapFirstAvailableLastAvailableAvailableCount
    02111
    206242062620625206251
    520915209352092520921
    555185552055519555191
    582225822458223582231
    6210778025621087802415917
    800868008880087800871
    801608016280161801611

    I've checked the results and my query is showing the correct gaps.  Your result line showing 58224 to 62107 represents my end gap result line 5 above to my start gap result line 6 above.

    Love to see your revision as certainly looks like it will be very fast.

    Stephen