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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:09:12+00:00 2026-06-11T12:09:12+00:00

I have query which needs to be dynamic on some of the columns, meaning

  • 0

I have query which needs to be dynamic on some of the columns, meaning I get a parameter and according its value I decide which column to fetch in my Where clause. I’ve implemented this request using “CASE” expression:

(CASE @isArrivalTime WHEN 1 THEN ArrivalTime ELSE PickedupTime END) 
>= DATEADD(mi, -@TZOffsetInMins, @sTime) 
 AND (CASE @isArrivalTime WHEN 1 THEN ArrivalTime ELSE PickedupTime END) 
 < DATEADD(mi, -@TZOffsetInMins, @fTime)

If @isArrivalTime = 1 then chose ArrivalTime column else chose PickedupTime column. I have a clustered index on ArrivalTime and nonclustered index on PickedupTime.

I’ve noticed that when I’m using this query (with @isArrivalTime = 1), my performance is a lot worse comparing to only using ArrivalTime.

Maybe the query optimizer can’t use\choose the index properly in this way?

I compared the execution plans an noticed that when I’m using the CASE 32% of the time is being wasted on the index scan, but when I didn’t use the CASE(just usedArrivalTime`) only 3% were wasted on this index scan.

Anyone know the reason for this?

  • 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-11T12:09:13+00:00Added an answer on June 11, 2026 at 12:09 pm

    try to set datetime boundary:

    declare @resSTime datetime
            ,@resFTime datetime
    
    set @resSTime = DATEADD(mi, -@TZOffsetInMins, @sTime)
    set @resFTime = DATEADD(mi, -@TZOffsetInMins, @fTime)
    

    and try to change case to ‘or’

    ( ArrivalTime >= @resSTime 
        and ArrivalTime < @resFTime 
        and @isArrivalTime = 1 )
    or ( PickedupTime >= @resSTime 
        and PickedupTime < @resFTime 
        and @isArrivalTime <> 1 )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an excel workbook which needs to query data from an HTTP server.
So I have this app which needs to query entities from the Azure Tables
I have an ASP.NET application which needs to search a VARCHAR(20) column using lists
I have very long select query which i need to filter based on some
I have a dynamic query which returns a DataTable. This DataTable will contain a
I have a select query which returns a list of data and I need
I have a situation where I need to retrieve data from a query which
I need a query which can do the following operation. I have a table
I have a rather large table which I need to query for a reporting
I have Db which has data of an year...so I need a query to

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.