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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:58:24+00:00 2026-06-12T22:58:24+00:00

A database guru has suggested refactoring a query: SELECT * FROM MyTable WHERE UnIndexedDate

  • 0

A database guru has suggested refactoring a query:

SELECT * FROM MyTable 
WHERE UnIndexedDate 
BETWEEN '2012-08-01' AND '2012-09-01'

to

SELECT * FROM MyTable
WHERE IndexedID 
BETWEEN (SELECT MIN(IndexedID) FROM MyTable WHERE UnIndexedDate BETWEEN '2012-08-01' AND  '2012-08-30')
AND (SELECT MAX(IndexedID) FROM MyTable WHERE UnIndexedDate BETWEEN '2012-08-01' AND  '2012-08-30')

Note that the table is not indexed on the UnIndexedDate column but is indexed on the IndexedID column. This table has several millions of records in it.

It clearly does improve the speed of the query and I suspect it is because the subqueries will only be performed once and may even be far more efficient in some way because they involve an indexed field.

My question is, does this apply generally across most databases or just the SQL2000 one here.

Added: BTW IndexedID is numeric, unique and strictly increasing.

  • 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-12T22:58:25+00:00Added an answer on June 12, 2026 at 10:58 pm

    Your optimization will only work if the unindexed date column increases in conjuntion with the indexed id column; this is an assumption of DESIGN, not of the platform on which it runs. In other words, it sounds like the unindexed date is correlated with the indexed ID, as in the ID is an autonumber that is inserted at the time of insertion, and the unindexed date is the time of insertion. In that narrow case, then @MartinSmith’s comment will hold true; your results may vary if the unindexed date column and the indexed column have no correlation.

    Without looking at the execution plans, I’d bet that you’re seeing two scans on the table to retrieve the ID’s (a very narrow and small data set), and then an index seek to return the actual rows. It’ll work as long as the assumed relationship holds true. However, if someone ever updates as date column (and breaks the relationship between date and id), your results will no longer be accurate because the MIN or MAX id values may no longer be in the range.

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

Sidebar

Related Questions

Database has tables Photos and PhotoAlbums. I need a query that will select all
Database Table1 Id Table2Id ... Table2 Id StartTime Duration //in hours Query select *
I'm not much of a database guru so I need some help on a
Hello guru of programming! Today I have got the task from my manager to
We have a query that is currently killing our database and I know there
I'm not much of a database guru so I would like some advice. Background
Database Scheme : http://pastebin.com/aPXk1rMf I'm want to select post owner's uName and comment owner's
I have a database which has an orders table and inventory table. The order-items
Database software: SQLITE Consider this scenario: Table X has attributes: A,B,C,D,1,2,3,4 Table Y needs
Database entries table test name ======================== Sunae Kasimov Obsop Natty Preem Kuman Mysql query

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.