Downsides to “WITH SCHEMABINDING” in SQL Server?

后端 未结 10 500
天命终不由人
天命终不由人 2021-01-31 01:20

I have a database with hundreds of awkwardly named tables in it (CG001T, GH066L, etc), and I have views on every one with its \"friendly\" name (the view \"CUSTOMERS\" is \"SELE

相关标签:
10条回答
  • 2021-01-31 01:56

    When using tSQLt Unit Test Framework you will come across issues and will need workarounds when using FakeTable method, which won't allow you to fake a table that is linked to a view with schemabinding.

    0 讨论(0)
  • 2021-01-31 01:57

    The negatives mentioned hardly outweigh this best practice since SQL Svr 2005. It avoids the dreaded table spooling. A major negative for me is that schema bound sprocs, funcs, views, can't include "foreign" databases such as the master db, so you can throw all the great realtime system stuff in the trash unless, for example, your production core database sits inside master. For me, I can't deal with life without the sys stuff. Of course not all processing requires spool-free performance and fast and slow results can be combined simultaneously in higher data class layers.

    0 讨论(0)
  • 2021-01-31 02:03

    You wont be able to alter/drop the table, unless you drop the view first.

    0 讨论(0)
  • 2021-01-31 02:04

    Oh, there are DEFINITELY DOWNSIDES to using SCHEMABINDING - these come from fact the SCHEMABINDING, especially when coupled with COMPUTED columns "LOCKS" THE RELATIONSHIPS and makes some "trivial changes" darn near impossible.

    1. Create a table.
    2. Create a SCHEMABOUND UDF.
    3. Create a COMPUTED PERSISTED column that references the UDF.
    4. Add an INDEX over said column.
    5. Try to update the UDF.

    Good luck with that one!

    1. The UDF can't be dropped or altered because it is SCHEMABOUND.
    2. The COLUMN can't be dropped because it is used in an INDEX.
    3. The COLUMN can't be altered because it is COMPUTED.

    Well, frak. Really..!?! My day just became a PITA. (Now, tools like ApexSQL Diff can handle this when provided with a modified schema, but the issue is here that I can't even modify the schema to begin with!)

    I'm not against SCHEMABINDING, mind (and it's needed for a UDF in this case), but I'm against there not being a way (that I can find) to "temporarily disable" the SCHEMABINDING.

    0 讨论(0)
  • 2021-01-31 02:04

    If these tables are from a third-party app (they're notorious for trying hide their tables), you cause and upgrade to fail if it attempts to alter any of these tables.

    You just have to alter the views without the schemabinding before the update/upgrade and then put them back. Like others have mentioned. Just takes some planning, discipline, etc.

    0 讨论(0)
  • 2021-01-31 02:06

    If your tool (ssms etc.) does not handle schema change failures on the base object well / elegantly you could cause yourself some real chaos. That's what I'm sitting with now, and I do realise that this is a fringe case

    0 讨论(0)
提交回复
热议问题