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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:01:09+00:00 2026-05-14T07:01:09+00:00

I have a SQL query that I’m trying to debug. It works fine for

  • 0

I have a SQL query that I’m trying to debug. It works fine for small sets of data, but in large sets of data, this particular part of it causes it to take 45-50 seconds instead of being sub second in speed. This subquery is one of the select items in a larger query. I’m basically trying to figure out when the earliest work date is that fits in the same category as the current row we are looking at (from table dr)

ISNULL(CONVERT(varchar(25),(SELECT MIN(drsd.DateWorked) FROM [TableName] drsd
  WHERE drsd.UserID = dr.UserID
        AND drsd.Val1 = dr.Val1
        OR (((drsd.Val2 = dr.Val2 AND LEN(dr.Val2) > 0) AND (drsd.Val3 = dr.Val3 AND LEN(dr.Val3) > 0) AND (drsd.Val4 = dr.Val4 AND LEN(dr.Val4) > 0))
        OR (drsd.Val5 = dr.Val5 AND LEN(dr.Val5) > 0)
        OR ((drsd.Val6 = dr.Val6 AND LEN(dr.Val6) > 0) AND (drsd.Val7 = dr.Val7 AND LEN(dr.Val2) > 0))))), '') AS WorkStartDate,

This winds up executing a key lookup some 18 million times on a table that has 346,000 records. I’ve tried creating an index on it, but haven’t had any success. Also, selecting a max value in this same query is sub second in time, as it doesn’t have to execute very many times at all.

Any suggestions of a different approach to try? Thanks!

  • 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-14T07:01:10+00:00Added an answer on May 14, 2026 at 7:01 am

    Create a composite index on drsd (UserID, DateWorked).

    It is also possible that the record distribution in drsd is skewed towards the greater dates, like this:

    DateWorked   Condition
    
    01.01.2001   FALSE
    02.01.2001   FALSE
    …
    18.04.2010   FALSE
    19.04.2010   TRUE
    

    In this case, the MAX query will need to browse over only 1 record, while the MIN query will have to browse all records from 2001 and further on.

    In this case, you’ll need to create four separate indexes:

    UserId, Val1, DateWorked
    UserId, Val2, Val3, Val4, DateWorked
    UserId, Val5, DateWorked
    UserId, Val6, Val7, DateWorked
    

    and rewrite the subquery:

    SELECT  MIN(dateWorked)
    FROM    (
            SELECT  MIN(DateWorked) AS DateWorked
            FROM    drsd
            WHERE   UserID = dr.UserID
                    AND Val1 = dr.Val1
            UNION ALL
            SELECT  MIN(DateWorked)
            FROM    drsd
            WHERE   UserID = dr.UserID
                    AND drsd.Val2 = dr.Val2 AND LEN(dr.Val2) > 0
                    AND drsd.Val3 = dr.Val3 AND LEN(dr.Val3) > 0
                    AND drsd.Val4 = dr.Val4 AND LEN(dr.Val4) > 0
            UNION ALL
            SELECT  MIN(DateWorked)
            FROM    drsd
            WHERE   UserID = dr.UserID
                    AND drsd.Val5 = dr.Val5 AND LEN(dr.Val5) > 0
            UNION ALL
            SELECT  MIN(DateWorked)
            FROM    drsd
            WHERE   UserID = dr.UserID
                    AND drsd.Val6 = dr.Val6 AND LEN(dr.Val6) > 0
                    AND drsd.Val7 = dr.Val7 AND LEN(dr.Val7) > 0
            ) q
    

    Each query will use its own index and the final query will just select the minimal of the four values (which is instant).

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

Sidebar

Related Questions

I have this SQL Query that pulls data from 3 tables. I am unable
I have a SQL query that I'm trying to write, but I'm not quite
I have an SQL query that works on older version of MySQL4 but won't
I have a SQL query that does some ranking, like this: SELECT RANK() OVER(PARTITION
I have a sql query that is retrieving results from a table but I
I have a sql query that looks like this: SELECT SUM(A) AS expr1 FROM
I have a SQL query that goes like this: UPDATE User SET flag='Y' WHERE
I have an SQL Query that i'm running but I only want to select
i have a sql query that returns 3 levels of a data chain on
In my grails application I have an sql query that selects column data 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.