i'm just wondering what would be the best/most performant way to store (and later select) relations in MySQL.
Just imagine you have a table "relations", Object A of type "Window" with ID "123" and Object B of type "Frame" with ID "456" and maybe some type of clarification, what type of relation it is
so the simplest way would be to have a table with: obj_a_type, obj_a_id, obj_b_type, obj_b_id, someinfo
Having indexes on (obj_a_type, obj_a_id) and (obj_b_type, obj_b_id) would help, but with a growing table i think it will become slow
an example query would be:
SELECT ... FROM relations WHERE obj_a_type='Window' AND obj_a_id=123 OR obj_b_type='Window' AND obj_b_id=123;
This Would at least give all possible relations with the Window-Object with id 123.
Another aproach could be, to map the object-types to a set of numbers and attach them to the id. For example i expect to have a maximum of 20 different object types, i would choose a 2-digit number. I will now map the window-object to 2. now i could reduce the table to the columns "obj_a, obj_b, someinfo" and the index SHOULD be more eficient, because he just has to read 2 indexes instead of 4:
SELECT ... FROm relations WHERE obj_a=CONCAT('123' /* the id */, '02' /* Window */) OR obj_b='12302';
(the CONCAT should just show how it is being connected) This aproach should be much more performant, but it is also much more complicated to read, because you have to know, that the last 2 digits represent the type AND you should know which number stands for which type. Of course, a little helper-table that stores the possible types would be nice.
But the question now that you (hopefully unterstand what i want to do): Do you know better ways to store such data that will allow me to store relations AND will be performant even if there will be millions of records?
Also as you can see in the queries, there shouldn't be any special "direction", so the given object could be object_a or object_b.
thanks for your help!
EDIT: The reason why i have different object-types is, that they are really different, so there is no "unique object_id", thare can be a Window-Object with id 123 AND there can be a Frame-Object with id 123 at the same time
another example: i have a table persons with person_id, i have a table products with product_id and a table appointments with appointment_id they could all have relationships (a person with an appointment and vice versa, a product with an appointment, or a product related to a person, whatever)