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 115827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:04:53+00:00 2026-05-11T03:04:53+00:00

I have a case where I need to translate (lookup) several values from the

  • 0

I have a case where I need to translate (lookup) several values from the same table. The first way I wrote it, was using subqueries:

SELECT     (SELECT id FROM user WHERE user_pk = created_by) AS creator,     (SELECT id FROM user WHERE user_pk = updated_by) AS updater,     (SELECT id FROM user WHERE user_pk = owned_by) AS owner,     [name] FROM asset 

As I’m using this subquery a lot (that is, I have about 50 tables with these fields), and I might need to add some more code to the subquery (for example, "AND active = 1" ) I thought I’d put these into a user-defined function UDF and use that. But the performance using that UDF was abysmal.

CREATE FUNCTION dbo.get_user ( @user_pk INT ) RETURNS INT AS BEGIN      RETURN ( SELECT id              FROM   ice.dbo.[user]              WHERE  user_pk = @user_pk ) END  SELECT dbo.get_user(created_by) as creator, [name] FROM asset 

The performance of #1 is less than 1 second. Performance of #2 is about 30 seconds…

Why, or more importantly, is there any way I can code in SQL server 2008, so that I don’t have to use so many subqueries?

Edit:

Just a litte more explanation of when this is useful. This simple query (that is, get userid) gets a lot more complex when I want to have a text for a user, since I have to join with profile to get the language, with a company to see if the language should be fetch’ed from there instead, and with the translation table to get the translated text. And for most of these queries, performance is a secondary issue to readability and maintainability.

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T03:04:53+00:00Added an answer on May 11, 2026 at 3:04 am

    The UDF is a black box to the query optimiser so it’s executed for every row. You are doing a row-by-row cursor. For each row in an asset, look up an id three times in another table. This happens when you use scalar or multi-statement UDFs (In-line UDFs are simply macros that expand into the outer query)

    One of many articles on the problem is ‘Scalar functions, inlining, and performance: An entertaining title for a boring post‘.

    The sub-queries can be optimised to correlate and avoid the row-by-row operations.

    What you really want is this:

    SELECT    uc.id AS creator,    uu.id AS updater,    uo.id AS owner,    a.[name] FROM     asset a     JOIN     user uc ON uc.user_pk = a.created_by     JOIN     user uu ON uu.user_pk = a.updated_by     JOIN     user uo ON uo.user_pk = a.owned_by 

    Update Feb 2019

    SQL Server 2019 starts to fix this problem.

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

Sidebar

Related Questions

I have a case where I need to load bitmap from a resource dll
I have a case where I need to select a random item, but I
I have a case where I need to update a jqgrid based on some
I have a use case where I need to call a (non-static) method in
I have a very specific case that I need to debug. I need to
I have a use case where I only need to store certain fields to
I have a complicated problem, and I need help. I have a base case,
I have a strong use case for pre-allocating all the memory I need upfront
I have a few strings which I need to translate and display. Those strings
I have a requirement where I need to check DB/@dbtype == 'oracle' (case insensitive).

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.