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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:59:37+00:00 2026-06-08T07:59:37+00:00

I have a huge table in my database that contains distances between cities. This

  • 0

I have a huge table in my database that contains distances between cities. This enables my application to find nearby cities around the world when a starting city is selected.

It contains 4 columns:

ID, StartCityID, EndCityID, Distance 

and contains about 120 million rows.

I’ve got indexes set up on the startcityID, endcityID, another one for both, and another one each for startcity + distance, and endcity + distance (this is my first real dealings with indexes so not 100% sure if I’m doing it correctly).

Anyway – I do the following 2 queries:

Select distinct StartCityID
From Distances where EndCityID = 23485

and

Select distinct EndCityID 
From Distances where StartCityID = 20045

They both return the same number of cityID‘s, but the top one takes 35 seconds to do, and the bottom one returns results immediately. When I look at the indexes, they seem to be set up to serve startCity and endCity in the same way.

Anyone know why they might be acting differently? I’m at a loss…

NB – this may offer more insight, but the one that takes 35 seconds – if I press execute again straight away with the same ID, it returns results immediately as well that time.

Unfortunately that isn’t good enough for my website but it may be useful information.

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-06-08T07:59:38+00:00Added an answer on June 8, 2026 at 7:59 am

    The second one is covering index and thus fast because you have index on startcity and endcity.

    The index on endcity is not covering (as it doesnt have startcity) and thus either it has to join with other indexes to get the data or has to do key lookup and thus takes time.Also, it has to do hash distinct or distinct using sor whereas first one doesnt need to do that as well as data is sorted in endcity order for a given startcity.Also why use distinct will you have duplicate data for startcity and endcity.If no dup data remove distinct.

    Check then plan for these first one should be index seek on endcity + distnace index and then most probably key lookup it could be clustred index scan as well based on the selectivity of the endcity.Then a hash distinct or sort distinct .

    Second one should have just the index seek on index startcity + endcity.

    You have mentioned that second time it returned immediately that is because data was already in cache. Thus try following

    dbcc dropcleanbuffers
    dbcc freeproccache
    and then run the second query first..

    CAUTION : Do not use these on PROD server and other cirtical servers.Try this on a machine where it wont impact other users.

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

Sidebar

Related Questions

I have an MEMORY MYSQL database that contains a table with 200k+ rows. I
I have a huge table in a database and I want to split that
I have a huge Database table containing around 5 Million rows. Now retrieving records
I have a database containing a single huge table. At the moment a query
I have a huge table that is mainly used for backup and administrative purposes.
I have a huge data.table in R which contains the result of a experiment:
I have a huge access mdb file which contains a single table with 20-30
I have a scripts that retrieves huge data on a table. I want to
I have a huge database that has several tables that hold several million records.
I have a huge database that has lots and lots of poorly documented constraints.

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.