DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Data Engineering
  3. Databases
  4. Stored Procedures Are Not Faster Than Views

Stored Procedures Are Not Faster Than Views

In this post we take a look at the supposition that stored procedures perform better than views in databases. Read on for the details.

Grant Fritchey user avatar by
Grant Fritchey
·
Nov. 02, 16 · Tutorial
Like (6)
Save
Tweet
Share
16.26K Views

Join the DZone community and get the full member experience.

Join For Free

a performance tuning tip i saw recently said, “views don’t perform as well as stored procedures.”

<sigh>

let’s break this down, just a little.

definitions

a view is nothing but a query. the definition given by microsoft is that it’s a virtual table that’s defined by a query. it’s a query that is used to mask data or perform a complex join or similar behaviors. views are queries that get stored in the database. views can be easily referred to as if they were a tables. that’s it. i’ve written in the past about views , including how they can possibly perform poorly .

a stored procedure is also a query, or a series of queries, or, a whole lot more. microsoft’s definition of a stored procedure basically defines it as programming object that can accept input through parameters, perform actions, and provide various types of output. stored procedures are also stored in the database, but that’s about the end of the direct correlations to a view. heck, you can call views from stored procedures, so i’m really at a loss as to where this tip comes from.

in short, yes, both these objects have in them queries, but these are fundamentally different objects. you can’t really say that using one or the other is faster because they each do different things. further, you can write code that will perform poorly using either construct.

test setup to compare performance

here’s the view definition i’m going to use for the tests:

create view dbo.customerdeliveryinfo
as
select c.customername,
   c.deliveryrun,
   c.runposition,
   dm.deliverymethodname,
   cid.cityname as deliverycity,
   cip.cityname as postalcity,
   c.customerid
from sales.customers as c
join application.deliverymethods as dm
   on dm.deliverymethodid = c.deliverymethodid
join application.cities as cid
   on cid.cityid = c.deliverycityid
join application.cities as cip
   on cip.cityid = c.postalcityid;

we’re going to compare that with a stored procedure that uses the same query. the procedure also takes advantage of the fact that it is a stored procedure using a parameter for input values:

create procedure dbo.customerdeliveryinformation 
(@customerid int)
as
begin
   select c.customername,
      c.deliveryrun,
      c.runposition,
      dm.deliverymethodname,
      cid.cityname as deliverycity,
      cip.cityname as postalcity,
      c.customerid
   from sales.customers as c
   join application.deliverymethods as dm
      on dm.deliverymethodid = c.deliverymethodid
   join application.cities as cid
      on cid.cityid = c.deliverycityid
   join application.cities as cip
      on cip.cityid = c.postalcityid
   where c.customerid = @customerid;
end;

we’re also going to create another stored procedure that uses the view:

create procedure dbo.infocustomerdelivery 
(@customerid int)
as
begin
   select * from dbo.customerdeliveryinfo as cdi
   where cdi.customerid = @customerid;
end;

because stored procedures and views are different, we’ll have to call these different objects in different ways:

select *
from dbo.customerdeliveryinfo as cdi
where cdi.customerid = 556;

exec dbo.customerdeliveryinformation @customerid = 556;

exec dbo.infocustomerdelivery @customerid = 556;

in this way, we can run each of these queries independently and compare the results between them.

results comparing stored procedures with views

if you run each of the queries above, you will find that they all create a nearly identical execution plan:

views exec plan

you can click on that to make it bigger. if we compare all the different plans, one set of details does stand out:

plandifferences

there is a difference in the compile time between the view by itself and the stored procedures (they were almost identical). let’s look at performance over a few thousand executions:

query duration
view avg: 210.431431431431
stored proc w/ view avg: 190.641641641642
stored proc avg: 200.171171171171

this is measured in microsends, so the variation we’re seeing is likely just some disparity on i/o, cpu or something else since the differences are trivial at 10mc or 5%. while that may seem like the view is suffering, please note that the view inside the procedure actually ran faster by 5%. again, this is explained by the fact that we’re only talking about a 10-microsecond difference. i’m not sure if that’s within the margin for error on the extended event sql_batch_complete or not (i couldn’t find documentation stating what it might be), but i’ll bet it’s close. i believe it’s safe to say that the average performance of these queries is identical.

all three queries had eight logical reads.

what about execution time including compile time, since there is a difference:

query duration
view avg: 10089.3226452906
stored proc avg: 9314.38877755511
stored proc w/ view avg: 9938.05410821643

the difference in the performance including compile time for the procedure alone is 700mc better on average than the view. that’s an 8% difference. it was almost that high for the view that used the procedure at 7%.

if we’re just talking compile time then, there is a significant win if we avoid the view. this is no doubt because of the extra work involved in unpacking the view and going through the simplification process within the optimizer. plus, the view alone in our query was parameterized by the optimizer in order to assist it’s performance over time (as we saw in the average results without the recompile). all that extra work explains the 8% difference.

let’s break it

what if we change the query around a little. i decide that all i want to see right now from the view is the customerid:

select cdi.customerid
from dbo.customerdeliveryinfo as cdi
where cdi.customerid = 556;

when i execute this, i get a whole new execution plan:

viewsimple

the execution time drops a little to around 190mc on average and the reads go from 8 to 2. the stored procedure would have to get rewritten to only return customerid. does that mean that views are faster than stored procs? absolutely not. it just means that there is some degree of flexibility built into the view, as a construct, that’s not there in a stored procedure, as a construct. these are fundamentally different objects.

what if we change the query against the view again:

select *
from dbo.customerdeliveryinfo as cdi
where cdi.customername = 'om yadav';

once more the execution plan will change to something different than before:

viewcomplex

performance drops to about 300mc and we get 10 reads instead of 8. does that mean that views are slower than stored procedures? no. we’re attempting to compare two different objects that perform two different functions within sql server.

conclusion

since a stored procedure can actually query a view, suggesting that we use stored procedures instead of views becomes quite problematic. with the exception of the differences in compile time, we see that views actually perform exactly the same as stored procedures, if the query in question is the same. there are reasons to use views as well as reasons to not use them. there are reasons to use stored procedures as well as reasons to not use them. neither of these objects is preferred above the other because of performance concerns.

Database

Published at DZone with permission of Grant Fritchey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Power of Zero-Knowledge Proofs: Exploring the New ConsenSys zkEVM
  • Key Elements of Site Reliability Engineering (SRE)
  • Top 10 Best Practices for Web Application Testing
  • DevOps vs Agile: Which Approach Will Win the Battle for Efficiency?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: