Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy would UPDATE with JOIN queries be so much slower than SELECT with JOIN queries?
    primarykey
    data
    text
    <p>This has been a 19-hour long nightmare.</p> <p>I have a very large query that in essence needs to join large datasets across a couple tables. After I make the join, I need to update the original table with the data from the select statement. The SELECT statement is super fast, and the UPDATE statement is super slow.</p> <p>This is the select statement.</p> <pre><code>SELECT l.col1, l.col2, l.col3, p.personid FROM table1 p LEFT JOIN table2 l ON (l.col1 = p.col1) LEFT JOIN (SELECT name, col AS 'col2' FROM tbl3 f WHERE f.col LIKE '%-F') pcf ON (pcf.col1 = p.col1) LEFT JOIN (SELECT name, col AS 'col3' FROM tbl4 f WHERE f.col LIKE '%-M') pcm ON (pcm.col1 = p.col1) WHERE p.requestid = '1928' </code></pre> <p>Now, if I take the EXACT SAME series of JOINs and put it inside an UPDATE context, the query takes forever.</p> <pre><code>UPDATE table1 p LEFT JOIN table2 l ON (l.col1 = p.col1) LEFT JOIN (SELECT name, col AS 'col2' FROM tbl3 f WHERE f.col LIKE '%-F') pcf ON (pcf.col1 = p.col1) LEFT JOIN (SELECT name, col AS 'col3' FROM tbl4 f WHERE f.col LIKE '%-M') pcm ON (pcm.col1 = p.col1) SET p.col1 = l.col1, p.col2 = l.col2, p.col3 = l.col3 WHERE p.requestid = '1928' </code></pre> <p>So...why would the UPDATE JOIN statement take so much longer than the SELECT JOIN statement? Oodles of time longer. And I already tried temporary tables and it didn't work.</p> <p>FYI, I'm working with tables of 50k records or more.</p> <p>If you're curious about the EXPLAIN results, this is what happens when I EXPLAIN the select query (although apparently you can't use explain for update?)</p> <pre><code>id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY p ALL NULL NULL NULL NULL 613246 Using where 1 PRIMARY l eq_ref PRIMARY,name_3,name,name_2 PRIMARY 257 drilldev_db.p.lastname 1 1 PRIMARY &lt;derived2&gt; ALL NULL NULL NULL NULL 23435 1 PRIMARY &lt;derived3&gt; ALL NULL NULL NULL NULL 13610 1 PRIMARY &lt;derived4&gt; ALL NULL NULL NULL NULL 13053 1 PRIMARY &lt;derived5&gt; ALL NULL NULL NULL NULL 8273 1 PRIMARY &lt;derived6&gt; ALL NULL NULL NULL NULL 11481 1 PRIMARY &lt;derived7&gt; ALL NULL NULL NULL NULL 6708 1 PRIMARY &lt;derived8&gt; ALL NULL NULL NULL NULL 9588 1 PRIMARY &lt;derived9&gt; ALL NULL NULL NULL NULL 5494 1 PRIMARY &lt;derived10&gt; ALL NULL NULL NULL NULL 6981 1 PRIMARY &lt;derived11&gt; ALL NULL NULL NULL NULL 4107 1 PRIMARY &lt;derived12&gt; ALL NULL NULL NULL NULL 5973 1 PRIMARY &lt;derived13&gt; ALL NULL NULL NULL NULL 3851 1 PRIMARY &lt;derived14&gt; ALL NULL NULL NULL NULL 4935 1 PRIMARY &lt;derived15&gt; ALL NULL NULL NULL NULL 3574 1 PRIMARY &lt;derived16&gt; ALL NULL NULL NULL NULL 5793 1 PRIMARY &lt;derived17&gt; ALL NULL NULL NULL NULL 4706 17 DERIVED f ref year,gender gender 257 364263 Using where; Using temporary; Using filesort 16 DERIVED f ref year,gender gender 257 397322 Using where; Using temporary; Using filesort 15 DERIVED f range year,gender year 4 NULL 54092 Using where; Using temporary; Using filesort 14 DERIVED f range year,gender year 4 NULL 54092 Using where; Using temporary; Using filesort 13 DERIVED f range year,gender year 4 NULL 62494 Using where; Using temporary; Using filesort 12 DERIVED f range year,gender year 4 NULL 62494 Using where; Using temporary; Using filesort 11 DERIVED f range year,gender year 4 NULL 69317 Using where; Using temporary; Using filesort 10 DERIVED f range year,gender year 4 NULL 69317 Using where; Using temporary; Using filesort 9 DERIVED f ref year,gender gender 257 364263 Using where; Using temporary; Using filesort 8 DERIVED f range year,gender year 4 NULL 94949 Using where; Using temporary; Using filesort 7 DERIVED f ref year,gender gender 257 364263 Using where; Using temporary; Using filesort 6 DERIVED f ref year,gender gender 257 397322 Using where; Using temporary; Using filesort 5 DERIVED f ref year,gender gender 257 364263 Using where; Using temporary; Using filesort 4 DERIVED f ref year,gender gender 257 397322 Using where; Using temporary; Using filesort 3 DERIVED f ALL NULL NULL NULL NULL 37045 Using where 2 DERIVED f ALL NULL NULL NULL NULL 37045 Using where </code></pre> <p>Thanks!</p> <p>-b</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.
    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