简体   繁体   中英

Increase numeric range of a table column in SQL Server without using ALTER TABLE

I have a table named Range which contains a column min_value with a data type of int . I need to save long value to that column. ALTER TABLE allocates more space for existing data. That's inefficient. Basically what I need is more space for entries to be added in future. Not for existing ones.

You're likely best off just using ALTER TABLE and changing the column datatype to BIGINT .

If you are worried about values taking 8 bytes that could happily consume much smaller you can enable row compression if on Enterprise Edition as this is one issue this solves.

An alternative would be to add a new nullable BIGINT column min_value2 . You could have a check constraint that ensures only one column has a value and a non persisted computed column using ISNULL(min_value2,min_value) . Initially this would be quite cheap as it is metadata only but over time it will be more expensive as new rows and updated rows would end up taking space for both columns.

Another alternative would be to create a new table with BIGINT and simply move on to using that for rows that need it. You could have a view that union alls them together. But this would add complexity and likely reduce query execution plan efficiency.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM