0

我有一个相当复杂的视图,它连接多个表和一个简单但必须处理数百万条记录的视图:

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 

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

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

我可以忍受 15 秒,但是当我将 ORDER BY xxx 添加到 VIEW 时,它实际上运行了 45 分钟。

这个值也会根据用户的排序而改变,所以我不能在 VIEW 本身中硬编码这个标准。

有任何想法吗?

4

1 回答 1

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
于 2013-01-04T22:33:05.130 回答