0

In a recent interview the interviewer asked:

"Would you write a simple SQL query OR Write that SQL query in PL/SQL as a stored procedure."

My question is, what's the difference, and when should each be used?

1
  • Could be looking for you to recognize different keywords in Postgres, or it could be testing if you know what a stored procedure is. Commented Aug 4, 2016 at 12:55

3 Answers 3

2

SQL query is usually faster than PL/SQL query, beacuse Oracle dont have to change engine to execute.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for fast responce
so if i write SQL query in store procedure, its take more time for execution compare to run simple SQL query
The answer is "it depends", Your question is too general, give more details.
2

To understand this you need to know about context switching. When you write a simple sql, only Oracle SQL engine plays. But when you write a sql statement in a PL/SQL, SQL engine makes a context switch to PL/SQL engine and hence the statement is executed. The more is the context switching the more bad will be the performance. Check below link and you may google more on context switching .

http://www.oracle.com/technetwork/issue-archive/2015/15-jul/o45plsql-2543984.html

Comments

0

Essentially there is no difference, since the SQL engine executes the query regardless of the host language - it could be COBOL for all it cares.

There is a slight overhead of the extra component (you call the procedure and it executes the SQL, instead of you just executing the SQL directly), however that step can normally be measured in microseconds. (And as soon as the procedure does more than one thing - two queries, for example - then the advantage is back with the procedure.)

There are permissions implications, since definer-rights stored PL/SQL can not see roles while standalone SQL can.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.