July 21, 2017 at 7:38 am
Hi All,
Happy Friday to All.
Is this a good practice to write functions with 1000+ lines. I attended one SQLSaturday event and one of the experts from Microsoft was giving advises to use Procs instead of functions. If the function is more than 5-10 lines it is better to replace it with a procedure.
thought he did not give any supporting reasons but wanted to know a bit more on this.
If there is any related articles / advise available please do share.
Thanks.
July 21, 2017 at 7:48 am
I typed "function v stored procedure" into my favourite search engine and got a whole load of results. You could do the same.
I'm not sure what your expert was getting at. It's not about length - it's about which is the more appropriate to use. Sure, if you have a an object definition that's over 1000 lines long, you may find that you can modularise it by breaking it into component parts. This makes it simpler to understand each individual part and reduces the amount of maintenance needed when you need to make a change.
John
July 21, 2017 at 7:48 am
SQL-DBA-01 - Friday, July 21, 2017 7:38 AMHi All,
Happy Friday to All.
Is this a good practice to write functions with 1000+ lines. I attended one SQLSaturday event and one of the experts from Microsoft was giving advises to use Procs instead of functions. If the function is more than 5-10 lines it is better to replace it with a procedure.thought he did not give any supporting reasons but wanted to know a bit more on this.
If there is any related articles / advise available please do share.
As with all else, "IT DEPENDS". What is even more important is the type of function being used. Scalar will be very bad for this. mTVF will be very, very bad for this. iTVF might be OK depending on how it was written and what it's being used for.
Having looked at many of the system stored procedures, I'd have to say that you should take such advice from MS with a grain of salt.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 21, 2017 at 7:49 am
SQL-DBA-01 - Friday, July 21, 2017 7:38 AM1000Hi All,
Happy Friday to All.
Is this a good practice to write functions with 1000+ lines. I attended one SQLSaturday event and one of the experts from Microsoft was giving advises to use Procs instead of functions. If the function is more than 5-10 lines it is better to replace it with a procedure.thought he did not give any supporting reasons but wanted to know a bit more on this.
If there is any related articles / advise available please do share.
I think it's bad practice to write any single chunk of code that is 1000+ lines regardless of what it is, if you can't segment it out at that point you're already doing something wrong.
July 21, 2017 at 8:00 am
Using a stored procedure will greatly reduce that traffic because the stored procedure call will always be much shorter. Also, stored procedures are easier to trace in Profiler or any other tool. A stored procedure is an actual object in your database. That means it's much easier to get performance statistics on a stored procedure than on an ad-hoc query and, in turn, find performance issues and draw out anomalies.
In addition, stored procedures parameterize more consistently. This means youβre more likely to reuse your execution plans and even deal with caching issues, which can be difficult to pin down with ad-hoc queries. Stored procedures also make it much easier to deal with edge cases and even add auditing or change-locking behavior. A stored procedure can handle many tasks that trouble ad-hoc queries.
Can all the above tasks being performed similarly using functions? When I look at the Execution plan of Function, it looks difficult to read through and the blocks just point about the function name.
Thanks.
July 21, 2017 at 10:02 am
SQL-DBA-01 - Friday, July 21, 2017 8:00 AM
Using a stored procedure will greatly reduce that traffic because the stored procedure call will always be much shorter. Also, stored procedures are easier to trace in Profiler or any other tool. A stored procedure is an actual object in your database. That means it's much easier to get performance statistics on a stored procedure than on an ad-hoc query and, in turn, find performance issues and draw out anomalies.
In addition, stored procedures parameterize more consistently. This means you’re more likely to reuse your execution plans and even deal with caching issues, which can be difficult to pin down with ad-hoc queries. Stored procedures also make it much easier to deal with edge cases and even add auditing or change-locking behavior. A stored procedure can handle many tasks that trouble ad-hoc queries.
Can all the above tasks being performed similarly using functions? When I look at the Execution plan of Function, it looks difficult to read through and the blocks just point about the function name.
What KIND of function? If you not using an iTVF, you stand a pretty good chance of having performance challenged code.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 21, 2017 at 10:09 am
This is one of the questions I ask while interviewing people for a SQL Developer position.
The importance of understanding the difference between a function and a stored procedure is fundamental for development. They both serve different purposes. As mentioned by Jeff, some functions while have a performance hit that you usually want to avoid. But most important, is to understand when to use a hammer and when to use a screwdriver.
July 21, 2017 at 1:29 pm
SQL-DBA-01 - Friday, July 21, 2017 8:00 AMUsing a stored procedure will greatly reduce that traffic because the stored procedure call will always be much shorter. Also, stored procedures are easier to trace in Profiler or any other tool. A stored procedure is an actual object in your database. That means it's much easier to get performance statistics on a stored procedure than on an ad-hoc query and, in turn, find performance issues and draw out anomalies.
In addition, stored procedures parameterize more consistently. This means you’re more likely to reuse your execution plans and even deal with caching issues, which can be difficult to pin down with ad-hoc queries. Stored procedures also make it much easier to deal with edge cases and even add auditing or change-locking behavior. A stored procedure can handle many tasks that trouble ad-hoc queries.
This quote seems largely irrelevant to the topic being discussed, because it is comparing stored procs to ad hoc queries, not to functions.
Or are you referring to functions in C# code, rather than SQL Server functions?
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
July 21, 2017 at 1:59 pm
Luis Cazares - Friday, July 21, 2017 10:09 AMThis is one of the questions I ask while interviewing people for a SQL Developer position.
The importance of understanding the difference between a function and a stored procedure is fundamental for development. They both serve different purposes. As mentioned by Jeff, some functions while have a performance hit that you usually want to avoid. But most important, is to understand when to use a hammer and when to use a screwdriver.
There are also times when you need to use a very special screw-hammer or nail-driver. π
--Jeff Moden
Change is inevitable... Change for the better is not.
July 21, 2017 at 2:24 pm
Jeff Moden - Friday, July 21, 2017 1:59 PMLuis Cazares - Friday, July 21, 2017 10:09 AMThis is one of the questions I ask while interviewing people for a SQL Developer position.
The importance of understanding the difference between a function and a stored procedure is fundamental for development. They both serve different purposes. As mentioned by Jeff, some functions while have a performance hit that you usually want to avoid. But most important, is to understand when to use a hammer and when to use a screwdriver.There are also times when you need to use a very special screw-hammer or nail-driver. π
That's why we have the hammer drill, but sometimes you don't want to give power tools to people who don't know how to use them π
July 22, 2017 at 5:34 pm
Luis Cazares - Friday, July 21, 2017 2:24 PMJeff Moden - Friday, July 21, 2017 1:59 PMLuis Cazares - Friday, July 21, 2017 10:09 AMThis is one of the questions I ask while interviewing people for a SQL Developer position.
The importance of understanding the difference between a function and a stored procedure is fundamental for development. They both serve different purposes. As mentioned by Jeff, some functions while have a performance hit that you usually want to avoid. But most important, is to understand when to use a hammer and when to use a screwdriver.There are also times when you need to use a very special screw-hammer or nail-driver. π
That's why we have the hammer drill, but sometimes you don't want to give power tools to people who don't know how to use them π
Heh... true enough. Otherwise, you might end up with something that looks like this...
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply