Note that there are some explanatory texts on larger screens.

plurals
  1. POCan Someone Help me Optimize this mysql statement?
    primarykey
    data
    text
    <p>I have one table that I'm using to build groups with in my database. The table contains a list of group names and ids. I have another table that has users, and a third table showing the relationships. (userid, groupid).</p> <p>The situation is this, I need to create a list of userids that belong to a specific subset of groups. So for instance, I want all users that are in group 1, 3, and 8. That is straight forward enough. It gets more complicated though, I may need a list of all users that are in groups 1, 3, and 8, or 1, 2, and 8. Then I might need to exclude users that fit that criteria, but are also in group 27.</p> <p>So I've got a script dynamically creating a query, using sub queries that works to a point. I have two problems with it. I don't think I'm handling the not-in part properly, because as I ad criteria, eventually, it just kinda hangs. (I think this is a result of me using sub-selects instead of joins, but I could not figure out how to build this with joins.)</p> <p>Here is an example of a query with 4 ANDed OR groups, and 2 NOT clauses.</p> <p>Please let me know if there is a better way to optimize this stmt. (I can handle the dynamic building of it in PHP)</p> <p>If I need to clarify anything or provide more details, let me know.</p> <pre><code> select * from users_table where username IN ( select user_id from ( select distinct user_id from group_user_map where user_id in ( select user_id from ( select * from ( select count(*) as counter, user_id from ( ( select distinct(user_id) from group_user_map where group_id in (2601,119) ) union all ( select distinct(user_id) from group_user_map where group_id in (58,226) ) union all ( select distinct(user_id) from group_user_map where group_id in (1299,525) ) union all ( select distinct(user_id) from group_user_map where group_id in (2524,128) ) ) thegroups group by user_id ) getall where counter = 4 ) getuserids ) and user_id not in ( select user_id from group_user_map where group_id in (2572) ) ) biggergroup ); </code></pre> <p>Note, the first part of the query is comparing an id to a username. This is because I have the usernames stored as id's from the other table. (This whole thing is a link between two completely different databases).</p> <p>(Also, if it looks like I have any extra sub-queries, that was to try to force mysql to evaluate the inner queries first.)</p> <p>Thanks.</p> <p>Aaron.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload