[Maria-discuss] Finicky syntax with RETURNING?
Hello All, I'm having a bit of an issue with the RETURNING feature of DELETE<https://mariadb.com/kb/en/mariadb/delete/>. This was introduced in MariaDB 10.0.5. Specifically there seems to be an issue when using aliases with the RETURNING keyword. It involves a query like this. A user came to me with queries of this form that were running slowly. Here is the explain plan... EXPLAIN DELETE from t1 where id in (select id from t1 where acc_id = 9999) \G *************************** 1. row *************************** id: 1 select_type: PRIMARY table: t1 type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 4675 Extra: Using where *************************** 2. row *************************** id: 2 select_type: DEPENDENT SUBQUERY table: t2 type: unique_subquery possible_keys: XXXXXXXXXX key: PRIMARY key_len: 4 ref: func rows: 1 Extra: Using where 2 rows in set (0.00 sec) You can see it's being forced to run a table scan and run the subquery for each row. This was fixed by properly aliasing the query... EXPLAIN EXTENDED DELETE uc from t1 AS uc where uc. id in (select p. id from t2 p where p.acc_id = 9999) \G *************************** 1. row *************************** id: 1 select_type: PRIMARY table: p type: ref possible_keys: XXXXXXXXXXXXXXXXXXXXXXXX key: uk_XXXXXXXXXXXXXXXXXXXX key_len: 4 ref: const rows: 1 filtered: 100.00 Extra: Using index *************************** 2. row *************************** id: 1 select_type: PRIMARY table: uc type: ref possible_keys: XXXXXXXXXXXXXXXXXXX key: XXXXXXXXXXXXXX key_len: 4 ref: XXXXXXXXXXXXXX rows: 1 filtered: 100.00 Extra: 2 rows in set (0.00 sec) There now seems to be an issue when attempting to use the RETURNING keyword with this aliases version of the query. I have tried numerous versions of this query... EXPLAIN DELETE uc FROM t1 AS uc WHERE uc.id IN (SELECT p.id FROM t2 AS p WHERE p.account_id = 9999) RETURNING uc.some_id; EXPLAIN DELETE uc FROM t1 uc INNER JOIN t2 p ON uc.id = p.id WHERE p.acc_id = 9999 RETURNING some_id; All of these produce a SQL syntax error. I've tried a range of ways of writing this, using different joins, subqueries, with and without the AS keyword. The issues seems to be with aliases. The only version I can seem to get to work is this (note the removal of the aliases)... EXPLAIN DELETE FROM t1 WHERE id IN (SELECT id FROM t2 WHERE acc_id = 9999) RETURNING some_id; But then that creates the slow table scan problem again. Anyone have any experience of this? Cheers, Rhys
Hi, Rhys.Campbell! On Jul 25, Rhys.Campbell@swisscom.com wrote:
Hello All,
I'm having a bit of an issue with the RETURNING feature of DELETE<https://mariadb.com/kb/en/mariadb/delete/>. This was introduced in MariaDB 10.0.5. Specifically there seems to be an issue when using aliases with the RETURNING keyword.
It involves a query like this. A user came to me with queries of this form that were running slowly. Here is the explain plan...
EXPLAIN DELETE from t1 where id in (select id from t1 where acc_id = 9999) \G ... You can see it's being forced to run a table scan and run the subquery for each row. This was fixed by properly aliasing the query...
EXPLAIN EXTENDED DELETE uc from t1 AS uc where uc. id in (select p. id from t2 p where p.acc_id = 9999) \G ...
The first query is, internally "single-table DELETE", the second is "multi-table DELETE". I've reported different plans as a bug https://jira.mariadb.org/browse/MDEV-10447 DELETE ... RETURNING does not support multi-table delete, it's documented here: https://mariadb.com/kb/en/mariadb/delete/ I've created a task to fix it. https://jira.mariadb.org/browse/MDEV-10448 Regards, Sergei Chief Architect MariaDB and security@mariadb.org
Hi Sergei, Thanks for your response. Shouldn't this work? EXPLAIN DELETE uc FROM t1 AS uc WHERE uc.id IN (SELECT p.id FROM t2 AS p WHERE p.account_id = 9999) RETURNING uc.some_id; Or does that count in the definition of a multi-table delete? (I thought not). Cheers, Rhys -----Original Message----- From: Sergei Golubchik [mailto:serg@mariadb.org] Sent: Tuesday, July 26, 2016 5:38 PM To: Campbell Rhys, INI-ON-FIT-TSO-QPM <Rhys.Campbell@swisscom.com> Cc: maria-discuss@lists.launchpad.net Subject: Re: [Maria-discuss] Finicky syntax with RETURNING? Hi, Rhys.Campbell! On Jul 25, Rhys.Campbell@swisscom.com wrote:
Hello All,
I'm having a bit of an issue with the RETURNING feature of DELETE<https://mariadb.com/kb/en/mariadb/delete/>. This was introduced in MariaDB 10.0.5. Specifically there seems to be an issue when using aliases with the RETURNING keyword.
It involves a query like this. A user came to me with queries of this form that were running slowly. Here is the explain plan...
EXPLAIN DELETE from t1 where id in (select id from t1 where acc_id = 9999) \G ... You can see it's being forced to run a table scan and run the subquery for each row. This was fixed by properly aliasing the query...
EXPLAIN EXTENDED DELETE uc from t1 AS uc where uc. id in (select p. id from t2 p where p.acc_id = 9999) \G ...
The first query is, internally "single-table DELETE", the second is "multi-table DELETE". I've reported different plans as a bug https://jira.mariadb.org/browse/MDEV-10447 DELETE ... RETURNING does not support multi-table delete, it's documented here: https://mariadb.com/kb/en/mariadb/delete/ I've created a task to fix it. https://jira.mariadb.org/browse/MDEV-10448 Regards, Sergei Chief Architect MariaDB and security@mariadb.org
Hi, Rhys.Campbell! On Jul 27, Rhys.Campbell@swisscom.com wrote:
Hi Sergei,
Thanks for your response. Shouldn't this work?
EXPLAIN DELETE uc FROM t1 AS uc WHERE uc.id IN (SELECT p.id FROM t2 AS p WHERE p.account_id = 9999) RETURNING uc.some_id;
Or does that count in the definition of a multi-table delete? (I thought not).
It does. See the manual: https://mariadb.com/kb/en/mariadb/delete/ Single table delete: DELETE FROM ... Multi-table detele: DELETE tables FROM Regards, Sergei Chief Architect MariaDB and security@mariadb.org
participants (2)
-
Rhys.Campbell@swisscom.com
-
Sergei Golubchik