对索引列的查询和排序问题的研究

--对索引列的查询和排序问题的研究
--1.创建表并插入模拟数据
create table test(id number,name varchar2(10));
insert into test values(1,'测试1');
insert into test values(2,'测试2');
insert into test values(5,'测试5');
insert into test values(4,'测试4');
insert into test values(3,'测试3');
insert into test values(6,'测试6');
insert into test values(9,'测试9');
insert into test values(8,'测试8');
insert into test values(7,'测试7');

commit;

--创建索引前:
--1.表的查询:表中的数据顺序为数据插入的顺序
SQL> select rownum,id from test where id<10;
 
    ROWNUM        ID
---------- ----------
        1
        2
        5
        4
        3
        6
        9
        8
        7
rows selected

--2.排序操作: 先从表中查数据,同时给每行加上一个行号,最后再进行排序
SQL> select rownum,id from test where id<10 order by id asc;
 
    ROWNUM        ID
---------- ----------
        1
        2
        3
        4
        5
        6
        7
        8
        9
rows selected

create index i_test_id on test(id);
--创建索引后:
--1.表的查询:从索引段中查找数据,排好序,再给每行加上行号
SQL> select rownum,id from test where id<10;
 
    ROWNUM        ID
---------- ----------
        1
        2
        3
        4
        5
        6
        7
        8
        9

--2.排序操作:找数据=> 排序 => 置行号
SQL> select rownum,id from test where id<10 order by id;
 
    ROWNUM        ID
---------- ----------
        1
        2
        3
        4
        5
        6
        7
        8
        9
rows selected
 
SQL> select rownum,id from test where id<10 order by id desc;
 
    ROWNUM        ID
---------- ----------
        9
        8
        7
        6
        5
        4
        3
        2
        1

猜你喜欢

转载自www.linuxidc.com/Linux/2017-12/149250.htm