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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:32:56+00:00 2026-06-17T14:32:56+00:00

Let’s say I have the following table: Table: RelationshipType ============================================================ | ID (PK) |

  • 0

Let’s say I have the following table:

Table: RelationshipType
============================================================
|  ID (PK)  |  ParentID  |  ChildID  |  RelationshipType   |
============================================================

There are mostly cases where ParentID and ChildID are selected on individually:

... WHERE ParentID = @SomeID

... WHERE ChildID  = @SomeID

And sometimes both are selected on:

... WHERE ParentID = @SomeID AND ChildID  = @SomeOtherID

I want to increase the performance of these queries but most notably the first two. Should I create a non-clustered index on ParentID + ChildID together or one index on ParentID and another index on ChildID?

EDIT: All of these queries are highly selective (1 or 2 records returned).

  • 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-17T14:32:57+00:00Added an answer on June 17, 2026 at 2:32 pm

    Can you get rid of the surrogate key ID?

    If yes, consider creating the following:

    • A primary and clustering key on {ParentID, ChildID}.
    • A secondary index on {ChildID, ParentID}, but include the RelationshipType in the index as well (use the INCLUDE keyword).

    This way, you have a covering index in all 3 cases, so you don’t have to pay the price of the double-lookup (that is normally required for secondary indexes in clustered tables):

    • ... WHERE ParentID = @SomeID can be satisfied by a simple seek in the B-Tree of the index: {ParentID, ChildID}. The value of ChildID and RelationshipType1 can be retrieved directly from the found leaf of this B-Tree.
    • ... WHERE ChildID = @SomeID can be satisfied by a simple seek in the B-Tree of the index: {ChildID, ParentID}. The value of ParentID and RelationshipType2 can be retrieved directly from the found leaf of this B-Tree.
    • ... WHERE ParentID = @SomeID AND ChildID = @SomeOtherID can be satisfied by either.

    1 The clustering key is the “main” B-Tree for the table and includes all columns, not just those that are unique.

    2 Thanks to INCLUDE (RelationshipType).


    Doing something similar with the ID present is possible, but would require 3 indexes instead of 2 and all of them would be fatter to achieve covering. You’d have to measure to make sure, but my feeling is that this would be more trouble than it’s worth.

    Otherwise, don’t use clustering at all. Just create normal indexes on:

    • {ID} – regular, non-clustering primary index (use the NONCLUSTERED keyword).
    • {ParentID} – regular secondary index.
    • {ChildID} – regular secondary index.

    You’ll have a normal heap table, so each access will require an index seek + (usually) table heap access, but your indexes will be kept slim, raising the cache effectiveness.

    ... WHERE ParentID = @SomeID AND ChildID = @SomeOtherID would require two index seeks (or possibly a seek on either {ParentID} or {ChildID} index + table heap access), but this is still pretty fast and is not too frequent (as you stated).


    Please do measure on realistic amounts of data before deciding either way.

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

Sidebar

Related Questions

Let's say I have a table with a Color column. Color can have various
Let's say for a moment that I have the following module in python: class
let's say I have the following string: string s = A B C D
Let's say I have the following entity: public class Store { public List<Product> Products
Let's say I have an abstract parent class called shape, and that there are
let's say.. I have the following java bean. Case1: (Student Bean) Integer id; String
Let's say I have a domain object with the following field: private Map<StatType, Double>
Let's say I have such MYSQL table for logins: id---name---location---login_date ---------------------------------- 1----mike----usa-----21.08.2012 2----tony----uk------22.08.2012 3----tony----uk------23.08.2012
Let's say I have the following two lists of tuples myList = [(1, 7),
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress

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.