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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:59:06+00:00 2026-05-17T00:59:06+00:00

I am working on an ASP.NET MVC project which allows users to construct arbitrarily

  • 0

I am working on an ASP.NET MVC project which allows users to construct arbitrarily complex queries by adding items clause at a time.

The application then generates appropriate SQL, runs it (currently using SQL Server 2008) and displays the results, with a breakdown which includes the number of matching records for each added item.

e.g.

UserQuery:

Has ConditionA (45)
And ConditionB (33)
Or ConditionC (55)
And ConditionD (15)

Total: 48

The problem is how best to build and run the SQL to produce these results with performance and scalability in mind.

The initial implementation built a query (using subqueries) for each item (combined with the previous) in turn, running them separately as scalars. Each execution involved generating the SQL and opening new SqlConnection, creating new SqlCommand and executing.

I spent a while re-writing this to produce a single query (which uses CTEs) to return a single row with the result of each item as a column.

This only required a single execution and performance seemed marginally favourable until the queries became complex and SQL Server started throwing errors:

The query processor ran out of
internal resources and could not
produce a query plan

What would be the most scalable and efficient way of building and running such a query?

  • 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. Editorial Team
    Editorial Team
    2026-05-17T00:59:07+00:00Added an answer on May 17, 2026 at 12:59 am

    The way forward for us was to construct the query using temporary tables for each clause, where each subsequent clause added was applied (via Union/Intersection/Exception) to the temporary table resulting from the previous clause.

    A temp table is also created for results and is updated with a TempTable Id and row count for each as they are populated.

    When the query has been processed the results are returned by selecting all rows from the results temp table which gives a full item by item breakdown.

    This prevented the need for gigantic SQL query statments featuring many many subqueries and also prevented constant re-execution of the same SQL, providing massive improvement in scalbility and performance.

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

Sidebar

Related Questions

I'm working on an ASP.NET MVC project which uses the MarkdownDeep Editor to add
I'm working on an ASP.NET MVC 2 / .NET 3.5 project which includes SSRS
I am working with an ASP.NET MVC project which was originally started from the
I am currently working on a ASP.NET MVC 3 project in which I have
I am currently working on a ASP.NET MVC 3 project in which I need
I'm working on an ASP.Net MVC 3 project, which needs localized error messages from
I'm working on a Web project using Asp.Net MVC, which I'll have to deploy
background:Me and my coworkers are working on asp.net mvc project ... we have a
I am currently working on an ASP.NET MVC project. Some developers on the team
I working on my first ASP.net MVC project and and i got some problems

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.