which one is better in both use of JOIN in sql query or stored function in
the query?
I have following tables:
1. student (stuID, classId, addId ,name)
2. student_address (addId, stuId, city, state)
3. student_hobby (stuId, hobby )
4. student_class (classId, stuId, className)
I have two options:-
One option is:
query to get all detail of the student using join:-
select s.name, sd.city, sc.className
from student as s join student_address sd on s.addId = sd.addId
inner join student_class sc on sc.classId = s.classId
inner join student_hobby sh on sh.stuId = s.stuId
where sh.hobby REGEXP 'cricket|footbal';
another option is using stored function:
select s.name, sd.city, sc.className
from student as s join student_address sd on s.addId = sd.addId
inner join student_class sc on sc.classId = s.classId
where f_searchHobby(s.stuId,'cricket|footbal')=1;
create function f_searchHobby(
sId int,
matches varchar(100)
) returns int
begin
select count(*) into @count from student_hobby where hobby regexp matches
and stuId = sId;
if @count > 1 then
return 1 ;
else
return 0;
end if;
end
Consider both result are getting resultset.
So let me suggest which approach will work better for heavy database.
Thanks,
No comments:
Post a Comment