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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:28:50+00:00 2026-06-16T02:28:50+00:00

I have a very complex SQL View. My Problem is, that its very slow

  • 0

I have a very complex SQL View. My Problem is, that its very slow and need a lot of memory When i want to make a report.
What the View do:
It queries a total value and multiply it with a factor
Obviously it’s because it contains a lot subqueries… (9 if you count it)

The query runs on a system with Millions of datasets.

My question. Where are points to optimize it? Would it be faster, if i make prepared procedures oder create sql views for the subqueries?

SELECT 
    CASE BID WHEN 1 THEN 
        b.TOTAL 
    ELSE 
        (b.TOTAL * - 1) 
    END 
    * 
    CASE WHEN
        (SELECT 
            (CAST(DATEDIFF(day, 
                ISNULL
                ((SELECT dbo.TableA.TimestampFrom + 1 AS start FROM dbo.TableB INNER JOIN
                    dbo.TableA ON dbo.TableB.SID = dbo.TableA.SID
                    WHERE   (dbo.TableB.PID = dbo.TableC.PID) AND
                        (dbo.TableB.MString = 'S' OR dbo.TableB.MString = 'ST') AND
                        (dbo.TableB.TimestampCreated <
                            (SELECT TimestampCreated
                                FROM dbo.TableB AS TableB_2
                            WHERE (RID = dbo.TableC.RID)))
                        AND
                            (dbo.TableB.VPFrom =
                                    (SELECT VPFrom FROM dbo.TableB AS TableB_1
                                            WHERE (RID = dbo.TableC.RID))
                            )), VPFrom), VPTo)
                            AS Float) + 1)
            / 
            (CAST(DATEDIFF(day, start, Vertragsende) AS Float) + 1) AS Faktor

            FROM dbo.TableC
                WHERE (LaufNr = b.LaufNr))
                    > 1 
        THEN 
            1 
        ELSE
            (SELECT (CAST(DATEDIFF(day, 
                ISNULL
                    ((SELECT dbo.TableA.TimestampFrom + 1 AS start FROM dbo.TableB INNER JOIN
                            dbo.TableA ON dbo.TableB.SID =  dbo.TableA.SID
                            WHERE (dbo.TableB.PID = dbo.TableC.PID) AND
                                (dbo.TableB.MString = 'S' OR dbo.TableB.MString = 'ST') AND
                                (dbo.TableB.TimestampCreated <
                                    (SELECT TimestampCreated FROM dbo.TableB AS TableB_2
                                     WHERE (RID = dbo.TableC.RID)))
                                AND (dbo.TableB.VPFrom =
                                    (SELECT VPFrom FROM dbo.TableB AS TableB_1
                                    WHERE (RID = dbo.TableC.RID)))
                        ), VPFrom),VPTo) AS Float) + 1)
                / (CAST(DATEDIFF(day, start, Vertragsende) AS Float) + 1) AS Faktor 
                FROM dbo.TableC
                WHERE (LaufNr = b.LaufNr)) 
        END AS GrossPremium
    ,RID, PID, ONR, OPosLaufNr, LaufNr, bdate
FROM dbo.TableC AS b
  • 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-06-16T02:28:52+00:00Added an answer on June 16, 2026 at 2:28 am

    The rules of thumb for indexing are to

    • first, read the execution plan,

    then think about adding an index to

    • every column used in a WHERE clause,
    • every column used in a JOIN condition,
    • every column used in an ORDER BY.

    In some cases, one multi-column index will be faster in SELECT statements than several single-column indexes.

    Then, make sure your WHERE clauses are sargable.

    Finally, it does make sense to try converting some of the subqueries into views for testing. My guess is that the root cause of the problem is that you’re executing different subqueries depending on the return value of several CASE statements.

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

Sidebar

Related Questions

I have a tricky problem. I have a very complex view in MS SQL
I have a very complex Linq to SQL query that returns a result set
I have a sql that is not very complex but sufficiently confusion that I
The problem: we have a very complex search query. If its result yields too
I have a very complex web app project I want to re-structure. Naturally, it
We have a very complex Oracle package here (over 4,100 lines of code) that's
I have a very complex Microsoft Access report. This report is run for multiple
I have a series of very complex XML files. I need to access these
I am using SQL Server 2008 and have a very large CASE statement that
I have a very complex process that involves downloading a number of files from

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.