Advantages of stored procedures Part 21 — Transcript
Full transcript
- 0:00Welcome to PRIM Technologies. I am
- 0:01Wenut. This is part 21 of SQL Server. In
- 0:05this session, we'll understand the
- 0:07advantages of using store procedures
- 0:09over ad hoc or inline SQL. Now, before
- 0:12continuing with this session, I strongly
- 0:14recommend to watch part 18 of this video
- 0:16series where we have covered the basics
- 0:18of STO
- 0:20procedures. The greatest benefit of
- 0:22using stow procedures is that they are
- 0:24capable of reusing an execution plan.
- 0:28Okay, let's understand what we mean by
- 0:30an execution plan. Okay, first of all,
- 0:33when we issue a query to a SQL server,
- 0:36three things happen. First, it checks
- 0:38the syntax of that query. Second, it
- 0:41compiles that query. Third, it generates
- 0:44an execution plan. Okay, what do we mean
- 0:46by an execution plan? An execution plan
- 0:49is nothing but for the query to retrieve
- 0:51the data, what is the best possible way
- 0:54available? Okay, now this depends on the
- 0:58indexes that are available in SQL server
- 1:00to help that query. So based on all
- 1:02these factors, it's going to generate an
- 1:04execution plan and then it executes the
- 1:08query. So next time when you issue the
- 1:10query, since that execution plan is
- 1:12already generated, it will be cached by
- 1:14SQL server. So next time when you run
- 1:17the same query, what's going to happen?
- 1:19It's going to reuse that execution plan.
- 1:22Okay. So basically if it is stored
- 1:24procedures you know stored procedures
- 1:26have been doing this from a long time.
- 1:29Okay. They are capable of retaining the
- 1:32execution plan caching that execution
- 1:34plan and reusing that. Okay. Now even
- 1:37with ad hoc queries these days you know
- 1:40they are able to reuse the execution
- 1:42plan. Okay. With the new versions of SQL
- 1:45server that we have. But the problem is
- 1:47even a slight change in the query if
- 1:51it's an ad hoc query. So even a slight
- 1:53change in the query can prevent the
- 1:55execution plan from being reused. Let's
- 1:58understand what we mean by that. I have
- 2:01this table called tbl employee which is
- 2:03very simple. It has got three columns
- 2:04name ID name and gender. And if you look
- 2:07at the query here it select name from
- 2:11tbl employee where ID is equal to one.
- 2:13Now when I execute this query what's
- 2:15going to happen first time when we issue
- 2:17this query the syntax is checked it is
- 2:19compiled and execution plan is generated
- 2:22and then it executes that query returns
- 2:24the data back which is very good now
- 2:27when I execute this again the query is
- 2:30the same thing even ad hoc queries these
- 2:32days they can you know reuse the
- 2:35execution plan so it reuses that
- 2:37execution plan and it will be quick but
- 2:40on the other hand if I change the
- 2:41parameter okay I'm saying in the initial
- 2:44query id is equal to 1. Now if I pass ID
- 2:47is equal to two, it is not going to
- 2:50reuse the execution plan. It's going to
- 2:52generate a new plan for this. Okay. But
- 2:55whereas when it comes to store
- 2:57procedures, look at this. I have the
- 2:58store procedure. Now to the store
- 3:01procedure, uh I mean what the store
- 3:03procedure bas basically does is it
- 3:05retrieves the name of an employee, you
- 3:07know, for a given ID. So this procedure
- 3:10has a parameter called at id. So when
- 3:12you pass that parameter, it gives the
- 3:15name of that employee. Okay. Now if
- 3:17you're new to stored procedures, I
- 3:18strongly recommend to watch part 18 of
- 3:20this video series where we have covered
- 3:22the basics of stored procedures. Okay.
- 3:25So now when I execute this one, look at
- 3:28this. The parameter that I'm passing is
- 3:29one. So basically the same thing
- 3:31happens. Okay. Um the syntax you know
- 3:35basically the the procedure is compiled
- 3:37and then execution plan is generated etc
- 3:40etc. And then when I execute this again
- 3:43the execution plan is reused. Okay. Now
- 3:46even if I execute this even if I change
- 3:48the parameter to two in case of store
- 3:51procedures the same plan will be reused.
- 3:54But that's not the case with ad hoc SQL
- 3:57queries. Okay. So in your interview just
- 4:00in case if you're attending an interview
- 4:02and if you if somebody asks you you know
- 4:04will stored procedures can't adop
- 4:07queries reuse execution plan they can
- 4:11but even a slightest change in the query
- 4:13maybe change in parameter values if it
- 4:15is not a parameterized query or even a
- 4:18space in the query you know if if for
- 4:21example if you look at this query you
- 4:24know initially when I execute this you
- 4:26know the plan gets created
- 4:28I get I execute again the plan will be
- 4:31reused. But even if I have a simple
- 4:33space here like this and if I execute
- 4:35the same query again
- 4:37now it doesn't reuse the execution plan.
- 4:40It's going to generate a new execution
- 4:41plan. So a simple chain you know space
- 4:45or change in parameter values you know
- 4:48can result in a new execution plan being
- 4:52created. Okay. But that's not the case
- 4:54with stored procedures. stored
- 4:56procedures can reuse the execution plan.
- 5:01All right. Another benefit from a
- 5:04performance standpoint that stored
- 5:05procedures has is they can reduce
- 5:07network traffic. Now how can they reduce
- 5:10network traffic? Let's understand that a
- 5:12stored procedure usually is something
- 5:14like this. We create the procedure.
- 5:16There is the body for the stored
- 5:17procedure. And within the body of the
- 5:19stored procedure in reality, stored
- 5:21procedures are or you know they they
- 5:24have got enough number of lines maybe
- 5:26there are I have seen stored procedures
- 5:27even with 3,000 lines of code within
- 5:30them lots and lots of logic happening
- 5:32within them. Okay. Now when we have such
- 5:35kind of a stored procedure and we want
- 5:37to execute that store procedure all you
- 5:40have to do is from the client
- 5:41application it could be a net
- 5:42application java application or simply a
- 5:45SQL server management studio. So from
- 5:47the SQL server management studio you
- 5:49just need to specify the name of the
- 5:52procedure I mean this execute keyword
- 5:54the name of the procedure and the
- 5:56parameter values and then you execute
- 5:58that. Okay. So over the network what
- 6:02gets transmitted just this three words.
- 6:05Okay. But if this store procedure I mean
- 6:07if you don't have a store procedure if
- 6:10it is just ad hoc SQL that you want to
- 6:12send from the client tools then you have
- 6:15to send all those 3,000 lines of code
- 6:18which is definitely going to have a you
- 6:21know impact on network traffic. Okay. So
- 6:24stored procedure since the store
- 6:25procedure resides on SQL server itself.
- 6:28All you have to tell the store procedure
- 6:29to do is give the name of the stored
- 6:31procedure and the parameters and tell it
- 6:34to execute it. That's it. But if it is
- 6:36not a stored procedure, then you will
- 6:38have to pass all those ad hoc SQL
- 6:40statements from the client tool to the
- 6:43SQL server which will definitely
- 6:45increase the network
- 6:47traffic. So that's another benefit. So
- 6:49from a performance standpoints, you
- 6:51know, these are the two advantages,
- 6:53execution plan reuse and network traffic
- 6:57redaction. Okay. And another advantage
- 7:00of using stored procedures is code
- 7:02reusability and better maintainability.
- 7:04Now if it's a stored procedure, you know
- 7:06that resides on the server. Now several
- 7:09applications can reuse that stored
- 7:11procedure. If there are multiple
- 7:12applications which want a similar kind
- 7:15of data, they can just call that stored
- 7:17procedure. Okay. Now what is the
- 7:19advantage of doing so? The advantage of
- 7:21doing so is that tomorrow for some
- 7:24reason if there is a bug in that store
- 7:26procedure or the logic of the store
- 7:28procedure has to be changed you only
- 7:30have one place to change it irrespective
- 7:33of that being used by multiple
- 7:35applications. So maintainability also
- 7:38becomes much easier. Reusability
- 7:41reusability definitely promotes
- 7:42maintainability. So stored procedures
- 7:45does that. Whereas if it is an inline
- 7:47SQL, what's the problem? If it's an
- 7:49inline SQL, then every application has
- 7:52to issue those SQL statements. They
- 7:54prepare that SQL and then issue those
- 7:56SQL statements from the application. So
- 7:58you have this copies of SQL statements
- 8:01spread across all the applications where
- 8:03you want to reuse that and tomorrow if
- 8:05there has to be a change in the logic,
- 8:07you will have to change it across all
- 8:09those applications which makes
- 8:12maintenance a
- 8:13nightmare. Okay. So definitely stored
- 8:16procedures promote reusability and
- 8:18better
- 8:19maintainability and from a security
- 8:21standpoint okay they provide better
- 8:23security as well. Okay. And they provide
- 8:26security by two means. Let's understand
- 8:28what you know both of them in terms of
- 8:30finer grain
- 8:32control. Okay. So what do we mean by
- 8:34that? Let's say a user needs data from a
- 8:37specific table. Okay. Now if a user
- 8:40needs let's say I have IT department, HR
- 8:43department, payroll department and one
- 8:45of the managers or employee needs IT
- 8:47department data. Okay.
- 8:49Now if that table has got all these
- 8:53departments data it payroll HR and if I
- 8:56grant him access to the entire table
- 8:57what's going to happen is going to he's
- 8:59going to see all everybody's records and
- 9:02we don't don't want that okay so in
- 9:04those cases what you can do you can
- 9:06actually create a store procedure and
- 9:08grant him access to execute that store
- 9:10procedure without actually granting
- 9:13access to the underlying table itself.
- 9:16So this give allows us to have finer
- 9:18grain control over what data he can see.
- 9:22Okay. So stored procedures can provide
- 9:24security in that angle. Not only stored
- 9:27procedures, we can also use views for
- 9:30that purpose. Okay. But stored
- 9:31procedures is another
- 9:33way. And another way you know how stored
- 9:37procedures provide security is they
- 9:39avoid SQL injection attack. Now most of
- 9:43the people who are new to application
- 9:45development they dynamically build SQL
- 9:48statements in the application and then
- 9:50issue those to the to the SQL server
- 9:53database. Now what's going to happen is
- 9:55in the user interface elements people
- 9:58can type in you know SQL and inject that
- 10:01to the dynamically built up SQL code.
- 10:04Now if you if you if you don't know how
- 10:06to do that you know I have already
- 10:08recorded a video on what is SQL
- 10:10injection attack how key how how can I
- 10:13how we can actually inject SQL and how
- 10:16to prevent it using stored procedures. I
- 10:18have a video recorded already on this.
- 10:20Please watch this video. Okay. So these
- 10:23are the these are the greatest benefits
- 10:25of using stored procedures. Okay. From a
- 10:28performance standpoint they can reuse
- 10:30execution plan and reduce network
- 10:32traffic. and code reusability and better
- 10:35maintainability. And from a security po
- 10:38standpoint, you can have finer grain
- 10:40control by granting access only to the
- 10:43store procedure and not to the
- 10:44underlying tables. And they can also
- 10:47simply avoid SQL injection attack. I
- 10:49mean not only stood procedures, even if
- 10:51it's an ad hoc query, if they are
- 10:53parameterized queries, they still can
- 10:55avoid SQL injection
- 10:59attack. On this slide, you can find
- 11:01resources for ASP.NET net and uh C#
- 11:04interview questions. That's it for
- 11:06today. Thank you for listening. Have a
- 11:08great day.
About this transcript
This page contains the full transcript of Advantages of stored procedures Part 21 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 1,876 words across 278 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.