You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
--- select_cases.json-- No column referencedselect1from plan_test
-- unqualified '*' expression for simple routeselect*from plan_test
-- qualified '*' expression for simple routeselect plan_test.*from plan_test
-- fully qualified '*' expression for simple routeselect :ks.plan_test.*from :ks.plan_test-- Hex number is not treated as a simple valueselect*from plan_test where f_tinyint = 0x04
-- sharded limit offsetselect f_int from plan_test order by f_int limit10, 20-- Multiple parenthesized expressionsselect*from plan_test where (f_tinyint =4) AND (f_varchar ='abc') limit5-- Column Aliasing with Columnselectuser0_.f_intas col0_ from plan_test user0_ where f_tinyint =1order by col0_ desclimit3-- Booleans and parenthesisselect*from plan_test where (f_tinyint =1) AND f_bit = true limit5-- Column as boolean-ishselect*from plan_test where (f_tinyint =1) AND f_bit limit5-- PK as fake boolean, and column as boolean-ishselect*from plan_test where (f_tinyint =5) AND f_bit = true limit5-- sql_calc_found_rows without limitselect sql_calc_found_rows *from plan_test where f_tinyint =1
聚合查询
--- aggr_cases.json-- Aggregate on unique shardedselectcount(*), f_int from plan_test where f_tinyint =1-- Aggregate detection (non-aggregate function)selecttrim(' ABC '), f_tinyint from plan_test
-- select distinct with unique vindex for scatter route.select distinct f_int, f_tinyint from plan_test
-- distinct and group by together for single route.select distinct f_int, f_tinyint from plan_test group by f_int
-- count aggregateselectcount(*) from plan_test
-- sum aggregateselectsum(f_int) from plan_test
-- min aggregateselectmin(f_int) from plan_test
-- max aggregateselectmax(f_int) from plan_test
-- distinct and group by together for scatter routeselect distinct f_int, f_midint from plan_test group by f_int
-- group by a unique vindex should use a simple routeselect f_tinyint, count(*) from plan_test group by f_tinyint
-- group by a unique vindex and other column should use a simple routeselect f_tinyint, f_int, count(*) from plan_test group by f_tinyint, f_int
-- group by a non-vindex column should use an OrderdAggregate primitiveselect f_int, count(*) from plan_test group by f_int
-- group by a unique vindex should use a simple route, even if aggr is complexselect f_tinyint, 1+count(*) from plan_test group by f_tinyint
-- group by a unique vindex where alias from select list is usedselect f_tinyint as val, 1+count(*) from plan_test group by val
-- group by a unique vindex where expression is qualified (alias should be ignored)select f_int as id, 1+count(*) from plan_test group byplan_test.f_tinyint-- group by a unique vindex where it should skip non-aliased expressions.select*, f_tinyint, 1+count(*) from plan_test group by f_tinyint
-- group by a unique vindex should revert to simple route, and having clause should find the correct symbols.select f_tinyint, count(*) c from plan_test group by f_tinyint having f_tinyint =1and c =1-- scatter aggregate using distinctselect distinct f_int from plan_test
-- scatter aggregate group by select colselect f_int from plan_test group by f_int
-- count with distinct group by unique vindexselect f_tinyint, count(distinct f_int) from plan_test group by f_tinyint
-- count with distinct unique vindexselect f_int, count(distinct f_tinyint) from plan_test group by f_int
-- count with distinct no unique vindexselect f_int, count(distinct f_timestamp) from plan_test group by f_int
-- count with distinct no unique vindex and no group byselectcount(distinct f_int) from plan_test
-- count with distinct no unique vindex, count expression aliasedselect f_smallint, count(distinct f_int) c2 from plan_test group by f_smallint
-- sum with distinct no unique vindexselect f_smallint, sum(distinct f_int) from plan_test group by f_smallint
-- min with distinct no unique vindex. distinct is ignored.select f_smallint, min(distinct f_int) from plan_test group by f_smallint
-- order by count distinctselect f_smallint, count(distinct f_int) k from plan_test group by f_smallint order by k
-- scatter aggregate multiple group by (columns)select f_smallint, f_int, count(*) from plan_test group by f_int, f_smallint
-- scatter aggregate group by column numberselect f_int from plan_test group by1-- scatter aggregate order by nullselectcount(*) from plan_test order bynull-- scatter aggregate with numbered order by columnsselect f_smallint, f_midint, f_int, f_varchar, count(*) from plan_test group by1, 2, 3order by1, 2, 3-- scatter aggregate with named order by columnsselect f_smallint, f_midint, f_int, f_varchar, count(*) from plan_test group by1, 2, 3order by f_smallint, f_midint, f_int
-- scatter aggregate with jumbled order by columnsselect f_smallint, f_midint, f_int, f_bigint, count(*) from plan_test group by1, 2, 3, 4order by f_smallint, f_midint, f_int, f_bigint
-- scatter aggregate with jumbled group by and order by columnsselect f_smallint, f_midint, f_int, f_bigint, count(*) from plan_test group by3, 2, 1, 4order by f_bigint, f_midint, f_smallint, f_int
-- scatter aggregate with some descending order by colsselect f_smallint, f_midint, f_int, count(*) from plan_test group by3, 2, 1order by1desc, 3desc, f_midint
-- Group by with collate operatorselectplan_test.f_intas a from plan_test whereplan_test.f_tinyint=5group by a collate utf8_general_ci
过滤条件
--- filter_cases.json-- No where clauseselect f_tinyint from plan_test
-- Query that always return emptyselect f_tinyint from plan_test where f_umidint =null-- Single table unique vindex routeselect f_tinyint from plan_test whereplan_test.f_tinyint=5-- Single table unique vindex route, but complex exprselect f_tinyint from plan_test whereplan_test.f_tinyint=5+5-- Single table complex in clauseselect f_tinyint from plan_test where f_varchar in (f_text, 'bb')
-- Route with multiple route constraints, SelectIN is the best constraintselect f_tinyint from plan_test whereplan_test.f_midint=123456andplan_test.f_tinyintin (1, 2)
-- Route with multiple route constraints and boolean, SelectIN is the best constraint.select f_tinyint from plan_test whereplan_test.f_varchar= case plan_test.f_varchar when 'foo' then true else false end andplan_test.f_tinyintin (1, 2)
-- Route with multiple route constraints, SelectEqualUnique is the best constraint, order reversed.select f_tinyint from plan_test whereplan_test.f_midint=123456andplan_test.f_tinyintin (1, 11) andplan_test.f_varchar='abc'andplan_test.f_tinyint=1-- Route with OR and AND clause, must parenthesize correctly. order needed; if not ordered, it may report failselect f_tinyint from plan_test whereplan_test.f_tinyint=1orplan_test.f_varchar='abc'andplan_test.f_tinyintin (1, 2, 8)
-- Route with OR and AND clause, must parenthesize correctly.select f_tinyint from plan_test whereplan_test.f_tinyint=1orplan_test.f_varchar='abc'andplan_test.f_tinyintin (1, 2)
-- SELECT with IS NULL.select f_tinyint from plan_test where f_umidint is null-- Single table with unique vindex match and null match.select f_tinyint from plan_test where f_tinyint =4and f_umidint is null-- Single table with unique vindex match and IN (null).select f_tinyint from plan_test where f_midint =123456and f_tinyint IN (null)
-- Single table with unique vindex match and IN (null, 1, 2).select f_tinyint from plan_test where f_midint =123456and f_tinyint IN (null, 1, 2)
-- Single table with unique vindex match and IN (null, 1, 2).select f_tinyint from plan_test where f_midint =123456and f_tinyint NOT IN (null, 1, 2)
FROM 子句
--- from_cases.json-- Single information_schema queryselect table_schema frominformation_schema.tables-- access to unqualified column names in information_schemaselect table_schema frominformation_schema.tableswhere table_schema ='mysql'-- access to qualified column names in information_schemaselect table_schema frominformation_schema.tableswhereinformation_schema.tables.table_schema ='mysql'-- Single performance_schema queryselect host fromperformance_schema.hostswhere host in ('localhost', '127.0.0.1')
-- access to unqualified column names in performance_schemaselect host fromperformance_schema.hostswhere host ='localhost'-- access to qualified column names in performance_schemaselect host fromperformance_schema.hostswhereperformance_schema.hosts.host ='localhost'-- Single sys queryselect mysql_version fromsys.version-- access to unqualified column names in sysselect mysql_version fromsys.versionwhere mysql_version is not null-- access to qualified column names in sysselect mysql_version fromsys.versionwheresys.version.mysql_version is not null-- Single mysql queryselect plugin frommysql.user-- access to unqualified column names in mysqlselect plugin frommysql.userwhere user ='root'limit1-- access to qualified column names in mysqlselect plugin frommysql.userwheremysql.user.user ='root'limit1
排序
--- memory_sort_cases.json-- scatter aggregate order by references ungrouped columnselect f_int, f_smallint, count(*) from plan_test group by f_int order by f_smallint
-- scatter aggregate order by references ungrouped columnselect f_int, f_smallint, count(*) k from plan_test group by f_int order by k
-- scatter aggregate order by references multiple non-group-by expressionsselect f_int, f_smallint, count(*) k from plan_test group by f_int order by f_smallint, f_int, k
-- scatter aggregate with memory sort and limitselect f_int, f_smallint, count(*) k from plan_test group by f_int order by k desclimit10-- scatter aggregate with memory sort and order by numberselect f_int, f_smallint, count(*) k from plan_test group by f_int order by1,3
Post Process
--- postprocess_cases.json-- ORDER BY uses column numbersselect f_midint from plan_test where f_tinyint =1order by1-- ORDER BY uses column numbersselect f_midint from plan_test order by f_midint
-- ORDER BY on scatter with multiple number columnselect f_smallint, f_midint, f_int from plan_test order by f_smallint, f_midint, f_int
-- ORDER BY on scatter with number column, qualified nameselect f_smallint, plan_test.f_midint, f_int from plan_test order by f_smallint, f_midint, f_int
-- ORDER BY NULLselect f_int from plan_test order bynull-- ORDER BY RAND()select f_midint from plan_test order by RAND()
-- Order by, '*' expressionselect*from plan_test where f_tinyint =5order by f_midint
-- Order by, qualified '*' expressionselect plan_test.*from plan_test where f_tinyint =5order by f_midint
-- Order by, qualified '*' expressionselect*from plan_test where f_tinyint =5order byplan_test.f_midint-- Order by with math functionsselect*from plan_test where f_tinyint =5order by-f_midint
-- Order by with math operationsselect*from plan_test where f_tinyint =5order by f_tinyint + f_varchar collate utf8_general_ci desc-- LIMITselect f_bigint from plan_test where f_tinyint =9limit1-- limit for scatterselect f_bigint from plan_test limit1-- cross-shard expression in parenthesis with limitselect f_int from plan_test where (f_int =12345AND f_varchar ='abc') order by f_int limit5
流式聚合查询
--- stream_aggr_cases.json-- order by count distinctselect f_smallint, count(distinct f_int), f_tinyint k from plan_test group by f_smallint order by k,f_tinyint
流式排序查询
--- stream_memory_sort_cases.json-- scatter aggregate order by references ungrouped columnselect f_int, f_smallint, count(*) k from plan_test group by f_int order by k, f_smallint
-- scatter aggregate with memory sort and limitselect f_int, f_smallint, count(*) k from plan_test group by f_int order by k desc,f_int limit10
算术运算
--- calculate_dual_case.json-- select from nothingselect1-- select from dualselect1as result from dual
-- add arithmeticselect1+2as result from dual
-- minus arithmeticselect1-2as result from dual
-- multiply arithmeticselect3*2as result from dual
-- divide arithmeticselect5/2as result from dual
Deep Pagination
--- deepPagination.json-- Sharding Key Condition in Parenthesisselect*from plan_test where f_varchar ='abc'AND (f_tinyint =4) limit5-- Column Aliasing with Table.Columnselectuser0_.f_intas col0_ from plan_test user0_ where f_tinyint =1order byuser0_.f_intdesclimit2
JOIN 子句(分片、不分片)
--- join/sharded/join_cases.json-- Multi-table unique vindex constraintselectuser_extra.idfrom user join user_extra onuser.name=user_extra.user_idwhereuser.name=105-- Multi-table unique vindex constraint on right tableselectuser_extra.idfrom user join user_extra onuser.name=user_extra.user_idwhereuser_extra.user_id=105-- Multi-table unique vindex constraint on left table of left joinselectuser_extra.idfrom user left join user_extra onuser.name=user_extra.user_idwhereuser.name=105-- Multi-table unique vindex constraint on left-joined right tableselectuser_extra.idfrom user left join user_extra onuser.name=user_extra.user_idwhereuser_extra.user_id=105-- Multi-route unique vindex constraintselectuser_extra.idfrom user join user_extra onuser.costly=user_extra.extra_idwhereuser.name=105-- Multi-route unique vindex route on both routesselectuser_extra.idfrom user join user_extra onuser.costly=user_extra.extra_idwhereuser.name=105anduser_extra.user_id=105-- Multi-route with cross-route constraintselectuser_extra.idfrom user join user_extra onuser.costly=user_extra.extra_idwhereuser_extra.user_id=user.costly-- Multi-route with non-route constraint, should use first route.selectuser_extra.idfrom user join user_extra onuser.costly=user_extra.extra_idwhere1=1-- Case preservation testselectuser_extra.Idfrom user join user_extra onuser.nAME=user_extra.User_Idwhereuser.Name=105-- Multi-table, multi-chunkselectmusic.colfrom user join music
-- ',' joinselectmusic.colfrom user, music
-- mergeable sharded join on unique vindexselectuser.costlyfrom user join user_extra onuser.name=user_extra.user_id-- mergeable sharded join on unique vindex (parenthesized ON clause)selectuser.costlyfrom user join user_extra on (user.name=user_extra.user_id)
-- mergeable sharded join on unique vindex, with a stray conditionselectuser.costlyfrom user join user_extra onuser.costly between 100and200anduser.name=user_extra.user_id-- mergeable sharded join on unique vindex, swapped operandsselectuser.costlyfrom user join user_extra onuser_extra.user_id=user.name-- mergeable sharded join on unique vindex, and conditionselectuser.costlyfrom user join user_extra onuser.name=105anduser.name=user_extra.user_id-- sharded join on unique vindex, inequalityselectuser.costlyfrom user join user_extra onuser.name<user_extra.user_id-- sharded join, non-col reference RHSselectuser.costlyfrom user join user_extra onuser.name=105-- sharded join, non-col reference LHSselectuser.costlyfrom user join user_extra on105=user.name-- sharded join, non-vindex colselectuser.costlyfrom user join user_extra onuser.costly=user_extra.extra_id-- col refs should be case-insensitiveselectuser.costlyfrom user join user_extra onuser.NAME=user_extra.User_Id-- order by on a cross-shard query. Note: this happens only when an order by column is from the second tableselectuser.predef1as a, user.predef2 b, music.col c from user, music whereuser.name=music.user_idanduser.name=101order by c
-- Order by for join, with mixed cross-shard orderingselectuser.predef1as a, user.predef2, music.colfrom user join music onuser.name=music.idwhereuser.name=101order by1asc, 3desc, 2asc-- non-ambiguous symbol referenceselectuser.predef1, user_extra.extra_idfrom user join user_extra havinguser_extra.extra_id=102-- HAVING multi-routeselectuser.predef1as a, user.predef2, user_extra.extra_idfrom user join user_extra having1=1and a =101and a =user.predef2anduser_extra.extra_id=101-- ORDER BY NULL for joinselectuser.predef1as a, user.predef2, music.colfrom user join music onuser.name=music.idwhereuser.name=101order bynull-- ORDER BY non-key column for joinselectuser.predef1as a, user.predef2, music.colfrom user join music onuser.name=music.idwhereuser.name=101order by a
-- ORDER BY non-key column for implicit joinselectuser.predef1as a, user.predef2, music.colfrom user, music whereuser.name=music.idanduser.name=101order by a
-- ORDER BY RAND() for joinselectuser.predef1as a, user.predef2, music.colfrom user join music onuser.name=music.idwhereuser.name=101order by RAND()
-- select * from join of authoritative tablesselect*from authoritative a join authoritative b ona.user_id=b.user_id-- select * from intermixing of authoritative table with non-authoritative results in no expansionselect*from authoritative join user onauthoritative.user_id=user.name-- select authoritative.* with intermixing still expandsselectuser.name, a.*, user.costlyfrom authoritative a join user ona.user_id=user.name-- auto-resolve anonymous columns for simple routeselect costly from user join user_extra onuser.name=user_extra.user_id-- Auto-resolve should work if unique vindex columns are referencedselect name, user_id from user join user_extra
-- RHS route referencedselectuser_extra.idfrom user join user_extra
-- Both routes referencedselectuser.costly, user_extra.idfrom user join user_extra
-- Expression with single-route referenceselectuser.costly, user_extra.id+user_extra.extra_idfrom user join user_extra
-- Jumbled referencesselectuser.textcol1, user_extra.id, user.textcol2from user join user_extra
-- for updateselectuser.costlyfrom user join user_extra for update-- Case preservationselectuser.Costly, user_extra.Idfrom user join user_extra
--- join/unsharded/join_cases.json-- Multi-table unshardedselectt1.f_tinyintfrom engine_test as t1 join plan_test as t2
-- Multi-table unshardedselectt1.f_tinyintfrom engine_test as t1, plan_test as t2
-- Multi-table unshardedselectt1.f_tinyintfrom engine_test as t1 left join plan_test as t2 ont1.f_tinyint=t2.f_tinyint-- Multi-table unshardedselectt1.f_tinyintfrom engine_test as t1 straight_join plan_test as t2 ont1.f_tinyint=t2.f_tinyint-- Multi-table unshardedselectt1.f_tinyintfrom engine_test as t1 right join plan_test as t2 ont1.f_tinyint=t2.f_tinyint-- Multi-table unshardedselectt1.f_tinyintfrom engine_test as t1 inner join plan_test as t2 ont1.f_tinyint=t2.f_tinyint
子查询 (分片、不分片)
--- subquery/sharded/subquery_cases.json-- scatter aggregate in a subqueryselect cnt from (selectcount(*) as cnt from user) t
-- subquery of information_schema with itselfselect PLUGIN_NAME pluginName frominformation_schema.PLUGINSwhere PLUGIN_NAME in (select PLUGIN_NAME frominformation_schema.PLUGINS)
-- subqueryselectu.idfrom user_extra join user u whereu.namein (select name from user whereuser.name=u.nameanduser_extra.extra_id=user.predef1) andu.namein (user_extra.extra_id, 1)
-- ensure subquery reordering gets us a better planselectu.idfrom user_extra join user u whereu.namein (select costly from user whereuser.name=105) andu.name=105-- nested subqueryselectu.idfrom user_extra join user u whereu.namein (select predef2 from user whereuser.name=u.nameanduser_extra.extra_id=user.predef1anduser.namein (select extra_id from user_extra whereuser_extra.user_id=user.name)) andu.namein (user_extra.extra_id, 1)
-- Correlated subquery in where clauseselect id from user whereuser.predef1in (selectuser_extra.extra_idfrom user_extra whereuser_extra.user_id=user.name)
-- outer and inner subquery route by same int valselect id from user where name =105anduser.predef1in (selectuser_extra.extra_idfrom user_extra whereuser_extra.user_id=105)
-- outer and inner subquery route by same str valselect id from user where name ='103'anduser.predef1in (selectuser_extra.extra_idfrom user_extra whereuser_extra.user_id='103')
-- outer and inner subquery route by same outermost column valueselect id from user uu where name in (select name from user where name =uu.nameanduser.predef1in (selectuser_extra.extra_idfrom user_extra whereuser_extra.user_id=uu.name))
-- cross-shard subquery in IN clause.select name from user where name in (select costly from user)
-- cross-shard subquery in EXISTS clause.select id from user where exists (select predef1 from user)
-- cross-shard subquery as expressionselect user_id from music where user_id = (select user_id from music)
-- multi-level pulloutselect user_id from music where user_id = (select user_id from music where user_id in (select user_id from music))
-- database() call in where clause.select id from user where database()
-- Select with equals nullselect id from music where id =null-- information_schema query using database() funcSELECT INDEX_LENGTH FROMINFORMATION_SCHEMA.TABLESWHERE TABLE_SCHEMA = DATABASE()
-- index hints, make sure they are not stripped.selectuser.costlyfrom user use index(user_costly_uindex)
-- subqueryselect id from (select id, textcol1 from user where name =5) as t
-- subquery with joinselectt.namefrom ( select name from user where name =105 ) as t join user_extra ont.name=user_extra.user_id-- subquery with join, and aliased referencesselectt.namefrom ( selectuser.namefrom user whereuser.name=105 ) as t join user_extra ont.name=user_extra.user_id-- subquery in RHS of joinselectt.namefrom user_extra join ( select name from user where name =105 ) as t ont.name=user_extra.user_id-- merge subqueries with single-shard routesselectu.predef1, e.extra_idfrom ( select predef1 from user where name =105 ) as u join ( select extra_id from user_extra where user_id =105 ) as e
-- wire-up on within cross-shard subqueryselectt.idfrom ( selectuser.id, user.predef2from user join user_extra onuser_extra.extra_id=user.costly ) as t
-- subquery with join primitive (FROM)select id, t.idfrom ( selectuser.idfrom user join user_extra ) as t
-- order by on a cross-shard subqueryselect name from ( selectuser.name, user.costlyfrom user join user_extra ) as t order by name
-- HAVING uses subqueryselect name from user having name in ( select costly from user )
-- Order by subquery columnselectu.namefrom user u join ( select user_id from user_extra where user_id =105 ) eu onu.name=eu.user_idwhereu.name=105order byeu.user_id--- subquery/unsharded/subquery_cases.json-- scatter aggregate in a subqueryselect cnt from (selectcount(*) as cnt from user_unsharded) t
-- subqueryselectu.idfrom user_unsharded_extra join user_unsharded u whereu.namein (select name from user_unsharded whereuser_unsharded.name=u.nameanduser_unsharded_extra.extra_id=user_unsharded.predef1) andu.namein (user_unsharded_extra.extra_id, 1)
-- ensure subquery reordering gets us a better planselectu.idfrom user_unsharded_extra join user_unsharded u whereu.namein (select costly from user_unsharded whereuser_unsharded.name=105) andu.name=105-- nested subqueryselectu.idfrom user_unsharded_extra join user_unsharded u whereu.namein (select predef2 from user_unsharded whereuser_unsharded.name=u.nameanduser_unsharded_extra.extra_id=user_unsharded.predef1anduser_unsharded.namein (select extra_id from user_unsharded_extra whereuser_unsharded_extra.user_id=user_unsharded.name)) andu.namein (user_unsharded_extra.extra_id, 1)
-- Correlated subquery in where clauseselect id from user_unsharded whereuser_unsharded.predef1in (selectuser_unsharded_extra.extra_idfrom user_unsharded_extra whereuser_unsharded_extra.user_id=user_unsharded.name)
-- outer and inner subquery route by same int valselect id from user_unsharded where name =105anduser_unsharded.predef1in (selectuser_unsharded_extra.extra_idfrom user_unsharded_extra whereuser_unsharded_extra.user_id=105)
-- outer and inner subquery route by same str valselect id from user_unsharded where name ='103'anduser_unsharded.predef1in (selectuser_unsharded_extra.extra_idfrom user_unsharded_extra whereuser_unsharded_extra.user_id='103')
-- outer and inner subquery route by same outermost column valueselect id from user_unsharded uu where name in (select name from user_unsharded where name =uu.nameanduser_unsharded.predef1in (selectuser_unsharded_extra.extra_idfrom user_unsharded_extra whereuser_unsharded_extra.user_id=uu.name))
-- cross-shard subquery in IN clause.select name from user_unsharded where name in (select costly from user_unsharded)
-- cross-shard subquery in NOT IN clause.select name from user_unsharded where name not in (select textcol1 from user_unsharded)
-- cross-shard subquery in EXISTS clause.select id from user_unsharded where exists (select predef1 from user_unsharded)
-- database() call in where clause.select id from user_unsharded where database()
-- information_schema query using database() funcSELECT INDEX_LENGTH, TABLE_NAME FROMINFORMATION_SCHEMA.TABLESWHERE TABLE_SCHEMA = DATABASE()
-- index hints, make sure they are not stripped.selectuser_unsharded.costlyfrom user_unsharded use index(user_costly_uindex)
-- subqueryselect id from (select id, textcol1 from user_unsharded where name =5) as t
-- subquery with join, and aliased referencesselectt.namefrom ( selectuser_unsharded.namefrom user_unsharded whereuser_unsharded.name=105 ) as t join user_unsharded_extra ont.name=user_unsharded_extra.user_id-- subquery in RHS of joinselectt.namefrom user_unsharded_extra join ( select name from user_unsharded where name =105 ) as t ont.name=user_unsharded_extra.user_id-- subquery in FROM with cross-shard joinselectt.namefrom ( select name from user_unsharded where name =105 ) as t join user_unsharded_extra ont.name=user_unsharded_extra.user_id-- merge subqueries with single-shard routesselectu.predef1, e.extra_idfrom ( select predef1 from user_unsharded where name =105 ) as u join ( select extra_id from user_unsharded_extra where user_id =105 ) as e
-- wire-up on within cross-shard subqueryselectt.idfrom ( selectuser_unsharded.id, user_unsharded.predef2from user_unsharded join user_unsharded_extra onuser_unsharded_extra.extra_id=user_unsharded.costly ) as t
-- subquery with join primitive (FROM)select id, t.idfrom ( selectuser_unsharded.idfrom user_unsharded join user_unsharded_extra ) as t
-- order by on a cross-shard subqueryselect name from ( selectuser_unsharded.name, user_unsharded.costlyfrom user_unsharded join user_unsharded_extra ) as t order by name
-- HAVING uses subqueryselect name from user_unsharded having name in ( select costly from user_unsharded )
-- ORDER BY after pull-out subqueryselect textcol1 from user_unsharded where textcol1 in ( select textcol1 from user_unsharded ) order by textcol1
-- ORDER BY NULL after pull-out subqueryselect textcol2 from user_unsharded where textcol2 in ( select textcol2 from user_unsharded ) order bynull-- ORDER BY RAND() after pull-out subqueryselect costly from user_unsharded where costly in ( select costly from user_unsharded ) order by rand()
-- Order by subquery columnselectu.namefrom user_unsharded u join ( select user_id from user_unsharded_extra where user_id =105 ) eu onu.name=eu.user_idwhereu.name=105order byeu.user_id-- scatter limit after pullout subqueryselect textcol2 from user_unsharded where textcol2 in ( select textcol2 from user_unsharded ) order by textcol2 limit1
UNION (分片、不分片)
--- union/sharded/union_cases.json-- union all between two scatter selectsselect id from user_extra union allselect id from user_metadata
-- union all between two SelectEqualUniqueselect id from user_extra where user_id =22union allselect id from user_metadata where user_id =55-- almost dereks query - two queries with order by and limit being scattered to two different sets of tablets
(select id, email from user_extra order by id asclimit1) union all (select id, email from user_metadata order by id desclimit1)
-- Union allselect id, email from user_extra union allselect id, email from user_metadata
-- union operations in subqueries (FROM)select*from (select id, email from user_extra union allselect id, email from user_metadata) as t
-- union all between two scatter selects, with order by
(select id from user_extra order by id limit5) union all (select id from user_extra order by id desclimit5)
-- union all on scatter and single routeselect id from user_extra where user_id =22unionselect id from user_extra where user_id =22union allselect id from user_extra
--- union/unsharded/union_cases.json-- union of information_schemaselect PLUGIN_NAME frominformation_schema.PLUGINSunionselect PLUGIN_NAME frominformation_schema.PLUGINS--select predef1, predef2 from ( select predef1, predef2 from user_unsharded where id =1unionselect predef1, predef2 from user_unsharded where id =3 ) a
--select id, name from user_unsharded where id in ( select id from user_unsharded where id =1unionselect id from user_unsharded where id =3 )
--
( select id from user_unsharded ) union ( select id from unsharded_auto ) order by id limit5--select id from user_unsharded unionselect id from unsharded_auto unionselect id from unsharded_auto where id in (101)
DML DELETE (分片、不分片)
--- dml_delete_sharded.json-- delete from, no owned vindexesdeletefrom music where user_id =1-- delete from with no where clausedeletefrom user_extra
-- delete from with no where clause & explicit keyspace referencedeletefrom :ks.user_extra-- delete with non-comparison exprdeletefrom user_extra where user_id between 1and2-- delete from with no index matchdeletefrom user_extra where email ='jose'-- delete from with primary id in through IN clausedeletefrom user_extra where user_id in (1, 2)
--- dml_delete_unsharded.json-- explicit keyspace referencedeletefrom :ks.unsharded-- delete unshardeddeletefrom unsharded
-- multi-table delete with comma joindelete a from unsharded_authoritative a, unsharded_auto b wherea.col1=b.idandb.val='aa'-- multi-table delete with comma join & explicit keyspace referencedelete a from :ks.unsharded_authoritative a, unsharded_auto b wherea.col1=b.idandb.val='aa'-- multi-table delete with ansi joindelete a from unsharded_authoritative a join unsharded_auto b ona.col1=b.idwhereb.val='aa'-- delete with join from subquerydelete foo from unsharded_authoritative as foo left join (select col1 from unsharded_authoritative where col2 is not nullorder by col2 desclimit10) as keepers onfoo.col1=keepers.col1wherekeepers.col1 is nullandfoo.col2is not nullandfoo.col2<1000-- unsharded delete where inner query references outer querydeletefrom unsharded_authoritative where col1 = (select id from unsharded_auto where id =unsharded_authoritative.col1)
-- delete from with no where clausedeletefrom user_unsharded_extra
-- delete with non-comparison exprdeletefrom user_unsharded_extra where user_id between 1and2-- delete from with no index matchdeletefrom user_unsharded_extra where extra_id =105-- delete from with primary id in through IN clausedeletefrom user_unsharded_extra where user_id in (1, 2)
DML INSERT (分片、不分片)
--- dml_insert_sharded.json-- insert no column list for sharded authoritative tableinsert into authoritative(user_id, col1, col2) values(1, 2, 3)
-- insert no column list for sharded authoritative tableinsert into authoritative(user_id, col1, col2) values(22,33,44),(333,444,555),(4444,5555,6666)
-- insert with one vindexinsert into user(id, name) values (null, 'aaa')
-- insert with one vindexinsert into user(id, name) values (100, 'bbb')
-- insert with one vindexinsert into user(id, name) values (null, 'ccc'),(null, 'ddd'),(null, 'eee')
-- insert with one vindexinsert into user(id, name) values (null, 'a'),(999, 'b'),(null, 'c')
-- insert with one vindexinsert into user(name) values ('aaaa')
-- insert with one vindexinsert into user(name) values ('aaaa'),('bbbb'),('cccc')
-- insert ignore shardedinsert ignore into user(id) values (1)
-- insert on duplicate keyinsert into user(id) values(1) on duplicate key update predef1 =11-- insert on duplicate keyinsert into user(id) values(1) on duplicate key update predef1 =111, predef2 =222-- insert with default seqinsert into user(id, name) values (default, 'ZS')
-- insert with one vindex and bind varinsert into user(id,name) values (null, 'name')
-- insert with non vindex bool valueinsert into user(predef1, textcol1) values (true, false)
-- replace sharded
replace into user(costly, textcol1, textcol2) values (123, 'textcol1', 'textcol2')
-- insert with one vindex and bind varinsert into user(id,name,textcol1,textcol2) values (null,'name','textcol1','textcol2'),(null,'name','textcol1','textcol2'),(null,'name','textcol1','textcol2'),(null,'name','textcol1','textcol2')
-- insert sharded pinnedinsert into pinned values (1, '1')
-- insert sharded pinnedinsert into pinned(id, name) values (2, '2')
-- insert sharded pinnedinsert into pinned(id) values (3)
-- insert sharded pinnedinsert into pinned(name) values ('4')
-- insert sharded pinnedinsert into pinned(id, name) values (NULL, '5')
-- insert sharded pinnedinsert into pinned(id, name) values (NULL, '6'),(NULL, '7'),(NULL, '8')
-- insert sharded pinnedinsert ignore into pinned(id) values (1) on duplicate key update name ='11'-- insert sharded pinned
replace into pinned(id, name) values (2, '22')
--- dml_insert_unsharded.json-- simple insert unshardedinsert into unsharded values(1, 2)
-- simple upsert unshardedinsert into unsharded values(1, 2) on duplicate key update predef2 =22-- unsharded insert, no col list with auto-inc and authoritative column listinsert into unsharded_authoritative values(11,22)
-- insert unsharded, column presentinsert into unsharded_auto(id, val) values(1, 'aa')
-- insert unsharded, column absentinsert into unsharded_auto(val) values('aa')
-- insert unsharded, column absentinsert into unsharded_auto(val) values(false)
-- insert unsharded, multi-valinsert into unsharded_auto(id, val) values(4, 'aa'), (null, 'bb')
-- simple replace unsharded
replace into unsharded values(1, 222)
-- simple replace unsharded
replace into unsharded values(3, 4)
-- simple replace unsharded
replace into unsharded values(1, 2)
-- replace unsharded, column present
replace into unsharded_auto(id, val) values(1, 'aa')
-- replace unsharded, column absent
replace into unsharded_auto(val) values('aa')
-- replace unsharded, multi-val
replace into unsharded_auto(id, val) values(5, 'aa'), (6, 'bb')
DML UPDATE (分片、不分片)
--- dml_update_sharded.json-- explicit keyspace referenceupdate :ks.engine_testset f_tinyint =1where f_key ='11'-- update by primary keyspace idupdate engine_test set f_tinyint =1where f_key ='11'-- update by primary keyspace id with aliasupdate engine_test as engine_test_alias set f_tinyint =1whereengine_test_alias.f_key='11'-- update by primary keyspace id with parenthesized expressionupdate engine_test set f_tinyint =1where (f_key ='11')
-- update by primary keyspace id with multi-part where clause with parensupdate engine_test set f_tinyint =1where (f_tinyint =0and f_key ='11')
-- update by primary keyspace id, changing one vindex column, using order by and limitupdate engine_test set f_tinyint =1where f_key ='11'order by f_key asclimit1-- update by primary keyspace id, stray where clauseupdate engine_test set f_tinyint =1where f_key = f_varchar and f_key ='11'-- update columns of multi column vindexupdate engine_test set f_tinyint =1, f_varchar ='22'where f_key ='11'-- update with no primary vindex on where clause (scatter update)update engine_test set f_tinyint =1-- update with non-comparison exprupdate engine_test set f_tinyint =1where f_key between '11'and'22'-- update with primary id through IN clauseupdate engine_test set f_tinyint =1where f_key in ('11', '22')
-- update with non-unique keyupdate engine_test set f_tinyint =1where f_varchar ='33'-- update with where clause with parensupdate engine_test set f_tinyint =1where (f_varchar ='11'or f_key ='11')
-- update vindex value to null with complex where clauseupdate engine_test set f_tinyint =1where f_key +11='22'--- dml_update_unsharded.json-- explicit keyspace referenceupdate :ks.unshardedset predef2 =1-- update unshardedupdate unsharded set predef2 =1-- update by primary keyspace idupdate unsharded set predef2 =1where predef1 =1-- update by primary keyspace id with aliasupdate unsharded as unsharded_alias set predef2 =1whereunsharded_alias.predef1=1-- update by primary keyspace id with parenthesized expressionupdate unsharded set predef2 =1where (predef1 =1)
-- subqueries in unsharded updateupdate unsharded set predef2 = (select col1 from unsharded_authoritative limit1)
-- unsharded union in subquery of unsharded updateupdate unsharded set predef2 = (select col1 from unsharded_authoritative unionselect col1 from unsharded_authoritative)
-- unsharded join in subquery of unsharded updateupdate unsharded set predef2 = (selecta.col1from unsharded_authoritative a join unsharded_authoritative b ona.col1=b.col1)
-- update with join subqueryupdate unsharded_authoritative as foo left join (select col1 from unsharded_authoritative where col2 is not nullorder by col2 desclimit10) as keepers onfoo.col1=keepers.col1setfoo.col1=100wherekeepers.col1 is nullandfoo.col2is not nullandfoo.col2<1000-- update by primary keyspace id with multi-part where clause with parensupdate unsharded set predef2 =1where (predef2 =0and predef1 =1)
-- update by primary keyspace id, changing same vindex twiceupdate unsharded set predef2 =0, predef2 =1where predef1 =1-- update by primary keyspace id, changing one vindex column, using order by and limitupdate unsharded set predef2 =1where predef2 =0order by predef1 asclimit1-- update by primary keyspace id, stray where clauseupdate unsharded set predef2 =3where predef1 = predef2 and predef1 =1-- update multi-table ansi joinupdate unsharded_authoritative a join unsharded_auto b ona.col1=b.idsetb.val='foo'whereb.val='aa'-- update multi-table comma joinupdate unsharded_authoritative a, unsharded_auto b setb.val='foo'wherea.col1=b.idandb.val='aa'-- update with non-comparison exprupdate unsharded set predef2 =1where predef1 between 1and2-- update with primary id through IN clauseupdate unsharded set predef2 =1where predef1 in (1, 2)
-- update with where clause with parensupdate unsharded set predef2 =1where (predef2 =2or predef1 =1)
-- unsharded update where inner query references outer queryupdate unsharded_authoritative set col1 = (select id from unsharded_auto where id =unsharded_authoritative.col1) where col1 = (select predef1 from unsharded)
-- update vindex value to null with complex where clauseupdate unsharded set predef2 =1where predef1 +1=2