Microsoft SQL Server/Best Practices - Wikibooks, open books for an open world (2024)

From Wikibooks, open books for an open world

Jump to navigationJump to search

Microsoft SQL Server/Best Practices - Wikibooks, open books for an open world (1)

Wikipedia has related information at Query optimization

  • Always qualify objects by owner.=
  • Use query "with (nolock)" when you don't require high transactional consistency.
  • Do not use GOTO.
  • Avoid CURSOR use because it's significantly slower. If necessary, always declare the correct type of cursor (FAST_FORWARD).
  • Avoid SELECT INTO for populating temp tables. Create the table then use INSERT SELECT.
  • Always use ANSI join syntax.
  • Always check for object existence.
  • Use SCOPE_IDENTITY() instead of @@IDENTITY.
  • Always check @@TRANCOUNT and commit/rollback as necessary.
  • Order DML to avoid deadlocks.
  • Always check @@ERROR and @@ROWCOUNT by assigning to a variable.
  • Always check sp return values.
  • Do not create cross-database dependencies.
  • Avoid table value UDF – performance problems.
  • Avoid dynamic SQL – if necessary use sp_executesql over EXEC.
  • Avoid using NULL values.
  • When there are only two values, ISNULL is more efficient than COALESCE.
  • Always specify columns; try to avoid "SELECT *". Exceptions include these two cases: "WHERE EXISTS (SELECT * ...)" and aggregate functions.
Microsoft SQL Server/Best Practices - Wikibooks, open books for an open world (2024)
Top Articles
Latest Posts
Article information

Author: Wyatt Volkman LLD

Last Updated:

Views: 6169

Rating: 4.6 / 5 (66 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Wyatt Volkman LLD

Birthday: 1992-02-16

Address: Suite 851 78549 Lubowitz Well, Wardside, TX 98080-8615

Phone: +67618977178100

Job: Manufacturing Director

Hobby: Running, Mountaineering, Inline skating, Writing, Baton twirling, Computer programming, Stone skipping

Introduction: My name is Wyatt Volkman LLD, I am a handsome, rich, comfortable, lively, zealous, graceful, gifted person who loves writing and wants to share my knowledge and understanding with you.