Keep table downtime to a minimum by renaming old table, then filling a new version?

前端 未结 4 739
醉话见心
醉话见心 2021-01-19 04:19

I have a handful or so of permanent tables that need to be re-built on a nightly basis.

In order to keep these tables \"live\" for as long as possible, and also to

4条回答
  •  有刺的猬
    2021-01-19 04:33

    Use indirection to avoid manuipulating tables directly:

    • Have 3 tables: Client1, Client2, Client3 with all indexes, constraints and triggers etc
    • Use synonyms to hide the real table eg Client, ClientOld, ClientToLoad
    • To generate the new table, you truncate/write to "ClientToLoad"
    • Then you DROP and CREATE the synonyms in a transaction so that
      • Client -> what was ClientToLoad
      • ClientOld -> what was Client
      • ClientToLoad -> what was ClientOld

    You can use SELECT base_object_name FROM sys.synonyms WHERE name = 'Client' to work out what the current indirection is

    This works on all editions of SQL Server: the other way is "partition switching" which requires enterprise edition

提交回复
热议问题