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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:16:29+00:00 2026-06-08T17:16:29+00:00

I have this following table: Matches -> match_id, team_a_id , team_b_id, score This table

  • 0

I have this following table:

Matches -> match_id, team_a_id , team_b_id, score

This table will record matches between two teams (team A and team B). However, sometimes team A play as the host and sometimes team B plays as the host. Therefore, when I tried to find history matches between team a and team b. What I currently I am doing is to

select * from matches where (team_a_id = 1 and team_b_id = 2) or (team_a_id = 2 and team_b_id = 1);

Is there any better approach to such case? As for the query above, am I right to include index of combination team_a_id and team_b_id? But even so, then I still have a Logical OR condition between AB OR BA.

Alternatively,
I have another idea, that is to have another table let say history

History -> team_hash, match_id

I manually build team_hash where hash(a,b) == hash(b,a). But this result in slightly slower insert but faster read. Or is it really faster read?

  • 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-08T17:16:30+00:00Added an answer on June 8, 2026 at 5:16 pm

    Assuming there is a composite index on {team_a_id, team_b_id}, the DBMS can execute your SQL statement using only two index seeks (one for the team_a_id = 1 and team_b_id = 2 and the other for the team_a_id = 2 and team_b_id = 1), which is very fast. I don’t expect you should find the performance lacking.


    However, there is a way to eliminate one of these index seeks. Add a constraint…

    CHECK(team_a_id < team_b_id)
    

    …and encode a “direction” (i.e. which team is host) in a separate field if necessary. This way, you know team_a_id = 2 and team_b_id = 1 can never be true, so you only need to search on team_a_id = 1 and team_b_id = 2.


    “Symmetrical” hashing is a neat idea, but:

    • The correctness of the hash cannot be enforced declaratively – you’ll need to do it through a trigger or at the application level.
    • It’s a redundant data. You’ll need to keep team_a_id and team_b_id anyway to resolve hashing conflicts. Larger data effectively means smaller cache.
    • It may actually increase the number of indexes – the efficient enforcement of the referential integrity will probably require indexes on team_a_id and team_b_id even if you don’t need them for the actual SQL query. In addition to putting more pressure on cache, every additional index must be maintained, potentially hurting the INSERT/UPDATE/DELETE performance. The situation is especially serious in InooDB where you cannot turn-off the clustering, so secondary indexes tend to be more expensive than in heap-based tables (see the “Disadvantages of clustering” in this article).
    • 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 following table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
I have the following tweets table: tweet_id user_id text --------------------------------------------------- 1 2 this is
I currently have the following row in my table: course_data: user_id days <-- This
I have the following function. $(function() { $(.sectionHeader:gt(0)).click(function() { $(this).next(.fieldset).slideToggle(fast); }); $(img[alt='minimize']).click(function(e) { $(this).closest(table).next(.fieldset).slideUp(fast);
I have the following table: scores: user_id | match_id | points 1 | 110
I have this following codes: @implementation MyImageView @synthesize image; //image is a UIImage -
i have this following html structure usilg ul and li. <ul class=treeview id=productTree> <li
I have this following jquery text fly-in animation.Here is my code before I explain
I have this following query for CodeIgniter: $q = $this->db->where('(message_from='.$user_id.' AND message_to='.$this->auth_model->userdata['user_id'].')') ->or_where('(message_from='.$this->auth_model->userdata['user_id'].' AND
I have this following IEnumerable LINQ query: var query = from p in Enumerable.Range(2,

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.