When does it make sense to use a function in a view

  • I'm developing a 'busy' view with a several datetime and string conversions. I don't mind cut-n-pasting throughout. But, it would be cleaner and easier to read if I used functions.

    My question is regarding CPU usage: Is it better (more efficient) to have a 'busy' view with no functions OR a 'clean' view using a few functions?

  • Since the aim of your question is at "efficient" I would suggest that (1) scalar functions and (2) multi-line table value functions will add "drag" to your views and will hurt efficiency.

    Inline table functions should optimize well and can frequently be used without the efficiency problems especially inherent with scalar functions.

  • Not seeing the code, this might not be applicable, but if you're creating the same derived tables over and over again as part of the query of the view, you might want to look up Common Table Expressions in BOL.

    ----------------------------------------------------The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood... Theodore RooseveltThe Scary DBAAuthor of: SQL Server 2017 Query Performance Tuning, 5th Edition and SQL Server Execution Plans, 3rd EditionProduct Evangelist for Red Gate Software

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply