Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yet another approach is to slightly denormalise your design to better support your query.</p> <p>To do this, create a new table "role":</p> <pre><code>create table role ( user_id int not null, role enum ('writer', 'editor', 'publisher' ) not null, primary key (user_id, role ) ); </code></pre> <p>This would be updated whenever a new row is added to one of your link tables that includes a user_id:</p> <pre><code>insert ignore into role values( $user_id, $role ); </code></pre> <p>After a while, it is likely that the role entry will already exist, hence the "ignore" modifier.</p> <p>The table could be boot-strapped from your existing tables:</p> <pre><code>insert ignore into role select distinct user_id, 'writer' from link_writers; insert ignore into role select distinct user_id, 'editor' from link_editors; insert ignore into role select distinct user_id, 'publisher' from link_publishers; </code></pre> <p>Your search query then becomes a set of simple JOINS which MySQL should have no problem optimising:</p> <pre><code>SELECT r.user_id, r.role, case r.role when 'writer' then w.publication_id when 'editor' then e.publication_id when 'publisher' then pg.publication_id end as publication_id FROM ( (SELECT user_id FROM users WHERE firstname LIKE '%Jenkz%') UNION (SELECT user_id FROM users WHERE lastname LIKE '%Jenkz%') ) AS dt JOIN role r on r.user_id = dt.user_id LEFT JOIN link_writers w on r.user_id = w.user_id and r.role = 'writer' LEFT JOIN link_editors e on r.user_id = e.user_id and r.role = 'editor' LEFT JOIN link_publishers p on r.user_id = p.user_id and r.role = 'publisher' LEFT JOIN link_publisher_groups pg on p.publisher_group_id = pg.publisher_group_id; </code></pre> <p>This will give a very "wide" answer.</p>
    singulars
    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.
    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