/*
 * 比较相邻数据保存在相同数据块和不同数据块查询速度
 */
prompt
prompt 创建表eoda.colocated
declare
	row_count number(5);
begin
	select count(1) into row_count from user_tables where table_name = upper('colocated');
	if row_count = 1 then
		execute immediate 'drop table eoda.colocated ';
	end if;
	execute immediate '
		create table eoda.colocated
		(
			x int,
			y varchar2(80)
		)
	';
end;
/

prompt
prompt 插入数据eoda.colocated
declare
	row_count number(5);
begin
	select count(1) into row_count from user_tables where table_name = upper('colocated');
	if row_count = 1 then
		for i in 1 .. 100000
		loop
			insert into eoda.colocated ( x,y )
			values ( i,rpad(dbms_random.random,75,'*') );
		end loop;
		commit;
		execute immediate '
			alter table eoda.colocated add constraint colocated_pk primary key(x)
		';
		dbms_stats.gather_table_stats( user, 'COLOCATED' );
	end if;
end;
/

prompt
prompt  创建表eoda.disorganized
declare
	row_count number(5);
begin
	select count(1) into row_count from user_tables where table_name = upper('disorganized');
	if row_count = 1 then
		execute immediate 'drop table eoda.disorganized';
	end if;
	execute immediate '
		create table eoda.disorganized
		as
		select x,y
		from eoda.colocated
		order by y
	';
	execute immediate '
		alter table eoda.disorganized add constraint disorganized_pk primary key(x)
	';
	dbms_stats.gather_table_stats( user,'DISORGANIZED' );
end;
/

select *
from eoda.colocated
where x between 20000 and 40000;
/

select /*+ index( eoda.disorganized disorganized_pk  ) */ *
from eoda.disorganized
where x between 20000 and 40000;
/

select a.index_name,
	   b.num_rows,
	   b.blocks,
	   a.clustering_factor
from user_indexes a, user_tables b
where a.index_name in ( 'COLOCATED_PK','DISORGANIZED_PK' )
and a.table_name = b.table_name;
/
