Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6638703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:28:02+00:00 2026-05-25T23:28:02+00:00

Given the view create view MyView as select Id, ExpensiveFunction(Name) as Expr from MyTable

  • 0

Given the view

create view MyView as
select Id, ExpensiveFunction(Name) as Expr from MyTable

If used in the query

select *
from AnotherTable a 
  inner join MyView b on b.Id = a.Id

Is ExpensiveFunction computed for every row in MyTable, or is SQL Server smart enough to join to AnotherTable first, and then call ExpensiveFunction for only the filtered rows?

Would it behave differently if MyView is a subquery or TVF?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:28:02+00:00Added an answer on May 25, 2026 at 11:28 pm

    ExpensiveFunction will only be called when it is relevant. If your final select statement does not even include it at all, the function will, in turn, not be called.

    Similarly, if the final select only selects one tenth of the table, the function will only be called on that one tenth.

    This works the same way in sub-queries and inline views. You could specify a function for a field in a sub-query, and if the field is never used, the function will never be executed.

    It is true that scalar functions are notoriously expensive. It might be possible to reduce the overhead by instead using a table valued function:

    SELECT 
      MyTable.*, 
      ExpensiveFunction.Value 
    FROM 
      MyTable 
    CROSS APPLY 
      MyFunction(MyTable.field1, MyTable.field2) as ExpensiveFunction 
    

    Provided that ExpensiveFunction is an inline (i.e. not multi-statement) function, and that it only returns one row, it will usually scale better than scalar functions do.

    If you use a multi-statement table valued function that returns multiple rows, it will call ExpensiveFunction for every row. Generally, though, you shouldn’t have TVFs that return records that will later be discarded.

    Inline functions, on the other hand, since they can be expanded inline, as if they were a SQL macro, will only call ExpensiveFunction when necessary, like the view.

    In the example in your question, ExpensiveFunction should only be called when necessary. To be sure, though, you should definitely look at your query execution plan and use an SQL profiler to optimize performance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a veiw and then select from it in one query,
When I go to my donors_controller#create from a form in my view, it will
I'd like to make sure a given view in my test is fetching an
Given an ASP.NET MVC view that generates a table of entries using a for
How do I view the grants (access rights) for a given user in MySQL?
In my WPF application I have a View that is given a ViewModel, and
I can see that Collections.unmodifiableSet returns an unmodifiable view of the given set but
I have a Django project. Given a url, how can I know which view
When creating a view, the name assigned to a column is not always usable
In my view page, i am using form_tag to create a form which will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.