索引分析
求出属于test02的所有test01的记录, 下面那种查询速度更快。
表
create table test01(seq int, test01_id uuid primary key);
create table test02(seq int, test02_id uuid primary key);
create table test01_test02_map(seq int, test01_id uuid, test02_id uuid, primary key(test02_id, test01_id));
test01 -> x records
test01_test02_map 0> y records
test02 -> z records
查询语句一
select * from test01 t1, test01_test02_map tm
where t1.test01_id = tm.test01_id and tm.test02_id in
('7bd0ee4d-6303-4d55-b329-8c952ea01fcb','3ac841a9-7840-440c-a7d5-4f2a0403126b','0992a0aa-4e18-4d57-9d5e-b3bd20425546');
执行计划
执行时间分析
索引是B-Tree索引
所以execute time = mlg(x) m 是 输入的test02_id的数目。
假设 x = 10000000, y=10000000, m = 10
mlg(x*y) = 230
查询语句二
select * from test01 t1 where t1.test01_id in
('7bd0ee4d-6303-4d55-b329-8c952ea01fcb','3ac841a9-7840-440c-a7d5-4f2a0403126b','0992a0aa-4e18-4d57-9d5e-b3bd20425546');
执行计划
select * from test01_test02_map tm where tm.test02_id in
('7bd0ee4d-6303-4d55-b329-8c952ea01fcb','3ac841a9-7840-440c-a7d5-4f2a0403126b','0992a0aa-4e18-4d57-9d5e-b3bd20425546');
执行计划
x = 10000000, y=10000000, m = 50, n=10
执行时间 = m lgx + nlgz = 50*23 + 10*23 = 1380