join - MySQL: many to many how to get all (related) categories & combine queries -



join - MySQL: many to many how to get all (related) categories & combine queries -

the table:

id | category (there index on id & category) ----------- 1 | 1 1 | 7 1 | 3 2 | 1 2 | 2 2 | 4 3 | 1 3 | 6 3 | 3 select distinct category many_to_many e1 id in ( select distinct e1.id many_to_many e1 inner bring together many_to_many x1 on e1.id=x1.id e1.category in (3) )

i retured: ** 6, 1, 7** (what query above) seems me query not gone preform well, because sub query searches id's , list can huge.

also doesn't matter how many if id related. performance if there 100 id's checking 1 time each unique category populated enough.

secondly utilize other query (the sub query )the id's contain category:

select distinct e1.id many_to_many e1 inner bring together many_to_many x1 on e1.id=x1.id e1.category in (3) returns: 3 & 1

what efficient way query result like. there more efficient (better preforming) solution? should utilize 1 instead of 2 query's?

select distinct x1.category many_to_many e1 inner bring together many_to_many x1 on e1.id = x1.id e1.category = 3 --- in (3) , x1.category <> 3 --- not in (3)

you shouldl check plan of query explain. compound index on (category, id) might query.

mysql join many-to-many

Comments

Popular posts from this blog

delphi - blogger via idHTTP : error 400 bad request -

c++ - compiler errors when initializing EXPECT_CALL with function which has program_options::variables_map as parameter -

How do I check if an insert was successful with MySQLdb in Python? -