添加ORDER BY时查询速度非常慢

时间:2013-01-04 21:00:39

标签: mysql

我有一个相当复杂的观点,即加入多个表和一个简单但必须处理数百万条记录的VIEW:

SELECT wo.id_primary       AS id_primary, 
       wo.id_station       AS id_station, 
       wo.id_stage         AS id_stage, 
       wo.id_type          AS id_type, 
       wo.id_options       AS id_options, 
       wo.id_warehouse     AS id_warehouse, 
       wo.id_customer      AS id_customer, 
       wo.id_manufacturer  AS id_manufacturer, 
       wo.date_advanced    AS date_advanced, 
       wo.date_received    AS date_received, 
       wo.date_approved    AS date_approved, 
       wo.date_promised    AS date_promised, 
       wo.date_inspected   AS date_inspected, 
       wo.date_scheduled   AS date_scheduled, 
       wo.date_completed   AS date_completed, 
       sh.date_shipped     AS date_shipped, 
       wo.rpi_revision     AS rpi_revision, 
       wo.rpi_number       AS rpi_number, 
       wo.purchase_order   AS purchase_order, 
       wo.serial_number    AS serial_number, 
       wo.model_number     AS model_number, 
       wo.part_number      AS part_number, 
       wo.part_name        AS part_name, 
       wo.quantity         AS quantity, 
       wo.status           AS status, 
       wo.quote            AS quote, 
       wo.notes            AS notes, 
       sm.name             AS manufacturer_name, 
       wh.name             AS warehouse_name, 
       sc.name             AS customer_name, 
       ss.name             AS station_name, 
       sc.certs            AS customer_certifications, 
       sc.address1         AS customer_address, 
       sc.city             AS customer_city, 
       sc.email            AS customer_email, 
       sc.phone1           AS customer_phone, 
       sc.postal           AS customer_postal, 
       sc.fax              AS customer_fax, 
       sh.shipped          AS shipped, 
       sh.waybill          AS waybill, 
       sh.invoice          AS invoice, 
       wq.type_currency    AS type_currency, 
       wq.date_quoted      AS date_quoted, 
       wq.grand_total      AS grand_total, 
       Round(( ( sh.date_shipped - IF(( wo.date_approved = 
0 ), sh.date_shipped, wo.date_approved) ) / 
86400 ), 2) AS TAT, 
ws.stations         AS stations 
FROM   aerospace_erp.erp_workorder wo 
       LEFT JOIN aerospace_erp.system_manufacturers sm 
              ON wo.id_manufacturer = sm.id_primary 
       LEFT JOIN aerospace_erp.system_stations ss 
              ON wo.id_station = ss.id_primary 
       LEFT JOIN aerospace_erp.crm_customer sc 
              ON wo.id_customer = sc.id_primary 
       LEFT JOIN aerospace_erp.list_workorder_warehouse wh 
              ON wo.id_warehouse = wh.id_primary 
       LEFT JOIN aerospace_erp.erp_shipping sh 
              ON wo.id_primary = sh.id_workorder 
       LEFT JOIN aerospace_crm.crm_quoting wq 
              ON wo.id_primary = wq.id_workorder 
       LEFT JOIN aerospace_erp.erp_workorder_scope ws 
              ON wo.id_primary = ws.id_primary 

这也在VIEW中。 erp_workorder_scope是嵌套的VIEW,它很简单,但由于每次必须处理的数据量,所以需要相当长的时间。

上面的整个VIEW在大约15秒或大约1.5秒内执行,没有erp_workorder_scope JOIN

我可以忍受15秒但是当我向VIEW添加一个ORDER BY xxx时,它实际运行45分钟。

此值将根据用户的排序方式进行更改,因此我无法在VIEW中对此条件进行硬编码。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试这个技巧

select * from (
SELECT wo.id_primary       AS id_primary, 
       wo.id_station       AS id_station, 
       wo.id_stage         AS id_stage, 
       wo.id_type          AS id_type, 
       wo.id_options       AS id_options, 
       wo.id_warehouse     AS id_warehouse, 
       wo.id_customer      AS id_customer, 
       wo.id_manufacturer  AS id_manufacturer, 
       wo.date_advanced    AS date_advanced, 
       wo.date_received    AS date_received, 
       wo.date_approved    AS date_approved, 
       wo.date_promised    AS date_promised, 
       wo.date_inspected   AS date_inspected, 
       wo.date_scheduled   AS date_scheduled, 
       wo.date_completed   AS date_completed, 
       sh.date_shipped     AS date_shipped, 
       wo.rpi_revision     AS rpi_revision, 
       wo.rpi_number       AS rpi_number, 
       wo.purchase_order   AS purchase_order, 
       wo.serial_number    AS serial_number, 
       wo.model_number     AS model_number, 
       wo.part_number      AS part_number, 
       wo.part_name        AS part_name, 
       wo.quantity         AS quantity, 
       wo.status           AS status, 
       wo.quote            AS quote, 
       wo.notes            AS notes, 
       sm.name             AS manufacturer_name, 
       wh.name             AS warehouse_name, 
       sc.name             AS customer_name, 
       ss.name             AS station_name, 
       sc.certs            AS customer_certifications, 
       sc.address1         AS customer_address, 
       sc.city             AS customer_city, 
       sc.email            AS customer_email, 
       sc.phone1           AS customer_phone, 
       sc.postal           AS customer_postal, 
       sc.fax              AS customer_fax, 
       sh.shipped          AS shipped, 
       sh.waybill          AS waybill, 
       sh.invoice          AS invoice, 
       wq.type_currency    AS type_currency, 
       wq.date_quoted      AS date_quoted, 
       wq.grand_total      AS grand_total, 
       Round(( ( sh.date_shipped - IF(( wo.date_approved = 
0 ), sh.date_shipped, wo.date_approved) ) / 
86400 ), 2) AS TAT, 
ws.stations         AS stations 
FROM   aerospace_erp.erp_workorder wo 
       LEFT JOIN aerospace_erp.system_manufacturers sm 
              ON wo.id_manufacturer = sm.id_primary 
       LEFT JOIN aerospace_erp.system_stations ss 
              ON wo.id_station = ss.id_primary 
       LEFT JOIN aerospace_erp.crm_customer sc 
              ON wo.id_customer = sc.id_primary 
       LEFT JOIN aerospace_erp.list_workorder_warehouse wh 
              ON wo.id_warehouse = wh.id_primary 
       LEFT JOIN aerospace_erp.erp_shipping sh 
              ON wo.id_primary = sh.id_workorder 
       LEFT JOIN aerospace_crm.crm_quoting wq 
              ON wo.id_primary = wq.id_workorder 
       LEFT JOIN aerospace_erp.erp_workorder_scope ws 
              ON wo.id_primary = ws.id_primary 
)
order by xxx