Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My initial idea was to create a temporary table to hold (and index) the user_id that match the name, and use that to join against each link table. Unfortunately, in MySQL a temporary table can only be joined against ONCE in a query.</p> <p>The nasty workaround is to create a permanent table, adding the connection_id to the primary key so separate sessions do not get confused.</p> <pre><code>create table tt ( connection_id int not null, user_id int not null, firstname varchar(10) not null, lastname varchar(10) not null, primary key( connection_id, user_id ) ); </code></pre> <p>The following sequence would be repeated each time you need an answer:</p> <pre><code>delete from tt where connection_id = connection_id(); insert into tt SELECT connection_id(), user_id, firstname, lastname FROM users WHERE firstname LIKE '%Jenkz%' UNION SELECT connection_id(), user_id, firstname, lastname FROM users WHERE lastname LIKE '%Jenkz%'; </code></pre> <p>Next, your existing UNION is extended so that only the relevant user_id is pulled out:</p> <pre><code>SELECT 'writer' AS role, link_writers.user_id, link_writers.publication_id FROM link_writers JOIN tt ON tt.connection_id = connection_id() and tt.user_id = link_writers.user_id UNION SELECT 'editor' AS role, link_editors.user_id, link_editors.publication_id FROM link_editors JOIN tt ON tt.connection_id = connection_id() and tt.user_id = link_editors.user_id UNION SELECT 'publisher' AS role, link_publishers.user_id, link_publisher_groups.publication_id FROM link_publishers JOIN link_publisher_groups ON link_publisher_groups.publisher_group_id = link_publishers.publisher_group_id JOIN tt ON tt.connection_id = connection_id() and tt.user_id = link_publishers.user_id </code></pre> <p>Perhaps this will be an improvement because not every row of all the link tables is being pulled into the union.</p> <p>The EXPLAIN is a bit peculiar in that only 4 bytes of the index on tt is being used - where I would have expected all 8 bytes. Perhaps this is because I have so little data in tt.</p> <pre><code>*************************** 1. row *************************** id: 1 select_type: PRIMARY table: tt type: ref possible_keys: PRIMARY key: PRIMARY key_len: 4 ref: const rows: 1 Extra: Using index *************************** 2. row *************************** id: 1 select_type: PRIMARY table: link_writers type: ref possible_keys: PRIMARY key: PRIMARY key_len: 4 ref: test.tt.user_id rows: 1 Extra: Using index *************************** 3. row *************************** id: 2 select_type: UNION table: tt type: ref possible_keys: PRIMARY key: PRIMARY key_len: 4 ref: const rows: 1 Extra: Using index *************************** 4. row *************************** id: 2 select_type: UNION table: link_editors type: ref possible_keys: PRIMARY key: PRIMARY key_len: 4 ref: test.tt.user_id rows: 1 Extra: Using index *************************** 5. row *************************** id: 3 select_type: UNION table: tt type: ref possible_keys: PRIMARY key: PRIMARY key_len: 4 ref: const rows: 1 Extra: Using index *************************** 6. row *************************** id: 3 select_type: UNION table: link_publishers type: ref possible_keys: PRIMARY key: PRIMARY key_len: 4 ref: test.tt.user_id rows: 1 Extra: Using index *************************** 7. row *************************** id: 3 select_type: UNION table: link_publisher_groups type: ref possible_keys: PRIMARY key: PRIMARY key_len: 4 ref: test.link_publishers.publisher_group_id rows: 2 Extra: Using index *************************** 8. row *************************** id: NULL select_type: UNION RESULT table: &lt;union1,2,3&gt; type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: NULL Extra: 8 rows in set (0.00 sec) </code></pre>
    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.
    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