Top 3 Tips You Need to Know to Write Faster SQL Views

2022/6/18 2:20:27

本文主要是介绍Top 3 Tips You Need to Know to Write Faster SQL Views,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Top 3 Tips You Need to Know to Write Faster SQL Views

Friend or foe? SQL Server views have been a subject of heated debates when I was in my first year using SQL Server. They said it was bad because it was slow. But how about today?

Are you on the same boat as I was many years ago? Then, join me on this journey to unravel the real deal about SQL views so that you can write them the fastest possible.

SQL views are virtual tables. The records in a view are the result of a query inside it. Whenever the base tables used in the view get updated, it updates the view too. You can also INSERT, UPDATE, and DELETE records in a view as a table in some cases. Though I haven’t tried this myself.

Similarly to a table, you can CREATE, ALTER, or DROP a view. You can even create an index, with some restrictions.

Note that I used SQL Server 2019 in the sample codes.

 

1. Know the Proper and Improper Use of SQL Views

First, the basics.

What are SQL views for?

It is crucial. If you use it as a hammer to a screwdriver, forget about faster SQL views. First, let’s recall the proper use:

  • To focus, simplify, and customize the perception each user has of the database.
  • To allow the users access to the only information they need to see for security reasons.
  • To provide backward compatibility to an old table or an old schema to not break dependent apps. It is temporary until all the needed changes are complete.
  • To partition data coming from different servers. Therefore, they appear as if they are one table from one server or instance.

How NOT to use SQL Server views?

  • Reuse the view in another view that will be reused in yet, another view. In short, deeply nested views. The code reuse has a few drawbacks in this case.
  • Save on keystrokes. It relates to the first one, which reduces finger pressure and seems to accelerate coding.

Improper use of views, if permitted, will obscure the real reason why you create views. As you will see later, the real benefits outweigh the perceived benefits of improper usage.

 



这篇关于Top 3 Tips You Need to Know to Write Faster SQL Views的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程