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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:13:21+00:00 2026-06-14T20:13:21+00:00

For example, we have table A, and table B which have a many-to-many relationship.

  • 0

For example, we have table A, and table B which have a many-to-many relationship. An intersection table, Table C stores A.id and B.id along with a value that represents a relationship between the two. Or as a concrete example, imagine stackexchange which has a user account, a forum, and a karma score. Or, a student, a course, and a grade. If table A and B are very large, table C can and probably will grow monstrously large very quickly(in fact lets just assume it does). How do we go about dealing with such an issue? Is there a better way to design the tables to avoid 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-14T20:13:22+00:00Added an answer on June 14, 2026 at 8:13 pm

    There is no magic. If some rows are connected and some aren’t, this information has to be represented somehow, and the “relational” way of doing it is a “junction” (aka “link”) table. Yes, a junction table can grow large, but fortunately databases are very capable of handling huge amounts of data.

    There are good reasons for using junction table versus comma-separated list (or similar), including:

    • Efficient querying (through indexing and clustering).
    • Enforcement of referential integrity.

    When designing a junction table, ask the following questions:

    1. Do I need to query in only one direction or both?1
      • If one direction, just create a composite PRIMARY KEY on both foreign keys (let’s call them PARENT_ID and CHILD_ID). Order matters: if you query from parent to children, PK should be: {PARENT_ID, CHILD_ID}.
      • If both directions, also create a composite index in the opposite order, which is {CHILD_ID, PARENT_ID} in this case.
    2. Is the “extra” data small?
      • If yes, cluster the table and cover the extra data in the secondary index as necessary.2
      • I no, don’t cluster the table and don’t cover the extra data in the secondary index.3
    3. Are there any additional tables for which the junction table acts as a parent?
      • If yes, consider whether adding a surrogate key might be worthwhile to keep child FKs slim. But beware that if you add a surrogate key, this will probably eliminate the opportunity for clustering.

    In many cases, answers to these questions will be: both, yes and no, in which case your table will look similar to this (Oracle syntax below):

    CREATE TABLE JUNCTION_TABLE (
        PARENT_ID INT,
        CHILD_ID INT,
        EXTRA_DATA VARCHAR2(50),
        PRIMARY KEY (PARENT_ID, CHILD_ID),
        FOREIGN KEY (PARENT_ID) REFERENCES PARENT_TABLE (PARENT_ID),
        FOREIGN KEY (CHILD_ID) REFERENCES CHILD_TABLE (CHILD_ID)
    ) ORGANIZATION INDEX COMPRESS;
    
    CREATE UNIQUE INDEX JUNCTION_TABLE_IE1 ON
        JUNCTION_TABLE (CHILD_ID, PARENT_ID, EXTRA_DATA) COMPRESS;
    

    Considerations:

    • ORGANIZATION INDEX: Oracle-specific syntax for what most DBMSes call clustering. Other DBMSes have their own syntax and some (MySQL/InnoDB) imply clustering and user cannot turn it off.
    • COMPRESS: Some DBMSes support leading-edge index compression. Since clustered table is essentially an index, compression can be applied to it as well.
    • JUNCTION_TABLE_IE1, EXTRA_DATA: Since extra data is covered by the secondary index, DBMS can get it without touching the table when querying in the direction from child to parents. Primary key acts as a clustering key so the extra data is naturally covered when querying from a parent to the children.

    Physically, you have just two B-Trees (one is the clustered table and the other is the secondary index) and no table heap at all. This translates to good querying performance (both parent-to-child and child-to-parent directions can be satisfied by a simple index range scan) and fairly small overhead when inserting/deleting rows.

    Here is the equivalent MS SQL Server syntax (sans index compression):

    CREATE TABLE JUNCTION_TABLE (
        PARENT_ID INT,
        CHILD_ID INT,
        EXTRA_DATA VARCHAR(50),
        PRIMARY KEY (PARENT_ID, CHILD_ID),
        FOREIGN KEY (PARENT_ID) REFERENCES PARENT_TABLE (PARENT_ID),
        FOREIGN KEY (CHILD_ID) REFERENCES CHILD_TABLE (CHILD_ID)
    );
    
    CREATE UNIQUE INDEX JUNCTION_TABLE_IE1 ON
        JUNCTION_TABLE (CHILD_ID, PARENT_ID) INCLUDE (EXTRA_DATA);
    

    Note that MS SQL Server automatically clusters tables, unless PRIMARY KEY NONCLUSTERED is specified.


    1 In other words, do you only need to get “children” of given “parent”, or you might also need to get parents of given child.

    2 Covering allows the query to be satisfied from the index alone, and avoids expensive double-lookup that would otherwise be necessary when accessing data through a secondary index in the clustered table.

    3 This way, the extra data is not repeated (which would be expensive, since it’s big), yet you avoid the double-lookup and replace it with (cheaper) table heap access. But, beware of clustering factor that can destroy the performance of range scans in heap-based tables!

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

Sidebar

Related Questions

I have an intermediate table that defines many-to-many relationship between, for instance, Customer and
I have a table that contains common entries in two columns. For Example: Column1
I have two models Property and Amenity the relationship between them is many to
I have a table which needs a relationship over itself. Table Example: field_PK --->
I have three tables: posts, tags and posts_has_tags (which facilitate the many-to-many relationship between
I have a table which defines what things another table can have, for example:
For example I have a table MyTable like this: Id Value Rate 1 10
I have a table, has many rows. for example one of from rows(all rows
I have a table, has many rows. for example one of from rows(all rows
I have a many-to-many relationship were the link table has an additional field. Hence

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.