diff --git a/data/test/vtexplain/deletesharded-queries.sql b/data/test/vtexplain/deletesharded-queries.sql index 5ec654b100..7876ef6225 100644 --- a/data/test/vtexplain/deletesharded-queries.sql +++ b/data/test/vtexplain/deletesharded-queries.sql @@ -1,4 +1,9 @@ -delete from user where id=10; +delete from music_extra where id=1; +delete from music_extra where id=1 and extra='abc'; + +/* with lookup index by pk */ +delete from user where id=1; +/* with lookup index by other field */ delete from user where name='billy'; /* not supported - multi-shard delete */ diff --git a/data/test/vtexplain/multi-output/deletesharded-output.json b/data/test/vtexplain/multi-output/deletesharded-output.json index 8d11932a3c..d8d6502f2f 100644 --- a/data/test/vtexplain/multi-output/deletesharded-output.json +++ b/data/test/vtexplain/multi-output/deletesharded-output.json @@ -1,6 +1,119 @@ [ { - "SQL": "delete from user where id=10", + "SQL": "delete from music_extra where id=1", + "Plans": [ + { + "Original": "delete from music_extra where id = :vtg1", + "Instructions": { + "Opcode": "DeleteEqual", + "Keyspace": { + "Name": "ks_sharded", + "Sharded": true + }, + "Query": "delete from music_extra where id = :vtg1", + "Vindex": "hash", + "Values": [ + ":vtg1" + ], + "Table": "music_extra" + }, + "ExecCount": 1, + "ExecTime": 0, + "ShardQueries": 1, + "Rows": 1, + "Errors": 0 + } + ], + "TabletActions": { + "ks_sharded/-40": { + "TabletQueries": [ + { + "Time": 1, + "SQL": "delete from music_extra where id = :vtg1 /* vtgate:: keyspace_id:166b40b44aba4bd6 */", + "BindVars": { + "#maxLimit": "10001", + "vtg1": "1" + } + } + ], + "MysqlQueries": [ + { + "Time": 1, + "SQL": "begin" + }, + { + "Time": 1, + "SQL": "delete from music_extra where id in (1) /* vtgate:: keyspace_id:166b40b44aba4bd6 */" + }, + { + "Time": 2, + "SQL": "commit" + } + ] + } + } + }, + { + "SQL": "delete from music_extra where id=1 and extra='abc'", + "Plans": [ + { + "Original": "delete from music_extra where id = :vtg1 and extra = :vtg2", + "Instructions": { + "Opcode": "DeleteEqual", + "Keyspace": { + "Name": "ks_sharded", + "Sharded": true + }, + "Query": "delete from music_extra where id = :vtg1 and extra = :vtg2", + "Vindex": "hash", + "Values": [ + ":vtg1" + ], + "Table": "music_extra" + }, + "ExecCount": 1, + "ExecTime": 0, + "ShardQueries": 1, + "Rows": 1, + "Errors": 0 + } + ], + "TabletActions": { + "ks_sharded/-40": { + "TabletQueries": [ + { + "Time": 1, + "SQL": "delete from music_extra where id = :vtg1 and extra = :vtg2 /* vtgate:: keyspace_id:166b40b44aba4bd6 */", + "BindVars": { + "#maxLimit": "10001", + "vtg1": "1", + "vtg2": "'abc'" + } + } + ], + "MysqlQueries": [ + { + "Time": 1, + "SQL": "begin" + }, + { + "Time": 1, + "SQL": "select id from music_extra where id = 1 and extra = 'abc' limit 10001 for update /* vtgate:: keyspace_id:166b40b44aba4bd6 */" + }, + { + "Time": 1, + "SQL": "delete from music_extra where id in (1) /* vtgate:: keyspace_id:166b40b44aba4bd6 */" + }, + { + "Time": 2, + "SQL": "commit" + } + ] + } + } + }, + { + "SQL": "delete from user where id=1", "Plans": [ { "Original": "delete from name_user_map where name = :name and user_id = :user_id", @@ -47,22 +160,22 @@ } ], "TabletActions": { - "ks_sharded/40-80": { + "ks_sharded/-40": { "TabletQueries": [ { "Time": 1, "SQL": "select name from user where id = :vtg1 for update", "BindVars": { "#maxLimit": "10001", - "vtg1": "10" + "vtg1": "1" } }, { "Time": 3, - "SQL": "delete from user where id = :vtg1 /* vtgate:: keyspace_id:594764e1a2b2d98e */", + "SQL": "delete from user where id = :vtg1 /* vtgate:: keyspace_id:166b40b44aba4bd6 */", "BindVars": { "#maxLimit": "10001", - "vtg1": "10" + "vtg1": "1" } } ], @@ -73,11 +186,11 @@ }, { "Time": 1, - "SQL": "select name from user where id = 10 limit 10001 for update" + "SQL": "select name from user where id = 1 limit 10001 for update" }, { "Time": 3, - "SQL": "delete from user where id in (10) /* vtgate:: keyspace_id:594764e1a2b2d98e */" + "SQL": "delete from user where id in (1) /* vtgate:: keyspace_id:166b40b44aba4bd6 */" }, { "Time": 4, @@ -93,7 +206,7 @@ "BindVars": { "#maxLimit": "10001", "name": "'name_val_1'", - "user_id": "10" + "user_id": "1" } } ], @@ -104,7 +217,7 @@ }, { "Time": 2, - "SQL": "delete from name_user_map where (name = 'name_val_1' and user_id = 10) /* vtgate:: keyspace_id:a6e89b54b129c33051b76db219595660 */" + "SQL": "delete from name_user_map where (name = 'name_val_1' and user_id = 1) /* vtgate:: keyspace_id:a6e89b54b129c33051b76db219595660 */" }, { "Time": 5, diff --git a/data/test/vtexplain/multi-output/deletesharded-output.txt b/data/test/vtexplain/multi-output/deletesharded-output.txt index f4fe8e7f0e..52b6d86231 100644 --- a/data/test/vtexplain/multi-output/deletesharded-output.txt +++ b/data/test/vtexplain/multi-output/deletesharded-output.txt @@ -1,12 +1,27 @@ ---------------------------------------------------------------------- -delete from user where id=10 +delete from music_extra where id=1 -1 ks_sharded/40-80: begin -1 ks_sharded/40-80: select name from user where id = 10 limit 10001 for update +1 ks_sharded/-40: begin +1 ks_sharded/-40: delete from music_extra where id in (1) /* vtgate:: keyspace_id:166b40b44aba4bd6 */ +2 ks_sharded/-40: commit + +---------------------------------------------------------------------- +delete from music_extra where id=1 and extra='abc' + +1 ks_sharded/-40: begin +1 ks_sharded/-40: select id from music_extra where id = 1 and extra = 'abc' limit 10001 for update /* vtgate:: keyspace_id:166b40b44aba4bd6 */ +1 ks_sharded/-40: delete from music_extra where id in (1) /* vtgate:: keyspace_id:166b40b44aba4bd6 */ +2 ks_sharded/-40: commit + +---------------------------------------------------------------------- +delete from user where id=1 + +1 ks_sharded/-40: begin +1 ks_sharded/-40: select name from user where id = 1 limit 10001 for update 2 ks_sharded/80-c0: begin -2 ks_sharded/80-c0: delete from name_user_map where (name = 'name_val_1' and user_id = 10) /* vtgate:: keyspace_id:a6e89b54b129c33051b76db219595660 */ -3 ks_sharded/40-80: delete from user where id in (10) /* vtgate:: keyspace_id:594764e1a2b2d98e */ -4 ks_sharded/40-80: commit +2 ks_sharded/80-c0: delete from name_user_map where (name = 'name_val_1' and user_id = 1) /* vtgate:: keyspace_id:a6e89b54b129c33051b76db219595660 */ +3 ks_sharded/-40: delete from user where id in (1) /* vtgate:: keyspace_id:166b40b44aba4bd6 */ +4 ks_sharded/-40: commit 5 ks_sharded/80-c0: commit ---------------------------------------------------------------------- diff --git a/data/test/vtexplain/multi-output/updatesharded-output.json b/data/test/vtexplain/multi-output/updatesharded-output.json index 686cb235b2..53f54ac029 100644 --- a/data/test/vtexplain/multi-output/updatesharded-output.json +++ b/data/test/vtexplain/multi-output/updatesharded-output.json @@ -214,5 +214,414 @@ ] } } + }, + { + "SQL": "update user set name='alicia' where id=1", + "Plans": [ + { + "Original": "insert into name_user_map(name, user_id) values (:name0, :user_id0)", + "Instructions": { + "Opcode": "InsertSharded", + "Keyspace": { + "Name": "ks_sharded", + "Sharded": true + }, + "Query": "insert into name_user_map(name, user_id) values (:_name0, :user_id0)", + "Values": [ + [ + [ + ":name0" + ] + ] + ], + "Table": "name_user_map", + "Prefix": "insert into name_user_map(name, user_id) values ", + "Mid": [ + "(:_name0, :user_id0)" + ] + }, + "ExecCount": 1, + "ExecTime": 0, + "ShardQueries": 1, + "Rows": 1, + "Errors": 0 + }, + { + "Original": "delete from name_user_map where name = :name and user_id = :user_id", + "Instructions": { + "Opcode": "DeleteEqual", + "Keyspace": { + "Name": "ks_sharded", + "Sharded": true + }, + "Query": "delete from name_user_map where name = :name and user_id = :user_id", + "Vindex": "md5", + "Values": [ + ":name" + ], + "Table": "name_user_map" + }, + "ExecCount": 1, + "ExecTime": 0, + "ShardQueries": 1, + "Rows": 1, + "Errors": 0 + }, + { + "Original": "update user set name = :vtg1 where id = :vtg2", + "Instructions": { + "Opcode": "UpdateEqual", + "Keyspace": { + "Name": "ks_sharded", + "Sharded": true + }, + "Query": "update user set name = :vtg1 where id = :vtg2", + "Vindex": "hash", + "Values": [ + ":vtg2" + ], + "ChangedVindexValues": { + "name_user_map": [ + ":vtg1" + ] + }, + "Table": "user", + "Subquery": "select name from user where id = :vtg2 for update" + }, + "ExecCount": 1, + "ExecTime": 0, + "ShardQueries": 2, + "Rows": 1, + "Errors": 0 + } + ], + "TabletActions": { + "ks_sharded/-40": { + "TabletQueries": [ + { + "Time": 1, + "SQL": "select name from user where id = :vtg2 for update", + "BindVars": { + "#maxLimit": "10001", + "vtg1": "'alicia'", + "vtg2": "1" + } + }, + { + "Time": 4, + "SQL": "update user set name = :vtg1 where id = :vtg2 /* vtgate:: keyspace_id:166b40b44aba4bd6 */", + "BindVars": { + "#maxLimit": "10001", + "_name0": "'alicia'", + "vtg1": "'alicia'", + "vtg2": "1" + } + } + ], + "MysqlQueries": [ + { + "Time": 1, + "SQL": "begin" + }, + { + "Time": 1, + "SQL": "select name from user where id = 1 limit 10001 for update" + }, + { + "Time": 4, + "SQL": "update user set name = 'alicia' where id in (1) /* vtgate:: keyspace_id:166b40b44aba4bd6 */" + }, + { + "Time": 5, + "SQL": "commit" + } + ] + }, + "ks_sharded/80-c0": { + "TabletQueries": [ + { + "Time": 2, + "SQL": "delete from name_user_map where name = :name and user_id = :user_id /* vtgate:: keyspace_id:a6e89b54b129c33051b76db219595660 */", + "BindVars": { + "#maxLimit": "10001", + "name": "'name_val_1'", + "user_id": "1" + } + } + ], + "MysqlQueries": [ + { + "Time": 2, + "SQL": "begin" + }, + { + "Time": 2, + "SQL": "delete from name_user_map where (name = 'name_val_1' and user_id = 1) /* vtgate:: keyspace_id:a6e89b54b129c33051b76db219595660 */" + }, + { + "Time": 6, + "SQL": "commit" + } + ] + }, + "ks_sharded/c0-": { + "TabletQueries": [ + { + "Time": 3, + "SQL": "insert into name_user_map(name, user_id) values (:_name0, :user_id0) /* vtgate:: keyspace_id:e2821261367fbee90bb5cf72955146c6 */", + "BindVars": { + "#maxLimit": "10001", + "_name0": "'alicia'", + "name0": "'alicia'", + "user_id0": "1" + } + } + ], + "MysqlQueries": [ + { + "Time": 3, + "SQL": "begin" + }, + { + "Time": 3, + "SQL": "insert into name_user_map(name, user_id) values ('alicia', 1) /* vtgate:: keyspace_id:e2821261367fbee90bb5cf72955146c6 */" + }, + { + "Time": 7, + "SQL": "commit" + } + ] + } + } + }, + { + "SQL": "update user set name='alicia' where name='alice'", + "Plans": [ + { + "Original": "insert into name_user_map(name, user_id) values (:name0, :user_id0)", + "Instructions": { + "Opcode": "InsertSharded", + "Keyspace": { + "Name": "ks_sharded", + "Sharded": true + }, + "Query": "insert into name_user_map(name, user_id) values (:_name0, :user_id0)", + "Values": [ + [ + [ + ":name0" + ] + ] + ], + "Table": "name_user_map", + "Prefix": "insert into name_user_map(name, user_id) values ", + "Mid": [ + "(:_name0, :user_id0)" + ] + }, + "ExecCount": 1, + "ExecTime": 0, + "ShardQueries": 1, + "Rows": 1, + "Errors": 0 + }, + { + "Original": "delete from name_user_map where name = :name and user_id = :user_id", + "Instructions": { + "Opcode": "DeleteEqual", + "Keyspace": { + "Name": "ks_sharded", + "Sharded": true + }, + "Query": "delete from name_user_map where name = :name and user_id = :user_id", + "Vindex": "md5", + "Values": [ + ":name" + ], + "Table": "name_user_map" + }, + "ExecCount": 1, + "ExecTime": 0, + "ShardQueries": 1, + "Rows": 1, + "Errors": 0 + }, + { + "Original": "select user_id from name_user_map where name = :name", + "Instructions": { + "Opcode": "SelectEqualUnique", + "Keyspace": { + "Name": "ks_sharded", + "Sharded": true + }, + "Query": "select user_id from name_user_map where name = :name", + "FieldQuery": "select user_id from name_user_map where 1 != 1", + "Vindex": "md5", + "Values": [ + ":name" + ] + }, + "ExecCount": 1, + "ExecTime": 0, + "ShardQueries": 1, + "Rows": 1, + "Errors": 0 + }, + { + "Original": "update user set name = :vtg1 where name = :vtg2", + "Instructions": { + "Opcode": "UpdateEqual", + "Keyspace": { + "Name": "ks_sharded", + "Sharded": true + }, + "Query": "update user set name = :vtg1 where name = :vtg2", + "Vindex": "name_user_map", + "Values": [ + ":vtg2" + ], + "ChangedVindexValues": { + "name_user_map": [ + ":vtg1" + ] + }, + "Table": "user", + "Subquery": "select name from user where name = :vtg2 for update" + }, + "ExecCount": 1, + "ExecTime": 0, + "ShardQueries": 2, + "Rows": 1, + "Errors": 0 + } + ], + "TabletActions": { + "ks_sharded/-40": { + "TabletQueries": [ + { + "Time": 2, + "SQL": "select name from user where name = :vtg2 for update", + "BindVars": { + "#maxLimit": "10001", + "vtg1": "'alicia'", + "vtg2": "'alice'" + } + }, + { + "Time": 5, + "SQL": "update user set name = :vtg1 where name = :vtg2 /* vtgate:: keyspace_id:166b40b44aba4bd6 */", + "BindVars": { + "#maxLimit": "10001", + "_name0": "'alicia'", + "vtg1": "'alicia'", + "vtg2": "'alice'" + } + } + ], + "MysqlQueries": [ + { + "Time": 2, + "SQL": "begin" + }, + { + "Time": 2, + "SQL": "select name from user where name = 'alice' limit 10001 for update" + }, + { + "Time": 5, + "SQL": "select id from user where name = 'alice' limit 10001 for update /* vtgate:: keyspace_id:166b40b44aba4bd6 */" + }, + { + "Time": 5, + "SQL": "update user set name = 'alicia' where id in (1) /* vtgate:: keyspace_id:166b40b44aba4bd6 */" + }, + { + "Time": 7, + "SQL": "commit" + } + ] + }, + "ks_sharded/40-80": { + "TabletQueries": [ + { + "Time": 1, + "SQL": "select user_id from name_user_map where name = :name", + "BindVars": { + "#maxLimit": "10001", + "name": "'alice'" + } + } + ], + "MysqlQueries": [ + { + "Time": 1, + "SQL": "begin" + }, + { + "Time": 1, + "SQL": "select user_id from name_user_map where name = 'alice' limit 10001" + }, + { + "Time": 6, + "SQL": "commit" + } + ] + }, + "ks_sharded/80-c0": { + "TabletQueries": [ + { + "Time": 3, + "SQL": "delete from name_user_map where name = :name and user_id = :user_id /* vtgate:: keyspace_id:a6e89b54b129c33051b76db219595660 */", + "BindVars": { + "#maxLimit": "10001", + "name": "'name_val_1'", + "user_id": "1" + } + } + ], + "MysqlQueries": [ + { + "Time": 3, + "SQL": "begin" + }, + { + "Time": 3, + "SQL": "delete from name_user_map where (name = 'name_val_1' and user_id = 1) /* vtgate:: keyspace_id:a6e89b54b129c33051b76db219595660 */" + }, + { + "Time": 8, + "SQL": "commit" + } + ] + }, + "ks_sharded/c0-": { + "TabletQueries": [ + { + "Time": 4, + "SQL": "insert into name_user_map(name, user_id) values (:_name0, :user_id0) /* vtgate:: keyspace_id:e2821261367fbee90bb5cf72955146c6 */", + "BindVars": { + "#maxLimit": "10001", + "_name0": "'alicia'", + "name0": "'alicia'", + "user_id0": "1" + } + } + ], + "MysqlQueries": [ + { + "Time": 4, + "SQL": "begin" + }, + { + "Time": 4, + "SQL": "insert into name_user_map(name, user_id) values ('alicia', 1) /* vtgate:: keyspace_id:e2821261367fbee90bb5cf72955146c6 */" + }, + { + "Time": 9, + "SQL": "commit" + } + ] + } + } } ] diff --git a/data/test/vtexplain/multi-output/updatesharded-output.txt b/data/test/vtexplain/multi-output/updatesharded-output.txt index 0a4c28ba56..62b02b7bea 100644 --- a/data/test/vtexplain/multi-output/updatesharded-output.txt +++ b/data/test/vtexplain/multi-output/updatesharded-output.txt @@ -24,3 +24,35 @@ update user set pet='fido' where id=1 2 ks_sharded/-40: commit ---------------------------------------------------------------------- +update user set name='alicia' where id=1 + +1 ks_sharded/-40: begin +1 ks_sharded/-40: select name from user where id = 1 limit 10001 for update +2 ks_sharded/80-c0: begin +2 ks_sharded/80-c0: delete from name_user_map where (name = 'name_val_1' and user_id = 1) /* vtgate:: keyspace_id:a6e89b54b129c33051b76db219595660 */ +3 ks_sharded/c0-: begin +3 ks_sharded/c0-: insert into name_user_map(name, user_id) values ('alicia', 1) /* vtgate:: keyspace_id:e2821261367fbee90bb5cf72955146c6 */ +4 ks_sharded/-40: update user set name = 'alicia' where id in (1) /* vtgate:: keyspace_id:166b40b44aba4bd6 */ +5 ks_sharded/-40: commit +6 ks_sharded/80-c0: commit +7 ks_sharded/c0-: commit + +---------------------------------------------------------------------- +update user set name='alicia' where name='alice' + +1 ks_sharded/40-80: begin +1 ks_sharded/40-80: select user_id from name_user_map where name = 'alice' limit 10001 +2 ks_sharded/-40: begin +2 ks_sharded/-40: select name from user where name = 'alice' limit 10001 for update +3 ks_sharded/80-c0: begin +3 ks_sharded/80-c0: delete from name_user_map where (name = 'name_val_1' and user_id = 1) /* vtgate:: keyspace_id:a6e89b54b129c33051b76db219595660 */ +4 ks_sharded/c0-: begin +4 ks_sharded/c0-: insert into name_user_map(name, user_id) values ('alicia', 1) /* vtgate:: keyspace_id:e2821261367fbee90bb5cf72955146c6 */ +5 ks_sharded/-40: select id from user where name = 'alice' limit 10001 for update /* vtgate:: keyspace_id:166b40b44aba4bd6 */ +5 ks_sharded/-40: update user set name = 'alicia' where id in (1) /* vtgate:: keyspace_id:166b40b44aba4bd6 */ +6 ks_sharded/40-80: commit +7 ks_sharded/-40: commit +8 ks_sharded/80-c0: commit +9 ks_sharded/c0-: commit + +---------------------------------------------------------------------- diff --git a/data/test/vtexplain/updatesharded-queries.sql b/data/test/vtexplain/updatesharded-queries.sql index bcf0b927a9..ef1b976411 100644 --- a/data/test/vtexplain/updatesharded-queries.sql +++ b/data/test/vtexplain/updatesharded-queries.sql @@ -2,5 +2,9 @@ update user set nickname='alice' where id=1; update user set nickname='alice' where name='alice'; update user set pet='fido' where id=1; +/* update secondary vindex value */ +update user set name='alicia' where id=1; +update user set name='alicia' where name='alice'; + /* not supported - multi-shard update */ -- update user set pet='rover' where name='alice';