- 浏览: 91456 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (99)
- JAVA (11)
- oracle (11)
- Hibernate (10)
- Utils (7)
- Struts (6)
- SQL (6)
- spring (6)
- Jquery (9)
- Exception (3)
- tomcat (3)
- SVN (1)
- Ajax (2)
- DOS (1)
- windows (1)
- Interview (1)
- Url (2)
- Jstl (1)
- Junit (1)
- PDI (1)
- JSR303 (0)
- BlogUrl (1)
- Maven (2)
- Date Plugin (0)
- Camera (1)
- 笔记 (1)
- doubleball (1)
- CSS (1)
- SSH (1)
- Mybatis (1)
- spring security (0)
- 参考书 (1)
- 在线软件 (1)
- VMware (1)
最新评论
在使用左,右,或者内连接的时候,在需要使用排序的时候,不妨先以一个表为标准,先进行排序,这样可以提供语句的性能
比如:
优化前:
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
比如:
优化前:
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
发表评论
-
如果没有就Insert,如果有就根据唯一索引更新
2015-05-18 10:25 4011.建立表结构 CREATE TABLE Test_Table ... -
oracle sql 指定记录 作为第一条
2015-02-10 17:43 5601)查询语句 select id,rank fro ... -
Oracle exists 用法 和 My Sql limit offset的用法
2015-02-03 10:34 789Oracle: 1.先执行子查询,再执行主查询。在执行子 ... -
树性结构实例
2015-02-02 10:29 793建表: create table address( ... -
Sql行列转换
2015-01-30 17:03 616name subject ... -
存储过程(二)案例
2015-01-22 15:42 601(1) Create or Replace Packag ... -
存储过程(一)游标
2015-01-22 14:25 6691. 概念 游标(Cursor)它使户可逐行访问由S ... -
数据库解锁
2014-12-24 17:12 493(1)查出锁定的Sesson_id select ses ... -
Oracel function instance
2014-09-01 15:39 6231 if end if语句的function实例 crea ... -
游标实例
2014-08-19 10:41 491PL/SQL 会为程序中执行的每一条UPDATE,DELETE ... -
树形结构查询
2014-07-29 19:24 674select id, name from (select ... -
left join
2014-06-30 15:05 488--- 库存结存查询记录数,总数量和总重量Sql: selec ... -
ORA-28000: ORACLE账户锁定的解决办法
2014-04-04 11:35 2300ORA-28000: ORACLE账户锁定的解决办法 ORA- ... -
MySQL、SqlServer、Oracle三大主流数据库分页查询
2014-03-06 21:33 495MySQL、SqlServer、Oracle三大主流数据库分页 ... -
sql经典语句
2014-02-21 11:16 419查询某张表中某些列一 ...
相关推荐
SQL查询优化是数据库管理中的一项重要任务,旨在提高查询效率,减少资源消耗,尤其是在处理大量数据时。本文将深入探讨几个关键的优化策略。 首先,影响SQL性能的主要原因之一是对大结果集进行高成本操作,例如排序...
在使用`GROUP BY`、`DISTINCT`和`ORDER BY`等操作时,应当尽可能地利用索引来加速执行。 - **推荐做法**:使用`UNION ALL`代替`UNION`以提高合并效率,如`SELECT * FROM A WITH (NOLOCK) WHERE Verify = 3 UNION ...
例如,在`WHERE`子句和`ORDER BY`子句中使用的字段上创建索引可以显著提高查询效率。 - **案例**:假设有一个表`t`,其中包含大量记录,如果执行如下查询:`SELECT * FROM t WHERE num IS NULL`,由于`NULL`值的存在...
VB中SQL语言查询优化设计 SQL语言是数据库管理系统中最重要的组成部分之一,它提供了对数据库的访问、管理和维护功能。VB开发工具中,SQL语言查询优化设计是非常重要的,能够提高数据库的访问效率和性能。本文将从...
SQL语句优化是数据库性能提升的关键技术之一,尤其在处理大量数据时,高效的SQL语句能够显著提高查询速度,减少系统资源的消耗。以下是一些关于SQL语句优化的重要原则和技巧: 1. **限制性条件的位置**:在`WHERE`...
在IT行业中,数据库管理和SQL(Structured Query Language)查询优化是至关重要的技能,特别是在Java应用程序开发中。本篇文章将深入探讨SQL查询优化,包括其概念、重要性以及如何在Java环境中进行有效的SQL调优。 ...
### SQL书写优化与性能优化详解 #### 一、书写风格 **1. SQL语句全部使用小写** 为了保持代码一致性与可读性,所有SQL语句均需使用小写字母编写。这一规则适用于所有的关键字、函数名称及操作符等。 **2. 引用...
为提升性能,应重新编写ORDER BY语句以使用索引,或者为相关列建立额外索引,并避免在ORDER BY子句中使用表达式。 在编写SQL语句时,需要清楚优化器是根据何种原则来决定是否使用索引的。这样,我们就能更有针对性...
ORDER BY disk_reads DESC; ``` - **定位阻塞的会话**:有时候,某些用户的操作可能会导致其他用户的会话被阻塞。可以通过查询`v$session`、`v$lock`和`v$sqltext`视图来定位这些阻塞的会话,并采取相应措施解决...
1. **建立索引**:索引可以显著提高查询速度,尤其是在WHERE子句和ORDER BY子句涉及的列上。创建合适的索引可以避免全表扫描,提高查询效率。 2. **避免NULL值判断**:在WHERE子句中检查NULL可能导致全表扫描。为了...
### SQL 查询语句优化 #### 一、引言 在当今大数据时代,高效的数据处理能力对于任何企业都至关重要。SQL作为关系型数据库的标准查询语言,其性能直接影响着系统的响应时间和资源消耗。尤其对于大型数据库系统而言...
从执行计划、SELECT语句、IN和EXIST语句、LIMIT语句、RAND函数、Order by、Group by、Distinct和Count等方面对MySQL数据库优化进行了详细的讲解。 一、执行计划 执行计划是MySQL数据库优化的重要步骤。执行计划...
SQL优化主要涉及如何更有效地执行查询,减少资源消耗和提高查询速度。理解MySQL查询过程有助于优化: 1. 连接器:处理用户身份验证和权限,确保只有授权用户能访问数据库。 2. 查询缓存:虽然MySQL 8.0已移除此...
1. **建立合适的索引**:在WHERE子句和ORDER BY子句中涉及到的列上建立索引,可以极大提高查询速度。例如,在执行如下查询时: ```sql SELECT id FROM t WHERE num = 10; ``` 如果`num`列上有索引,则可以快速...
本文将深入探讨`ORDER BY`子句在Oracle中的排序机制,以及查询时根据`IN`条件顺序输出的方法。 首先,`ORDER BY`用于指定查询结果的排序方式,可以基于一个或多个列进行升序(ASC)或降序(DESC)排序。然而,...
本书及其续篇——《Microsoft SQL Server 2005技术内幕:T-SQL程序设计》介绍了SQL Server 2005中高级T-SQL查询、查询优化及编程相关的知识。这两本书侧重于解决实践中的常见问题,并讨论了解决这些问题的方法。它们...
本文讨论了查询优化技术的必要性和重要性,并介绍了索引的使用、相关子查询、ORDER BY 语句的使用、临时表的使用和避免困难的正规表达式等查询优化技术。掌握这些技术可以提高系统的可用性和响应速度。
sql学习 索引特性之有序优化order by.sql
更高级的查询包括`WHERE`子句来过滤结果,`GROUP BY`进行分组,`ORDER BY`进行排序,以及`HAVING`来对分组后的数据进行过滤。 2. **数据插入**:`INSERT INTO`语句用于向表中添加新的记录,如`INSERT INTO table_...
在"sqlserver2000经典脚本"这个压缩包中,很可能包含了SQL Server 2000的常用脚本和示例,包括但不限于创建表、索引、视图、触发器、存储过程等,以及性能优化相关的查询和脚本。通过学习和实践这些脚本,可以深入...