分布键join+limit优化 本页介绍天翼云TeleDB数据库通过分布键join+limit优化来提高性能的优化案例。 1. 测试数据准备: teledb CREATE TABLE t1(f1 serial not null unique,f2 text,f3 text,f4 text,f5 text,f6 text,f7 text,f8 text,f9 text,f10 text,f11 text,f12 text) distribute by shard(f1); CREATE TABLE teledb CREATE TABLE t2(f1 serial not null unique,f2 text,f3 text,f4 text,f5 text,f6 text,f7 text,f8 text,f9 text,f10 text,f11 text,f12 text) distribute by shard(f1); CREATE TABLE teledb insert into t1 select t,md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text) from generateseries(1,1000000) as t; INSERT 0 1000000 teledb insert into t2 select t,md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text) from generateseries(1,1000000) as t; INSERT 0 1000000 teledb analyze t1; ANALYZE teledb analyze t2; ANALYZE teledb teledb timing Timing is on. 2. 优化前SQL执行计划和耗时: 此处是将preferolap参数设置为off的场景,该参数默认为on。 teledb explain select t1. from t1,t2 where t1.f1t2.f1 limit 10; QUERY PLAN Limit (cost0.25..1.65 rows10 width367) > Merge Join (cost0.25..140446.26 rows1000000 width367) Merge Cond: (t1.f1 t2.f1) > Remote Subquery Scan on all (dn01,dn02,dn03,dn04,dn05,dn06,dn07,dn08,dn09,dn10) (cost100.12..434823.13 rows1000000 width367) > Index Scan using t1f1key on t1 (cost0.12..62723.13 rows1000000 width367) > Remote Subquery Scan on all (dn01,dn02,dn03,dn04,dn05,dn06,dn07,dn08,dn09,dn10) (cost100.12..71823.13 rows1000000 width4) > Index Only Scan using t2f1key on t2 (cost0.12..62723.13 rows1000000 width4) (7 rows) Time: 1.372 ms teledb