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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:11:09+00:00 2026-06-08T01:11:09+00:00

I hold a set of nodes in one mysql table1 and a table of

  • 0

I hold a set of nodes in one mysql table1 and a table of edges in another one (table2). Nodes come with primary keys and edges use this “foreign key”

**table1**
id label
1  node1
2  node2
3  node3

**table2**
FK_first FK_sec   rel
1        3        guardian
2        1        guardian
1        3        times

I know the db-design is not perfect, but its simple…
Now i want the number of ‘rel’ for every node and do a query like:

SELECT 
  label, 
  COUNT( rel ) as freq
FROM
  `table1` 
  LEFT JOIN table2 ON (id=FK_first OR id=FK_second) 
GROUP BY label
ORDER BY freq DESC

I have about 1000 nodes and 2000 edges. A query with ON (id=FK_first OR id=FK_second), then the query is way faster (<1 sec). The other query needs about 6 sec which is ver slow.
I would appreciate some comments to speed this up a bit 🙂

  1. LEFT JOIN table2 ON (id=FK_first OR id=FK_second) ~6 sec
  2. LEFT JOIN table2 ON (id=FK_first) ~0.16 sec
  3. LEFT JOIN table2 ON (id=FK_second) ~0.16 sec

  4. LEFT JOIN table2 ON id IN (FK_first,FK_second) ~6 sec


EXPLAIN 1:
        id  select_type table   type    possible_keys                   key key_len ref rows    Extra
        1   SIMPLE          table1  ALL NULL                            NULL    NULL    NULL    2571    Using temporary; Using filesort
        1   SIMPLE          table2  ALL FK_first,FK_second,FK_first_2   NULL    NULL    NULL    3858    

EXPLAIN 2:
id  select_type  table  type   possible_keys       key    key_len ref        rows   Extra
1   SIMPLE       table1 index  NULL                PRIMARY    2   NULL       2571   Using index; Using temporary; Using filesort
1   SIMPLE       table2 ref   FK_first,FK_first_2  FK_first_2 4   table1.id  1
  • 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-08T01:11:10+00:00Added an answer on June 8, 2026 at 1:11 am

    Try doing two joins and moving the “OR” into the COUNT() function:

    For every row, this joins table2 once on FK1, then again on FK2 (if it is not already joined to that row via FK1. Then in the COUNT, we specify that only rows which have either join’s rel column as non-null.

    SELECT 
      label, 
      COUNT( table2A.rel || table2B.rel ) as freq
    FROM
      `table1` 
      LEFT JOIN 
        table2 as table2A 
        ON id=table2A.FK_first
      LEFT JOIN 
        table2 as table2B 
        ON id=table2B.FK_second 
        AND  table2A.FKFirst != table2B.FKFirst
    GROUP BY label
    ORDER BY freq DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table set up to hold folders. Each folder has an ID
Can a body tag hold two set intervals? or have 2 functions use the
I'm trying to hold a set of ID's in an ArrayList I then want
I am using an std::set to hold members of type Board , and I
I've created a JavaScript object to hold onto a value set by a user
both of them hold 8 bytes, but how come the max value for double
I'm designing a namespace to hold a set of classes that will handle user
I can't seem to find this info anywhere? Do window.getSelection() or document.selection.createRange() hold a
Im trying to use std::bind to bind this to method that is used in
I have a set of classes which hold only data (no operations have been

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.