Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is better: calling procedure inside a procedure or executing sql inside a procedure?

I have 3 tables:
1. student: {id, name, roll}
2. subject: {id, name}
3. marks: {student_id, subject_id, marks}

I have a procedure which returns all the student and their marks (say getAllMarks() ), i want to create another procedure which returns marks of a given roll no (say getRollMarks(int rollno)). So there are two ways i can create the procedure:
1. write the appropriate SQL to get the result in getRollMarks procedure.
2. call getAllMarks in getRollMarks and then apply where clause on the result returned by getAllMarks.

Can anyone suggest pros and cons of above two methods? I have 2 Pros of method 2:
1.SQL queries won't be repeated across procedures.
2.If anything is changing in one table, the change will be done at one place/procedure, not in every procedure which is using that table.

PS: The tables are just for example, actually they are bigger tables with many parameters to query. The question can be simplified as should we write repeated queries in different procedures, or should we use existing procedures with required clause (where, order by, joins etc)?

like image 855
thekosmix Avatar asked Nov 03 '14 11:11

thekosmix


1 Answers

If you don't want to repeat the query, you should be using a view. Also performance wise it is faster.

like image 75
Aman B Avatar answered Oct 04 '22 18:10

Aman B