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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:56:33+00:00 2026-06-13T21:56:33+00:00

I have a piece of code in an ASP.NET application that builds a SQL

  • 0

I have a piece of code in an ASP.NET application that builds a SQL query from a list of parameters. The number of parameters can vary, so various criteria can be added to this query. The database is Microsoft SQL Server 2008.

All the AND and OR’s are generated programmatically.

The query took more than 3 seconds to execute, but after some profiling and indexes it runs less than a second. Still I think the query itself can be optimized. I’ve looked at execution plans, but that doesn’t mean much to me – not being a SQL guru.

I wonder if the query can be done in a more intelligent fashion – I haven’t been able to figure it out. Here’s an example of the query:

 SELECT [id], [WorkTitle], [CreateDate], [UpdateDate], [Writer], [ValidFrom], 

[ValidTo], [Text] 
 FROM dbo.Texts T  
 WHERE Category_id = 3 AND '2012-11-06' BETWEEN ValidFrom AND ValidTo  
 AND (EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id   AND CL.Criteria_id = 1 AND CL.Value = '95068')       
    OR NOT EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 1))  

 AND (EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id   AND CL.Criteria_id = 2 AND CL.Value = 'C')       
    OR NOT EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 2))  

 AND (EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id   AND CL.Criteria_id = 3 AND CL.Value = 'HEL')       
    OR NOT EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 3))  

 AND (EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id   AND CL.Criteria_id = 4 AND CL.Value = 'CC')      
    OR NOT EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 4))  

 AND (EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id   AND CL.Criteria_id = 5 AND CL.Value = NULL)     
    OR NOT EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 5))  

 AND (EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id   AND CL.Criteria_id = 7 AND CL.Value = '321')      
    OR NOT EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 7))  

 AND (EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id   AND CL.Criteria_id = 9 AND CL.Value = 'DK7778')    
    OR NOT EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 9))  

 AND (EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id   AND CL.Criteria_id = 10 AND CL.Value = 'TFS')   
    OR NOT EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 10)) 

 AND (EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id   AND CL.Criteria_id = 11 AND CL.Value = 'TMP')   
    OR NOT EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 11)) 

 AND (EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id   AND CL.Criteria_id = 13 AND CL.Value = 'OY-VKB')   
    OR NOT EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 13)) 

Any tips and tricks are appreciated.

Cheers
Jens

  • 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-13T21:56:34+00:00Added an answer on June 13, 2026 at 9:56 pm
    • For table Texts I’d add an index on (Category_id, ValidFrom) INCLUDE (ValidTo), if there isn’t one. (If you already have indexes on either (Category_id, ValidFrom, ValidTo) or on (Category_id, ValidFrom), they may be quite good too.

    • For the Criteria_List table an index on (Text_id, Criteria_id, Value) would probably be enough for the optimizer to produce a good execution plan.

      The second option (or maybe even better, you’d have to test the execution plans and running times with your tables sizes and distributions) would be two indexes, one on (Criteria_id, Text_id) and one on (Criteria_id, Value, Text_id).


    You could rewrite the 10 conditions like this – but the indexing should be taken care anyway:

     WHERE Category_id = 3 AND '2012-11-06' BETWEEN ValidFrom AND ValidTo  
     AND NOT EXISTS ( SELECT 'X' FROM dbo.Criteria_List CL 
                      WHERE T.id = CL.Text_id AND CL.Criteria_id = 1 
                        AND (CL.Value <> '95068' OR CL.Value IS NULL)
                    ) 
     AND NOT EXISTS ( SELECT 'X' FROM dbo.Criteria_List CL 
                      WHERE T.id = CL.Text_id AND CL.Criteria_id = 2 
                        AND (CL.Value <> 'C' OR CL.Value IS NULL)
                    ) 
     ...
     AND NOT EXISTS ( SELECT 'X' FROM dbo.Criteria_List CL 
                      WHERE T.id = CL.Text_id AND CL.Criteria_id = 5 
                        AND (CL.Value IS NOT NULL)
                    ) 
     ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a code library that works in ASP.NET, the SQL CLR, and stand-alone
I have heard that you can run an ASP.NET application and ASP.NET mvc application
I have a piece of code (below) that can get the text of an
I have an ASP.NET 4 web application that references a SOAP web service. I
I have two very similar pieces of ASP.NET code that send a file in
We currently have an existing ASP.NET application that we want to migrate to DNN.
I have an ASP.NET application that relies on the Random class to generate a
My task is to create an ASP.NET application with a piece of code which
I have a piece of code that was written by someone else before the
I have this piece of code for my images, so the opacity can lower

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.