首页 > 数据库 >SQL分页优化五 like+非等值+order by分页

SQL分页优化五 like+非等值+order by分页

时间:2023-08-05 16:01:15浏览次数:32  
标签:10 00 分页 id SQL 00.01 order select 14

测试验证

如下SQL:

select * 
 from (select * 
         from (select a.*, rownum rn 
                 from (select 
                        * 
                         from test 
                        where owner like  'SYS%' 
                          and object_id > 1000 
                        order by object_name) a) 
        where rownum <= 10) 
where rn >= 1;

有like模糊过滤条件,有非等值过滤条件,有排序。
创建索引:
测试1:

SQL> create index idx_name_id on test(object_name,object_id);

Index created.

SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID  6h95n2pg07f77, child number 0
-------------------------------------
select *   from (select *           from (select a.*, rownum rn
          from (select /*+index(test idx_name_id)*/
      *                           from test
where owner like  'SYS%'                            and object_id >
1000                          order by object_name) a)          where
rownum <= 10)  where rn >= 1

Plan hash value: 1798652263

------------------------------------------------------------------------------------------------------------------
| Id  | Operation                        | Name        | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  |
------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                 |             |      1 |        |     10 |00:00:00.01 |      19 |      5 |
|*  1 |  VIEW                            |             |      1 |     10 |     10 |00:00:00.01 |      19 |      5 |
|*  2 |   COUNT STOPKEY                  |             |      1 |        |     10 |00:00:00.01 |      19 |      5 |
|   3 |    VIEW                          |             |      1 |  35003 |     10 |00:00:00.01 |      19 |      5 |
|   4 |     COUNT                        |             |      1 |        |     10 |00:00:00.01 |      19 |      5 |
|   5 |      VIEW                        |             |      1 |  35003 |     10 |00:00:00.01 |      19 |      5 |
|*  6 |       TABLE ACCESS BY INDEX ROWID| TEST        |      1 |  35003 |     10 |00:00:00.01 |      19 |      5 |
|*  7 |        INDEX FULL SCAN           | IDX_NAME_ID |      1 |  72571 |     23 |00:00:00.01 |       4 |      5 |
------------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("RN">=1)
   2 - filter(ROWNUM<=10)
   6 - filter("OWNER" LIKE 'SYS%')
   7 - access("OBJECT_ID">1000)
       filter("OBJECT_ID">1000)

测试2:

SQL> create index idx_name_id_owner on test(object_name,object_id,owner);

Index created.


SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));


PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID  bs0qhcj9p45hp, child number 0
-------------------------------------
select *   from (select *           from (select a.*, rownum rn
          from (select /*+index(test idx_name_id_owner)*/
           *                           from test
    where owner like  'SYS%'                            and object_id >
1000                          order by object_name) a)          where
rownum <= 10)  where rn >= 1

Plan hash value: 810954641

-----------------------------------------------------------------------------------------------------------------------
| Id  | Operation                        | Name              | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  |
-----------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                 |                   |      1 |        |     10 |00:00:00.01 |      14 |      5 |
|*  1 |  VIEW                            |                   |      1 |     10 |     10 |00:00:00.01 |      14 |      5 |
|*  2 |   COUNT STOPKEY                  |                   |      1 |        |     10 |00:00:00.01 |      14 |      5 |
|   3 |    VIEW                          |                   |      1 |  35003 |     10 |00:00:00.01 |      14 |      5 |
|   4 |     COUNT                        |                   |      1 |        |     10 |00:00:00.01 |      14 |      5 |
|   5 |      VIEW                        |                   |      1 |  35003 |     10 |00:00:00.01 |      14 |      5 |
|   6 |       TABLE ACCESS BY INDEX ROWID| TEST              |      1 |  35003 |     10 |00:00:00.01 |      14 |      5 |
|*  7 |        INDEX FULL SCAN           | IDX_NAME_ID_OWNER |      1 |  35003 |     10 |00:00:00.01 |       4 |      5 |
-----------------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("RN">=1)
   2 - filter(ROWNUM<=10)
   7 - access("OBJECT_ID">1000 AND "OWNER" LIKE 'SYS%')
       filter(("OWNER" LIKE 'SYS%' AND "OBJECT_ID">1000))

测试3:

SQL> create index idx_name_owner_id on test(object_name,owner,object_id);

Index created.

SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));


PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID  du0qd7phsxnc6, child number 0
-------------------------------------
select *   from (select *           from (select a.*, rownum rn
          from (select /*+index(test idx_name_owner_id)*/
           *                           from test
    where owner like  'SYS%'                            and object_id >
1000                          order by object_name) a)          where
rownum <= 10)  where rn >= 1

Plan hash value: 1258538393

-----------------------------------------------------------------------------------------------------------------------
| Id  | Operation                        | Name              | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  |
-----------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                 |                   |      1 |        |     10 |00:00:00.01 |      14 |      2 |
|*  1 |  VIEW                            |                   |      1 |     10 |     10 |00:00:00.01 |      14 |      2 |
|*  2 |   COUNT STOPKEY                  |                   |      1 |        |     10 |00:00:00.01 |      14 |      2 |
|   3 |    VIEW                          |                   |      1 |  35003 |     10 |00:00:00.01 |      14 |      2 |
|   4 |     COUNT                        |                   |      1 |        |     10 |00:00:00.01 |      14 |      2 |
|   5 |      VIEW                        |                   |      1 |  35003 |     10 |00:00:00.01 |      14 |      2 |
|   6 |       TABLE ACCESS BY INDEX ROWID| TEST              |      1 |  35003 |     10 |00:00:00.01 |      14 |      2 |
|*  7 |        INDEX FULL SCAN           | IDX_NAME_OWNER_ID |      1 |  35003 |     10 |00:00:00.01 |       4 |      2 |
-----------------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("RN">=1)
   2 - filter(ROWNUM<=10)
   7 - access("OWNER" LIKE 'SYS%' AND "OBJECT_ID">1000)
       filter(("OWNER" LIKE 'SYS%' AND "OBJECT_ID">1000))

从上面三个执行计划可以看到,当过滤条件中有like时,非等值过滤,排序时,应该将排序列放在索引最前面,like和非等值过滤列放后面,二者之间顺序可以互换。

标签:10,00,分页,id,SQL,00.01,order,select,14
From: https://blog.51cto.com/u_13482808/6976303

相关文章

  • openGauss的SQL引擎在3.1.0版本中做了哪些优化?
    openGauss的SQL引擎在3.1.0版本中做了哪些优化?收录于合集#技术干货83个查询执行能力对数据库来说至关重要,这直接决定了查询语句生成的执行计划以何种方式进行执行,如果哪个执行算子的执行表现不好,将会对数据库的整体性能产生极大的影响。同时,执行算子的实现也极大考验一款数据库的工......
  • 数据库迁移系列】从MySQL到openGauss的数据库对象迁移实践
    数据库迁移系列】从MySQL到openGauss的数据库对象迁移实践原创酷哥[openGauss](javascript:void(0);)2022-11-0718:03发表于广东9月30日新发布的openGauss3.1.0版本,工具的全量迁移和增量迁移的性能不但有了全面提升,而且支持数据库对象视图、触发器、自定义函数、存储过程的......
  • SQL-去除最大值与最小值求均值的问题
    背景今天有同事问我一道关于数据库SQL的面试题,我刚开始随便给了一个思路,后来思索发现这个思路有漏洞,于是总结下来,仅供参考。问题:薪水表中是员工薪水的基本信息,包括雇员编号,和薪水,查询除去最高、最低薪水后的平均薪水。一、薪水表信息CREATETABLE`salaries`(`emp_n......
  • Docker-Compose 一键部署mysql及初始化sql脚本
    1.部署前提:服务器具备docker和docker-compose环境2.镜像准备:-mysql:DockerFile文件:FROMmysql:5.7.41COPY*.sql/docker-entrypoint-initdb.d/初始化sql脚本xxx.sql放在DockerFile文件同级目录下执行指令:dockerbuild-fdockerfile_mysql-tmysql:1.0.0.......
  • Sqlmap的使用
    1、get型提交sqlmap.py-u+网址sqlmap.py-uhttp://127.0.0.1/sqli/Less-1/?id=1参数levellevel的等级越高,测试的等级越广,>=2时会检查cookie里的参数>=3时检查user-agent和refereer--dbs              查询所有的数据库sqlmap.py-uhttp://127.0.0.1/sql......
  • 老派Sql之必要,逆天,我在ef core中使用ado.net!
    Wlkr.Core.EFCore逆天,我在efcore中使用ado.net!老派Sql之必要当你开发生涯中基本只用一两种数据库当你觉得用EF的类写报表时很别扭当你觉自己的Sql(Server)语句写得出神入化当你觉自己的Sql(Server)语句比EF生成的更优化当你刚从.netframework转.netcore,还不知道sqls......
  • MySql之锁
    MySql之锁一、全局锁对整个数据库加锁应用:数据库所有表备份二、表级锁1.表锁分为两类:表共享读锁readlock表独占写锁writelock2.元数据锁避免DML语句和DDL语句的冲突问题3.意向锁:作用:针对行锁和表锁,当表中有行锁时,会同时加一把意向锁,表锁不用去逐行遍历是否加了行锁,......
  • 最简单的Qt连接MYSQL的方法
    最简单的Qt连接MYSQL的方法⭐当我试图在项目中连接本地的mysql时,反复出现:QMYSQLdrivernotloaded,显示没有成功加载mysql的驱动,在网上查询了很多教程和视频,大多为互相转载且老旧,耗费了大半天还是没有构建成功,通常的解决方法是在本地构建mysql驱动(通过安装qt时勾选的src选项里......
  • MySQL之InnoDB存储结构 转载 https://juejin.cn/post/7253816086679846972
    1InnoDB存储引擎InnoDB存储引擎最早由InnobaseOy公司开发(属第三方存储引擎)。从MySQL5.5版本开始作为表的默认存储引擎。该存储引擎是第一个完整支持ACID事务的MySQL存储引擎,特点是行锁设计、支持MVCC、支持外键、提供一致性非锁定读,非常适合OLTP场景的应用使用。目前也是应用......
  • MySQL查询排序和分页
    连接数据库mysql-hlocalhost-uroot-proot排序查询语法:select字段列表from表名orderby字段1排序方式1,字段3排序方式2,字段3排序方式3,....;ASC:升序(默认值)DESC降序注意:如果是多字段排序,当第一个字段值相同时,才会根据第二个字段进行排序。根据年龄对公司......