Comparing SQL Views and Stored Procedures
Tags: sql, database
Published: 2019-08-05 13:28:04

---
title: Comparing SQL Views and Stored Procedures
published: true
description: 
tags: SQL, Database, Beginner
---

My workplace uses a large number of stored procedures and views in our data management and as a consequence, I have had to learn to use and even build more of them as I develop applications for the company. Although we use them on a daily basis, stored procedures and views were a bit of a mystery to me and I had a few questions:

What does a view do?
What does a stored procedure do?
When should I use a view and when should I use a stored procedure?
Is there a difference in performance?

I was given a brief answer when I asked my manager these questions, but I wasn't satisfied and needed to dig deeper. This post is a result of that digging and hopefully it will provide some insight to other developers of similar experience levels so we can all learn to use best practices in not only our code, but also in how we manage our data.

<h2>What does a view do?</h2>
Much as its name would suggest, a view is simply a "window" where you can observe a specific segment of your data. Oftentimes a developer will create a view when they are constantly running a query and want to automate or make their process more simplified.

<h3>Displaying data from a view</h3>
A view can be thought of as a virtual table and works much the same as any other table in your database when it comes to querying data. Using a SELECT statement, you can query all or query a specific portion of the view using WHERE clauses:

<i>SELECT * FROM vAccounts
WHERE accountId = '523'</i>

You may also join on both views and tables:

<i>SELECT * FROM vAccounts vAcc
INNER JOIN PhoneNumbers pho ON pho.accountId = vAcc.accountId</i>

<h2>What does a stored procedure do?</h2>
A stored procedure is an encapsulation of logic that can be called from an application (or run in SQL Management Studio). They are often used to perform common INSERT, DELETE, and UPDATE statements on data after a parameter has been passed in by an external application. They will often contain various loops, variables, and calls to other stored procedures, tables, and views.

<h3>Displaying data from a stored procedure</h3>
Unlike views, stored procedures require an EXECUTE statement and a passed in parameter value to be tested and run:

<i>EXECUTE spAccount 'James';</i>

The returned result of the above would be a virtual table with some kind of association to the name James (we'd need to see the stored procedure to know for sure).

<h2>When should I use a view and when should I use a stored procedure?</h2>
Most simply, a view is used when only a SELECT statement is needed. Views should be used to store commonly-used JOIN queries and specific columns to build virtual tables of an exact set of data we want to see. Stored procedures hold the more complex logic, such as INSERT, DELETE, and UPDATE statements to automate large SQL workflows.

<h2>Is there a difference in performance?</h2>
In tests done by Grant Fritchey [Scary DBA - Stored Procedures Are Not Faster Than Views](https://www.scarydba.com/2016/11/01/stored-procedures-not-faster-views/), it was determined that, contrary to popular belief, the performance of SQL views and SQL stored procedures is fundamentally identical although they are "fundamentally different objects." Fritchey ran a few thousand executions of a view, a stored procedure w/ view, and a stored procedure, each with 8 logical reads, to retrieve the same data set. His results showed a trivial 10 microsecond (5%) difference which he states could be a disparity on I/O, CPU, or something else.

The only major difference Fritchey could find was in compile time - stored procedures have a much faster compile time than views and this is where the advantage lies for anyone who is seeking a boost in performance.


Sources:
[Microsoft - Lesson 1: Creating Database Objects](https://docs.microsoft.com/en-us/sql/t-sql/lesson-1-creating-database-objects?view=sql-server-2017)
[bytes.com - "Views vs Stored Procedures: What's the difference?"](https://bytes.com/topic/sql-server/answers/450173-views-vs-stored-procedures-whats-difference)
[stackoverflow.com - "What is the difference between a stored procedure and a view?"](https://stackoverflow.com/questions/5194995/what-is-the-difference-between-a-stored-procedure-and-a-view)


---


If you'd like to catch up with me on social media, come find me over on [Twitter](https://twitter.com/RachSoderberg) or [LinkedIn](https://www.linkedin.com/in/rachelsoderberg/) and say hello!