`

有关Sql order by 优化查询的建议

 
阅读更多
在使用左,右,或者内连接的时候,在需要使用排序的时候,不妨先以一个表为标准,先进行排序,这样可以提供语句的性能
比如:
   优化前:
      SELECT *
  FROM (SELECT a.*, ROW_NUMBER() OVER(ORDER BY createDate desc) row_num
          FROM (SELECT a.ID as id,
                       a.CODE as code,
                       a.LAST_STORE_JOB_ID as lastStoreJobId,
                       a.JOB_DATE as jobDate,
                       a.JOB_STORE_ID as jobStoreId,
                       c.NAME as jobStoreName,
                       a.TRANSFER_STORE_ID as transferStoreId,
                       d.NAME as transferStoreName,
                       a.STORE_COMPANY_ID as storeCompanyId,
                       e.COMPANY_FULLNAME as companyName,
                       a.EXTRACT_COMPANY_ID as extractCompanyId,
                       f.COMPANY_FULLNAME as extractCompanyName,
                       a.SOURCE_TYPE as sourceType,
                       a.EXTERNAL_CODE as externalCode,
                       a.REMARK as remark,
                       a.ALLOT_TYPE as allotType,
                       a.ALLOT_STATUS as allotStatus,
                       a.STATUS as status,
                       a.PDA_STATUS as pdaStatus,
                       a.JOB_TYPE_ID as jobTypeId,
                       a.ACTION_TYPE_ID as actionTypeId,
                       a.SOLVE_STATUS as solveStatus,
                       a.UPDATE_DATE as updateDate,
                       a.CREATE_DATE as createDate,
                       a.VEHICLE_CODE as vehicleCode,
                       a.USER_ID as userId,
                       g.USER_NAME as userName,
                       a.FLOW_STATUS as flowStatus,
                       a.TARGET_ALLOCATION_ID as targetAllocationId,
                       h.NAME as allocationName,
                       (select count(*)
                          FROM STORE_JOB_FLOW_NODES i
                          left join STORE_JOB_FLOW_MODES j
                            on j.id = i.JOB_FLOW_MODE_ID
                           and i.ACTION_TYPE_ID = 'HWMAN02'
                          left join STORE_JOB_FLOWS k
                            on k.job_flow_mode_id = j.id
                         where k.STORE_JOB_ID = a.ID) as nextIsPlan
                  FROM  STORE_JOBS  a                  LEFT JOIN STORE_JOBS b
                    on a.last_store_job_id = b.id
                  LEFT JOIN STORES c
                    on a.job_store_id = c.id
                  LEFT JOIN STORES d
                    on a.transfer_store_id = d.id
                  LEFT JOIN COMPANYS e
                    on a.store_company_id = e.id
                  LEFT JOIN COMPANYS f
                    on a.extract_company_id = f.id
                  LEFT JOIN USERS g
                    on a.user_id = g.id
                  LEFT JOIN ALLOCATIONS h
                    on a.target_allocation_id = h.id
                 where 1 = 1
                   AND a.ACTION_TYPE_ID = 'HWMAN01'
                   AND a.JOB_DATE between
                       to_date('2014-07-02 00:00:00', 'yyyy-mm-dd hh24:mi:ss') and
                       to_date('2014-08-02 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                   AND a.JOB_TYPE_ID = 'HWMSN02'
                  order by a.CREATE_DATE desc ) a
                   )
WHERE row_num BETWEEN 1 AND 30
ORDER BY createDate desc

优化后:
     SELECT *
  FROM (SELECT a.*, ROW_NUMBER() OVER(ORDER BY createDate desc) row_num
          FROM (SELECT a.ID as id,
                       a.CODE as code,
                       a.LAST_STORE_JOB_ID as lastStoreJobId,
                       a.JOB_DATE as jobDate,
                       a.JOB_STORE_ID as jobStoreId,
                       c.NAME as jobStoreName,
                       a.TRANSFER_STORE_ID as transferStoreId,
                       d.NAME as transferStoreName,
                       a.STORE_COMPANY_ID as storeCompanyId,
                       e.COMPANY_FULLNAME as companyName,
                       a.EXTRACT_COMPANY_ID as extractCompanyId,
                       f.COMPANY_FULLNAME as extractCompanyName,
                       a.SOURCE_TYPE as sourceType,
                       a.EXTERNAL_CODE as externalCode,
                       a.REMARK as remark,
                       a.ALLOT_TYPE as allotType,
                       a.ALLOT_STATUS as allotStatus,
                       a.STATUS as status,
                       a.PDA_STATUS as pdaStatus,
                       a.JOB_TYPE_ID as jobTypeId,
                       a.ACTION_TYPE_ID as actionTypeId,
                       a.SOLVE_STATUS as solveStatus,
                       a.UPDATE_DATE as updateDate,
                       a.CREATE_DATE as createDate,
                       a.VEHICLE_CODE as vehicleCode,
                       a.USER_ID as userId,
                       g.USER_NAME as userName,
                       a.FLOW_STATUS as flowStatus,
                       a.TARGET_ALLOCATION_ID as targetAllocationId,
                       h.NAME as allocationName,
                       (select count(*)
                          FROM STORE_JOB_FLOW_NODES i
                          left join STORE_JOB_FLOW_MODES j
                            on j.id = i.JOB_FLOW_MODE_ID
                           and i.ACTION_TYPE_ID = 'HWMAN02'
                          left join STORE_JOB_FLOWS k
                            on k.job_flow_mode_id = j.id
                         where k.STORE_JOB_ID = a.ID) as nextIsPlan
                  FROM (SELECT * FROM STORE_JOBS order by CREATE_DATE desc) a                  LEFT JOIN STORE_JOBS b
                    on a.last_store_job_id = b.id
                  LEFT JOIN STORES c
                    on a.job_store_id = c.id
                  LEFT JOIN STORES d
                    on a.transfer_store_id = d.id
                  LEFT JOIN COMPANYS e
                    on a.store_company_id = e.id
                  LEFT JOIN COMPANYS f
                    on a.extract_company_id = f.id
                  LEFT JOIN USERS g
                    on a.user_id = g.id
                  LEFT JOIN ALLOCATIONS h
                    on a.target_allocation_id = h.id
                 where 1 = 1
                   AND a.ACTION_TYPE_ID = 'HWMAN01'
                   AND a.JOB_DATE between
                       to_date('2014-07-02 00:00:00', 'yyyy-mm-dd hh24:mi:ss') and
                       to_date('2014-08-02 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                   AND a.JOB_TYPE_ID = 'HWMSN02') a)
WHERE row_num BETWEEN 1 AND 30
ORDER BY createDate desc

分享到:
评论

相关推荐

    sql查询优化总结

    SQL查询优化是数据库管理中的一项重要任务,旨在提高查询效率,减少资源消耗,尤其是在处理大量数据时。本文将深入探讨几个关键的优化策略。 首先,影响SQL性能的主要原因之一是对大结果集进行高成本操作,例如排序...

    SQL Server SQL优化

    在使用`GROUP BY`、`DISTINCT`和`ORDER BY`等操作时,应当尽可能地利用索引来加速执行。 - **推荐做法**:使用`UNION ALL`代替`UNION`以提高合并效率,如`SELECT * FROM A WITH (NOLOCK) WHERE Verify = 3 UNION ...

    SQL百万级数据库优化大全

    例如,在`WHERE`子句和`ORDER BY`子句中使用的字段上创建索引可以显著提高查询效率。 - **案例**:假设有一个表`t`,其中包含大量记录,如果执行如下查询:`SELECT * FROM t WHERE num IS NULL`,由于`NULL`值的存在...

    VB中SQL语言查询优化设计.pdf

    VB中SQL语言查询优化设计 SQL语言是数据库管理系统中最重要的组成部分之一,它提供了对数据库的访问、管理和维护功能。VB开发工具中,SQL语言查询优化设计是非常重要的,能够提高数据库的访问效率和性能。本文将从...

    sql语句优化,提高查询性能

    SQL语句优化是数据库性能提升的关键技术之一,尤其在处理大量数据时,高效的SQL语句能够显著提高查询速度,减少系统资源的消耗。以下是一些关于SQL语句优化的重要原则和技巧: 1. **限制性条件的位置**:在`WHERE`...

    SQL.rar_java sql_java sql查询_query optimization_sql_sql优化

    在IT行业中,数据库管理和SQL(Structured Query Language)查询优化是至关重要的技能,特别是在Java应用程序开发中。本篇文章将深入探讨SQL查询优化,包括其概念、重要性以及如何在Java环境中进行有效的SQL调优。 ...

    sql书写优化和性能优化

    ### SQL书写优化与性能优化详解 #### 一、书写风格 **1. SQL语句全部使用小写** 为了保持代码一致性与可读性,所有SQL语句均需使用小写字母编写。这一规则适用于所有的关键字、函数名称及操作符等。 **2. 引用...

    SQL语句优化原则.pdf

    为提升性能,应重新编写ORDER BY语句以使用索引,或者为相关列建立额外索引,并避免在ORDER BY子句中使用表达式。 在编写SQL语句时,需要清楚优化器是根据何种原则来决定是否使用索引的。这样,我们就能更有针对性...

    SQL语句优化手册

    ORDER BY disk_reads DESC; ``` - **定位阻塞的会话**:有时候,某些用户的操作可能会导致其他用户的会话被阻塞。可以通过查询`v$session`、`v$lock`和`v$sqltext`视图来定位这些阻塞的会话,并采取相应措施解决...

    sql大数据量查询的优化技巧

    1. **建立索引**:索引可以显著提高查询速度,尤其是在WHERE子句和ORDER BY子句涉及的列上。创建合适的索引可以避免全表扫描,提高查询效率。 2. **避免NULL值判断**:在WHERE子句中检查NULL可能导致全表扫描。为了...

    sql 查询语句优化

    ### SQL 查询语句优化 #### 一、引言 在当今大数据时代,高效的数据处理能力对于任何企业都至关重要。SQL作为关系型数据库的标准查询语言,其性能直接影响着系统的响应时间和资源消耗。尤其对于大型数据库系统而言...

    MySQL数据库优化SQL篇PPT课件.pptx

    从执行计划、SELECT语句、IN和EXIST语句、LIMIT语句、RAND函数、Order by、Group by、Distinct和Count等方面对MySQL数据库优化进行了详细的讲解。 一、执行计划 执行计划是MySQL数据库优化的重要步骤。执行计划...

    sql基础与优化吐血整理

    SQL优化主要涉及如何更有效地执行查询,减少资源消耗和提高查询速度。理解MySQL查询过程有助于优化: 1. 连接器:处理用户身份验证和权限,确保只有授权用户能访问数据库。 2. 查询缓存:虽然MySQL 8.0已移除此...

    sql语句的优化

    1. **建立合适的索引**:在WHERE子句和ORDER BY子句中涉及到的列上建立索引,可以极大提高查询速度。例如,在执行如下查询时: ```sql SELECT id FROM t WHERE num = 10; ``` 如果`num`列上有索引,则可以快速...

    Oracle数据库中ORDER BY排序和查询按IN条件的顺序输出

    本文将深入探讨`ORDER BY`子句在Oracle中的排序机制,以及查询时根据`IN`条件顺序输出的方法。 首先,`ORDER BY`用于指定查询结果的排序方式,可以基于一个或多个列进行升序(ASC)或降序(DESC)排序。然而,...

    Microsoft_SQL_Server_2005技术内幕:T-SQL查询.pdf

    本书及其续篇——《Microsoft SQL Server 2005技术内幕:T-SQL程序设计》介绍了SQL Server 2005中高级T-SQL查询、查询优化及编程相关的知识。这两本书侧重于解决实践中的常见问题,并讨论了解决这些问题的方法。它们...

    浅谈SQL的查询优化技术.pdf

    本文讨论了查询优化技术的必要性和重要性,并介绍了索引的使用、相关子查询、ORDER BY 语句的使用、临时表的使用和避免困难的正规表达式等查询优化技术。掌握这些技术可以提高系统的可用性和响应速度。

    sql学习 索引特性之有序优化order by.sql

    sql学习 索引特性之有序优化order by.sql

    最新整理的常用sql语句及优化大全

    更高级的查询包括`WHERE`子句来过滤结果,`GROUP BY`进行分组,`ORDER BY`进行排序,以及`HAVING`来对分组后的数据进行过滤。 2. **数据插入**:`INSERT INTO`语句用于向表中添加新的记录,如`INSERT INTO table_...

    SQL语法优化大全

    在"sqlserver2000经典脚本"这个压缩包中,很可能包含了SQL Server 2000的常用脚本和示例,包括但不限于创建表、索引、视图、触发器、存储过程等,以及性能优化相关的查询和脚本。通过学习和实践这些脚本,可以深入...

Global site tag (gtag.js) - Google Analytics