求一个SQL 语句的优化,目前速度是26秒。结果集里有3866条
SELECT scheduleGroup.e_description as x0_0_, tmsEmployee.e_employee_no as x1_0_, employee.e_full_name as x2_0_, 
tmsAttendance.e_attendance_date as x3_0_, weeklySchedule.e_description as x4_0_, shift.e_shift_type as x5_0_, 
shift.e_shift_description as x6_0_, tmsAttendance.e_daily_in_time as x7_0_, tmsAttendance.e_daily_out_time as x8_0_, 
tmsAttendance.e_total_raw_working_hours as x9_0_, tmsAttendance.e_total_raw_ot_time as x10_0_, tmsAttendance.e_late_in as x11_0_, 
tmsAttendance.e_employee_id as x12_0_, tmsAttendance.e_tms_attendance_id as x13_0_, shift.e_shift_time_type as x14_0_, 
shift.e_from_time as x15_0_, shift.e_from_time1 as x16_0_, shift.e_to_time as x17_0_, shift.e_to_time1 as x18_0_ 
FROM  
ev_employee employee
INNer join 
e_tms_attendance_view tmsAttendance
ON 
tmsAttendance.e_employee_id=employee.e_employee_id 
and(tmsAttendance.e_attendance_date>='2009-07-26' )
and(tmsAttendance.e_attendance_date<='2009-08-25' )
and(tmsAttendance.e_late_in>'00:00' )
and(tmsAttendance.e_company_id='MYDIN' )
and(employee.e_employee_id in(SELECT Employee.e_employee_id FROM ev_employee Employee WHERE (Employee.e_deleted=0 )
and(Employee.e_company_id in('MYDIN'))
)
)
INNer join ev_tms_employee tmsEmployee
ON tmsAttendance.e_employee_id=tmsEmployee.e_employee_id
INNER JOIN e_personal_schedule_view personalSchedule
ON tmsAttendance.e_attendance_date=personalSchedule.e_date 
and tmsAttendance.e_employee_id=personalSchedule.e_employee_id 
INNER JOIN ev_weekly_schedule weeklySchedule
ON personalSchedule.e_weekly_schedule_id=weeklySchedule.e_weekly_schedule_id 
INNER JOIN ev_schedule_group scheduleGroup
ON scheduleGroup.e_schedule_group_id=tmsEmployee.e_schedule_group_id
INNER JOIN ev_shift shift
ON tmsAttendance.e_shift_id=shift.e_shift_id 

解决方案 »

  1.   

    都是连接的话好像没什么好优化的,在连接条件上加上相应索引and(tmsAttendance.e_attendance_date>='2009-07-26' ) 
    and(tmsAttendance.e_attendance_date <='2009-08-25' 像这种查询加聚集索引
      

  2.   

    employee.e_employee_id in(SELECT Employee.e_employee_id FROM ev_employee Employee WHERE (Employee.e_deleted=0 ) IN这里尝试改连接查询
      

  3.   

    FROM  
    ev_employee employee 
    INNer join 
    e_tms_attendance_view tmsAttendance 
    ON 
    tmsAttendance.e_employee_id=employee.e_employee_id 
    and(tmsAttendance.e_attendance_date>='2009-07-26' ) 
    and(tmsAttendance.e_attendance_date <='2009-08-25' ) 
    and(tmsAttendance.e_late_in>'00:00' ) 
    and(tmsAttendance.e_company_id='MYDIN' ) 
    AND (employee.e_deleted=0 ) and(employee.e_company_id in('MYDIN')) 你同一个表也要子查询?来加条件
      

  4.   

    and(employee.e_company_id in('MYDIN')) IN后面是不是一个参数?
    如果不是请用=暂时只发现这些地方
      

  5.   

    如何写出性能优良的SQL
    (1) 选择最有效率的表名顺序(只在基于规则的优化器中有效): 
    ORACLE的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving table)将被最先处理,在FROM子句中包含多个表的情况下,你必须选择记录条数最少的表作为基础表。如果有3个以上的表连接查询, 那就需要选择交叉表(intersection table)作为基础表, 交叉表是指那个被其他表所引用的表. 
    (2) WHERE子句中的连接顺序.: 
    ORACLE采用自下而上的顺序解析WHERE子句,根据这个原理,表之间的连接必须写在其他WHERE条件之前, 那些可以过滤掉最大数量记录的条件必须写在WHERE子句的末尾. 
    (3) SELECT子句中避免使用 ‘ * ‘: 
    ORACLE在解析的过程中, 会将'*' 依次转换成所有的列名, 这个工作是通过查询数据字典完成的, 这意味着将耗费更多的时间 
    (4) 减少访问数据库的次数: 
    ORACLE在内部执行了许多工作: 解析SQL语句, 估算索引的利用率, 绑定变量 , 读数据块等; 
    (5) 在SQL*Plus , SQL*Forms和Pro*C中重新设置ARRAYSIZE参数, 可以增加每次数据库访问的检索数据量 ,建议值为200 
    (6) 使用DECODE函数来减少处理时间: 
    使用DECODE函数可以避免重复扫描相同记录或重复连接相同的表. 
    (7) 整合简单,无关联的数据库访问: 
    如果你有几个简单的数据库查询语句,你可以把它们整合到一个查询中(即使它们之间没有关系) 
    (8) 删除重复记录: 
    最高效的删除重复记录方法 ( 因为使用了ROWID)例子: 
    DELETE FROM EMP E WHERE E.ROWID > (SELECT MIN(X.ROWID) 
    FROM EMP X WHERE X.EMP_NO = E.EMP_NO); 
    (9) 用TRUNCATE替代DELETE: 
    当删除表中的记录时,在通常情况下, 回滚段(rollback segments ) 用来存放可以被恢复的信息. 如果你没有COMMIT事务,ORACLE会将数据恢复到删除之前的状态(准确地说是恢复到执行删除命令之前的状况) 而当运用TRUNCATE时, 回滚段不再存放任何可被恢复的信息.当命令运行后,数据不能被恢复.因此很少的资源被调用,执行时间也会很短. (译者按: TRUNCATE只在删除全表适用,TRUNCATE是DDL不是DML) 
    (10) 尽量多使用COMMIT: 
    只要有可能,在程序中尽量多使用COMMIT, 这样程序的性能得到提高,需求也会因为COMMIT所释放的资源而减少: 
    COMMIT所释放的资源: 
    a. 回滚段上用于恢复数据的信息. 
    b. 被程序语句获得的锁 
    c. redo log buffer 中的空间 
    d. ORACLE为管理上述3种资源中的内部花费 
    (11) 用Where子句替换HAVING子句: 
    避免使用HAVING子句, HAVING 只会在检索出所有记录之后才对结果集进行过滤. 这个处理需要排序,总计等操作. 如果能通过WHERE子句限制记录的数目,那就能减少这方面的开销. (非oracle中)on、where、having这三个都可以加条件的子句中,on是最先执行,where次之,having最后,因为on是先把不符合条件的记录过滤后才进行统计,它就可以减少中间运算要处理的数据,按理说应该速度是最快的,where也应该比having快点的,因为它过滤数据后才进行sum,在两个表联接时才用on的,所以在一个表的时候,就剩下where跟having比较了。在这单表查询统计的情况下,如果要过滤的条件没有涉及到要计算字段,那它们的结果是一样的,只是where可以使用rushmore技术,而having就不能,在速度上后者要慢如果要涉及到计算的字段,就表示在没计算之前,这个字段的值是不确定的,根据上篇写的工作流程,where的作用时间是在计算之前就完成的,而having就是在计算后才起作用的,所以在这种情况下,两者的结果会不同。在多表联接查询时,on比where更早起作用。系统首先根据各个表之间的联接条件,把多个表合成一个临时表后,再由where进行过滤,然后再计算,计算完后再由having进行过滤。由此可见,要想过滤条件起到正确的作用,首先要明白这个条件应该在什么时候起作用,然后再决定放在那里 
    (12) 减少对表的查询: 
    在含有子查询的SQL语句中,要特别注意减少对表的查询.例子: 
    SELECT TAB_NAME FROM TABLES WHERE (TAB_NAME,DB_VER) = ( SELECT 
    TAB_NAME,DB_VER FROM TAB_COLUMNS WHERE VERSION = 604) 
    (13) 通过内部函数提高SQL效率.: 
    复杂的SQL往往牺牲了执行效率. 能够掌握上面的运用函数解决问题的方法在实际工作中是非常有意义的 
    (14) 使用表的别名(Alias): 
    当在SQL语句中连接多个表时, 请使用表的别名并把别名前缀于每个Column上.这样一来,就可以减少解析的时间并减少那些由Column歧义引起的语法错误. 
    (15) 用EXISTS替代IN、用NOT EXISTS替代NOT IN: 
    在许多基于基础表的查询中,为了满足一个条件,往往需要对另一个表进行联接.在这种情况下, 使用EXISTS(或NOT EXISTS)通常将提高查询的效率. 在子查询中,NOT IN子句将执行一个内部的排序和合并. 无论在哪种情况下,NOT IN都是最低效的 (因为它对子查询中的表执行了一个全表遍历). 为了避免使用NOT IN ,我们可以把它改写成外连接(Outer Joins)或NOT EXISTS. 
    例子: 
    (高效)SELECT * FROM EMP (基础表) WHERE EMPNO > 0 AND EXISTS (SELECT ‘X' FROM DEPT WHERE DEPT.DEPTNO = EMP.DEPTNO AND LOC = ‘MELB') 
    (低效)SELECT * FROM EMP (基础表) WHERE EMPNO > 0 AND DEPTNO IN(SELECT DEPTNO FROM DEPT WHERE LOC = ‘MELB') 
    (16) 识别'低效执行'的SQL语句: 
    虽然目前各种关于SQL优化的图形化工具层出不穷,但是写出自己的SQL工具来解决问题始终是一个最好的方法: 
    SELECT EXECUTIONS , DISK_READS, BUFFER_GETS, 
    ROUND((BUFFER_GETS-DISK_READS)/BUFFER_GETS,2) Hit_radio, 
    ROUND(DISK_READS/EXECUTIONS,2) Reads_per_run, 
    SQL_TEXT 
    FROM V$SQLAREA 
    WHERE EXECUTIONS>0 
    AND BUFFER_GETS > 0 
    AND (BUFFER_GETS-DISK_READS)/BUFFER_GETS < 0.8 
    ORDER BY 4 DESC; 
    (17) 用索引提高效率: 
    索引是表的一个概念部分,用来提高检索数据的效率,ORACLE使用了一个复杂的自平衡B-tree结构. 通常,通过索引查询数据比全表扫描要快. 当ORACLE找出执行查询和Update语句的最佳路径时, ORACLE优化器将使用索引. 同样在联结多个表时使用索引也可以提高效率. 另一个使用索引的好处是,它提供了主键(primary key)的唯一性验证.。那些LONG或LONG RAW数据类型, 你可以索引几乎所有的列. 通常, 在大型表中使用索引特别有效. 当然,你也会发现, 在扫描小表时,使用索引同样能提高效率. 虽然使用索引能得到查询效率的提高,但是我们也必须注意到它的代价. 索引需要空间来存储,也需要定期维护, 每当有记录在表中增减或索引列被修改时, 索引本身也会被修改. 这意味着每条记录的INSERT , DELETE , UPDATE将为此多付出4 , 5 次的磁盘I/O . 因为索引需要额外的存储空间和处理,那些不必要的索引反而会使查询反应时间变慢.。定期的重构索引是有必要的.: 
    ALTER INDEX <INDEXNAME> REBUILD <TABLESPACENAME> 
    (18) 用EXISTS替换DISTINCT: 
    当提交一个包含一对多表信息(比如部门表和雇员表)的查询时,避免在SELECT子句中使用DISTINCT. 一般可以考虑用EXIST替换, EXISTS 使查询更为迅速,因为RDBMS核心模块将在子查询的条件一旦满足后,立刻返回结果. 例子: 
    (低效): 
    SELECT DISTINCT DEPT_NO,DEPT_NAME FROM DEPT D , EMP E 
    WHERE D.DEPT_NO = E.DEPT_NO 
    (高效): 
    SELECT DEPT_NO,DEPT_NAME FROM DEPT D WHERE EXISTS ( SELECT ‘X' 
    FROM EMP E WHERE E.DEPT_NO = D.DEPT_NO); 
    (19) sql语句用大写的;因为oracle总是先解析sql语句,把小写的字母转换成大写的再执行 
    (20) 在java代码中尽量少用连接符“+”连接字符串! 
    (21) 避免在索引列上使用NOT 通常,  
    我们要避免在索引列上使用NOT, NOT会产生在和在索引列上使用函数相同的影响. 当ORACLE”遇到”NOT,他就会停止使用索引转而执行全表扫描. 
      

  6.   

    INNer join e_tms_attendance_view tmsAttendance ON tmsAttendance.e_employee_id=employee.e_employee_id 
    and(tmsAttendance.e_attendance_date>='2009-07-26' ) 
    and(tmsAttendance.e_attendance_date <='2009-08-25' ) 
    and(tmsAttendance.e_late_in>'00:00' ) 
    and(tmsAttendance.e_company_id='MYDIN' ) 
    and(employee.e_employee_id in(SELECT Employee.e_employee_id FROM ev_employee Employee WHERE (Employee.e_deleted=0 ) 
    and(Employee.INNer join e_tms_attendance_view tmsAttendance ON tmsAttendance.e_employee_id=employee.e_employee_id 
    and(tmsAttendance.e_attendance_date>='2009-07-26' ) 
    and(tmsAttendance.e_attendance_date <='2009-08-25' ) 
    and(tmsAttendance.e_late_in>'00:00' ) 
    and(tmsAttendance.e_company_id='MYDIN' ) 
    and(employee.e_employee_id in(SELECT Employee.e_employee_id FROM ev_employee Employee WHERE (Employee.e_deleted=0 ) 
    and(Employee.e_company_id in('MYDIN'))) 
    Employee.e_company_id in('MYDIN')
    如果只有一个条件的话,应该用=  会好些吧!
      

  7.   

    为什么 会比下面的语句慢呢?SELECT scheduleGroup.e_description as x0_0_, tmsEmployee.e_employee_no as x1_0_, employee.e_full_name as x2_0_, 
    tmsAttendance.e_attendance_date as x3_0_, weeklySchedule.e_description as x4_0_, shift.e_shift_type as x5_0_, 
    shift.e_shift_description as x6_0_, tmsAttendance.e_daily_in_time as x7_0_, tmsAttendance.e_daily_out_time as x8_0_, 
    tmsAttendance.e_total_raw_working_hours as x9_0_, tmsAttendance.e_total_raw_ot_time as x10_0_, tmsAttendance.e_late_in as x11_0_, 
    tmsAttendance.e_employee_id as x12_0_, tmsAttendance.e_tms_attendance_id as x13_0_, shift.e_shift_time_type as x14_0_, 
    shift.e_from_time as x15_0_, shift.e_from_time1 as x16_0_, shift.e_to_time as x17_0_, shift.e_to_time1 as x18_0_ 
    FROM e_personal_schedule_view personalSchedule, ev_tms_employee tmsEmployee, ev_schedule_group scheduleGroup, 
    e_tms_attendance_view tmsAttendance, ev_employee employee, ev_weekly_schedule weeklySchedule, ev_shift shift 
    WHERE (tmsAttendance.e_attendance_date=personalSchedule.e_date )
    and(tmsAttendance.e_employee_id=personalSchedule.e_employee_id )
    and(scheduleGroup.e_schedule_group_id=tmsEmployee.e_schedule_group_id )
    and(personalSchedule.e_weekly_schedule_id=weeklySchedule.e_weekly_schedule_id )
    and(tmsAttendance.e_shift_id=shift.e_shift_id )
    and(tmsAttendance.e_employee_id=employee.e_employee_id )
    and(tmsAttendance.e_employee_id=tmsEmployee.e_employee_id )
    and(tmsAttendance.e_attendance_date>='2009-07-26' )
    and(tmsAttendance.e_attendance_date<='2009-08-25' )
    and(tmsAttendance.e_late_in>'00:00' )
    and(tmsAttendance.e_company_id='MYDIN' )
    and(employee.e_employee_id in(SELECT Employee.e_employee_id FROM ev_employee Employee WHERE (Employee.e_deleted=0 )
    and(Employee.e_company_id in('MYDIN'))))
     ORDER BY  tmsEmployee.e_employee_no , tmsAttendance.e_attendance_date慢好多啊。
      

  8.   

    select tmsAttendance.* from(
    select a.* from e_tms_attendance_view a 
    where (a.e_attendance_date>='2009-07-26' )
    AND(a.e_attendance_date<='2009-08-25' )
    AND(a.e_late_in>'00:00' )
    AND(a.e_company_id='MYDIN' )
    )tmsAttendance inner join ev_employee employee
    ON tmsAttendance.e_employee_id=employee.e_employee_id 
    AND employee.e_employee_id in(SELECT Employee.e_employee_id FROM ev_employee Employee WHERE (Employee.e_deleted=0 )
    and(Employee.e_company_id in('MYDIN')))
    INNER JOIN ev_tms_employee tmsEmployee
    ON tmsAttendance.e_employee_id=tmsEmployee.e_employee_id
    INNER JOIN e_personal_schedule_view personalSchedule
    ON tmsAttendance.e_attendance_date=personalSchedule.e_date 
    AND tmsAttendance.e_employee_id=personalSchedule.e_employee_id 
    INNER JOIN ev_weekly_schedule weeklySchedule
    ON personalSchedule.e_weekly_schedule_id=weeklySchedule.e_weekly_schedule_id 
    INNER JOIN ev_schedule_group scheduleGroup
    ON scheduleGroup.e_schedule_group_id=tmsEmployee.e_schedule_group_id
    INNER JOIN ev_shift shift
    ON tmsAttendance.e_shift_id=shift.e_shift_id ORDER BY  tmsEmployee.e_employee_no , tmsAttendance.e_attendance_date
    目前看来 这个语句是效率最高的。
    不知道为什么会这样。
      

  9.   

    select tmsAttendance.* from(
    select a.e_employee_id,a.e_attendance_date,a.e_shift_id from e_tms_attendance_view a 
    where (a.e_attendance_date>='2009-07-26' )
    AND(a.e_attendance_date<='2009-08-25' )
    AND(a.e_late_in>'00:00' )
    AND(a.e_company_id='MYDIN' )
    )tmsAttendance inner join ev_employee employee
    ON tmsAttendance.e_employee_id=employee.e_employee_id 
    AND employee.e_employee_id in(SELECT Employee.e_employee_id FROM ev_employee Employee WHERE (Employee.e_deleted=0 )
    and(Employee.e_company_id in('MYDIN')))
    INNER JOIN ev_tms_employee tmsEmployee
    ON tmsAttendance.e_employee_id=tmsEmployee.e_employee_id
    INNER JOIN e_personal_schedule_view personalSchedule
    ON tmsAttendance.e_attendance_date=personalSchedule.e_date 
    AND tmsAttendance.e_employee_id=personalSchedule.e_employee_id 
    INNER JOIN ev_weekly_schedule weeklySchedule
    ON personalSchedule.e_weekly_schedule_id=weeklySchedule.e_weekly_schedule_id 
    INNER JOIN ev_schedule_group scheduleGroup
    ON scheduleGroup.e_schedule_group_id=tmsEmployee.e_schedule_group_id
    INNER JOIN ev_shift shift
    ON tmsAttendance.e_shift_id=shift.e_shift_id ORDER BY  tmsEmployee.e_employee_no , tmsAttendance.e_attendance_date
      

  10.   

    1、在连接字段上建立索引
    2、ev_employee employee 
    INNer join 
    e_tms_attendance_view tmsAttendance 
    ON 
    tmsAttendance.e_employee_id=employee.e_employee_id 
    and(tmsAttendance.e_attendance_date>='2009-07-26' ) 
    and(tmsAttendance.e_attendance_date <='2009-08-25' ) 
    and(tmsAttendance.e_late_in>'00:00' ) 
    and(tmsAttendance.e_company_id='MYDIN' ) 
    and(employee.e_employee_id in(SELECT Employee.e_employee_id FROM ev_employee Employee WHERE (Employee.e_deleted=0 ) 
    and(Employee.e_company_id in('MYDIN'))这一部分里面的子查询进行优化 
      

  11.   


    select scheduleGroup.e_description as scheduleGroupDesc, tmsEmployee.e_employee_no employeeNo, 
    employee.e_full_name as fullName, tmsAttendance.e_attendance_date as attendanceDate, weeklySchedule.e_description as weeklyScheduleDesc,
     shift.e_shift_type as shiftType, shift.e_shift_description as shiftDesc, tmsAttendance.e_daily_in_time as attendanceDailyInTime, 
     tmsAttendance.e_daily_out_time as attendanceDailyOutTime, tmsAttendance.e_total_raw_working_hours as attendanceTotalRawWorking, 
     tmsAttendance.e_total_raw_ot_time as attendanceToTalRawOtTime, tmsAttendance.e_late_in as attendanceLateIn,
     tmsAttendance.e_employee_id as emloyeeId , tmsAttendance.e_tms_attendance_id as attendanceId, 
     shift.e_shift_time_type as shiftTimeType, shift.e_from_time as shiftFromTime, shift.e_from_time1 as shiftFromTime1, 
     shift.e_to_time as shiftToTime, shift.e_to_time1 as shiftToTime1,vi.e_time_log as timeLog,vi.e_is_in as isIn 
     from( 
     
     select a.e_attendance_date,a.e_daily_in_time,a.e_daily_out_time,a.e_total_raw_working_hours,a.e_total_raw_ot_time,
     a.e_late_in,a.e_tms_attendance_id,a.e_employee_id ,a.e_shift_id from e_tms_attendance_view a 
     WHERE 
     
     (a.e_attendance_date>='2009-07-26' AND a.e_attendance_date <='2009-08-25' AND a.e_late_in>'00:00' ) 
     
     AND(a.e_company_id='MYDIN')
     )tmsAttendance INNer JOIN ev_employee employee ON tmsAttendance.e_employee_id=employee.e_employee_id  and employee.e_deleted=0 
     and employee.e_company_id  ='MYDIN' 
     INNER JOIN ev_tms_employee tmsEmployee
     ON tmsAttendance.e_employee_id=tmsEmployee.e_employee_id 
     INNER JOIN e_personal_schedule_view personalSchedule  ON tmsAttendance.e_attendance_date=personalSchedule.e_date  AND tmsAttendance.e_employee_id=personalSchedule.e_employee_id 
     INNER JOIN ev_weekly_schedule weeklySchedule ON personalSchedule.e_weekly_schedule_id=weeklySchedule.e_weekly_schedule_id 
     INNER JOIN ev_schedule_group scheduleGroup ON scheduleGroup.e_schedule_group_id=tmsEmployee.e_schedule_group_id 
     INNER JOIN ev_shift shift
     ON tmsAttendance.e_shift_id=shift.e_shift_id 
     
    left join e_tms_attendance_detail_view vi 
    ON vi.e_is_in =1 AND vi.e_tms_attendance_id =tmsAttendance.e_tms_attendance_idorder by tmsEmployee.e_employee_no , tmsAttendance.e_attendance_date
      

  12.   

    我觉得你应该从
    1.表的索引
    2.尽量减少表的连接
    等方面去考虑了.
    这么多表,很难优化.SQL语句优化汇总
    http://topic.csdn.net/u/20080716/11/2317d040-48e7-42da-822e-040b4c55b46d.htmlSQL优化34条
    http://topic.csdn.net/u/20080808/14/dfc973e7-fcf2-4526-9de1-fa5efc148f5c.html给你两文章,自己看看去吧.