有關Sql order by 優化查詢的建議

2021-09-02 04:12:25 字數 4864 閱讀 2899

在使用左,右,或者內連線的時候,在需要使用排序的時候,不妨先以乙個表為標準,先進行排序,這樣可以提供語句的效能

比如:優化前:

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 [size=x-large][color=red]store_jobs a[/color][/size] 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'

[size=x-large][color=red] order by a.create_date desc [/color][/size]) 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 [size=x-large][color=blue](select * from store_jobs order by create_date desc) a[/color][/size] 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

Oracle 有關SQL效能優化

訪問 table 的方式 全表掃瞄 全表掃瞄就是順序地訪問表中每條記錄,oracle 採用一次讀入多個資料塊 databaseblock 的方式優化全表掃瞄 通過rowid 訪問表 rowid 包含了表中記錄的物理位置資訊,oracle 採用索引實現了資料和存放資料的物理位置 rowid 之間的聯絡...

MySql有關索引的優化

一 寫sql需要注意的?1.在where及order by之後建立索引。2.避免使用 和 會導致索引無效。3.有關null值問題 例如 select from user where is null,避免使用 應給 設定乙個預設的值0。4.避免使用or查詢,否則會放棄索引。可以使用union all替...

kruskal 並查集優化

這兩天搞dp搞的快暴了,想學學網路流。拿過算導來一看,最短路還沒整完呢。寫了乙個用並查集優化的kruskal演算法,並查集是用非遞迴的狀態壓縮實現的。詳見 kruskal沒有用堆優化,不是我不想,而是實在不會。所以直接用sort按權值排了下序,時間複雜度o n n logn my code incl...