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

  • Home
  • SEARCH
  • 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 3873306
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:01:17+00:00 2026-05-19T22:01:17+00:00

Please bear with me — I know this is complex. I have a table

  • 0

Please bear with me — I know this is complex.

I have a table that contains apartments, and another that contains leases for those apartments. My task is to select the “most relevant” lease from the list. In general, that means the most recent lease, but there are a few quirks that make it more complex than just ordering by date.

That has led me to create this common table expression query inside a View, which I then JOIN with a number of others inside a Stored Procedure to get the results I need:

WITH TempTable AS (
    SELECT  l.BuildingID, l.ApartmentID, l.LeaseID, l.ApplicantID,
                ROW_NUMBER() OVER (PARTITION BY l.ApartmentID ORDER BY s.Importance DESC, MovedOut, MovedIN DESC, LLSigned DESC, Approved DESC, Applied DESC) AS 'RowNumber'
    FROM    dbo.NPleaseapplicant AS l INNER JOIN
            dbo.NPappstatus AS s ON l.BuildingID = s.BuildingID AND l.AppStatus = s.Code

)

SELECT  BuildingID, ApartmentID, LeaseID, ApplicantID
FROM    TempTable
WHERE   RowNumber = 1

This works and returns the correct result. The challenge I’m facing is very slow performance.

As a test, I created a temp table inside the Stored Procedure instead of using View, and got much, much better performance:

CREATE TABLE #Relevant (
    BuildingID int,
    ApartmentID int,
    LeaseID int,
    ApplicantID int,
    RowNumber int
)

INSERT INTO #Relevant (BuildingID, ApartmentID, LeaseID, ApplicantID, RowNumber)
SELECT  l.BuildingID, l.ApartmentID, l.LeaseID, l.ApplicantID,
            ROW_NUMBER() OVER (PARTITION BY l.ApartmentID ORDER BY s.Importance DESC, MovedOut, MovedIN DESC, LLSigned DESC, Approved DESC, Applied DESC) AS 'RowNumber'
FROM    dbo.NPleaseapplicant AS l INNER JOIN
        dbo.NPappstatus AS s ON l.BuildingID = s.BuildingID AND l.AppStatus = s.Code
WHERE   (l.BuildingID = @BuildingID)

DROP TABLE #Relevant

At first glance this doesn’t add up to me. I’ve heard that temp tables are notoriously bad for performance. The wrinkle is that I’m able to better limit the query in the Temp Table with a WHERE clause that I can’t in the View. With over 10,000 leases across 16 buildings in the table, the ability to filter with the WHERE may drop the rows affected by 90% – 95%.

Bearing all that in mind, is there anything glaring that I’m missing here? Am I doing something wrong with the View that might cause the dreadful performance, or is it just a matter of a smaller result set in the Temp Table beating the unrestricted result set in the CTE?

EDIT: I should add that this business logic of selecting the “most relevant lease” is key to many reports in the system. That’s why it was placed inside a View to begin with. The View gives us “Write Once, Use Many” capabilities whereas a Temp Table in a Stored Procedure would need to be recreated for every other Stored Proc in the system. Ugly.

EDIT #2: Could I use a Table Based Function instead of a view? Would that allow me to limit the rows affected up front, and still use the resulting dataset in a JOIN with other tables? If it works — and has decent performance — this would allow me to keep the business logic in one place (the function) instead of duplicating it in dozens of Stored Procedures.

  • 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-19T22:01:18+00:00Added an answer on May 19, 2026 at 10:01 pm

    Just to put a bow on this one, here’s what I ended up doing:

    Instead of using a View to join all possible rows from 2 or 3 tables, I created a Table Based Function that makes the same basic query. As one of the parameters I pass in the Building ID, and use that in a WHERE clause, like so:

    SELECT  l.BuildingID, l.ApartmentID, l.LeaseID, l.ApplicantID,
                ROW_NUMBER() OVER (PARTITION BY l.ApartmentID ORDER BY s.Importance DESC, MovedOut, MovedIN DESC, LLSigned DESC, Approved DESC, Applied DESC) AS 'RowNumber'
    FROM    dbo.NPleaseapplicant AS l INNER JOIN
            dbo.NPappstatus AS s ON l.BuildingID = s.BuildingID AND l.AppStatus = s.Code
    WHERE  (l.BuildingID = @BuildingID)
    

    The result is that it drastically reduces the number of joins required, and it speeds up the query immensely.

    I then change all the stored procedures that rely on the View to use the Function instead, and bingo — huge performance gains.

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

Sidebar

Related Questions

This is all hypothetical, so please bear with me. Say I'm writing a tool
This is a long text. Please bear with me. Boiled down, the question is:
This might be a bit difficult to explain in writing, so please bear with
Please bear with me if this question isn't well formulated. Not knowing is part
Please bear with me Silverlight Designer Gurus, this is compicated (to me). I'm creating
Please bear with me as I've been thrown into the middle of this project
Please bear with me. I really want to know as I am curious about
I'm certain this has been asked before-in other ways-please bear with me (for a
I'm a bit new to rails so if this is very basic please bear
I'm new to JQuery, so please bear with me =) I have a Menu

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.