Mysql 3表2加入

时间:2017-12-27 08:33:08

标签: mysql sql

目标是更新order_items表,然后更新列transportcost

Table test.inserttest2
- Rows: 480 000
 - Order table
 - Column: Ordernumber and Country


Table test.inserttest3
- Rows: 2 600 000
 - Order items
 - Column: Ordernumber, SKU and Transportcost

Table transportcost
- Rows: 17 000
- a global list of transport price
- Column: SKU, Country, Transportprice

在我做一个更新语句之前,我做一个select语句来检查我是否得到了正确的字段:   - 64秒   - 行:62 000

SELECT
  a.ordernumber,
  b.country,
  b.transportprice
FROM test.inserttest3 a
INNER JOIN transportcost AS b
  ON a.sku = b.sku
WHERE a.system_created > '2017-12-01'

我得到这样一个列表:

17004706    Germany 3.00
17004706    France  6.00
17004706    Netherlands 3.00
17004706    Belgium 3.00
17004706    Austria 4.00
17004706    Italy   5.00
17004706    Switzerland 1.00
17004706    UK  8.00
17004706    Spain   4.00
17004706    Poland  4.00
17004706    Norway  9.00
17004706    Denmark 5.00
17004706    Sweden  6.00
17004706    Czech   4.00
17004706    Finland 6.00
17004706    Ireland 3.00

下一步是将此信息用于order_items表。这是错误的,这是我尝试过的,但在运行这些查询8小时后我就停止了它们。

SELECT
  ps2.ordernumber,
  ps2.sku,
  ps2.country,
  ps2.transportprice
FROM test.inserttest2 aa
JOIN (SELECT
  a.ordernumber,
  b.sku,
  b.country,
  b.transportprice
FROM test.inserttest3 a
INNER JOIN transportcost AS b
  ON a.sku = b.sku
WHERE a.system_created > '2017-12-01') ps2
  ON aa.odernumber = ps2.ordernumber
WHERE aa.country = ps2.country
AND aa.system_created > '2017-12-01'
;

SELECT
  b.sku,
  b.country,
  b.transportcost
FROM test.inserttest3 AS a
INNER JOIN test.inserttest2 AS aa
  ON aa.ordernumber = a.ordernumber
INNER JOIN transportcost AS b
  ON a.sku = b.sku
WHERE aa.country = b.country
AND a.system_created > '2017-12-01'

;

1 个答案:

答案 0 :(得分:1)

  1. 您希望使用inserttest3表的传输价格更新您调用transportcost的订单商品表。为此,您首先尝试查询。这听起来很简单:只需加入表格。
  2. 您希望将此限制为2017-12-01之后创建的订单和订单商品。使用适当的数据库,无法在订单之前插入订单商品,因此我们可以将此限制为2017-12-01之后创建的订单
  3. 查询:

    select
      io.ordernumber,
      io.sku,
      io.transportcost as cost_wrong,
      t.transportprice as cost_right
    from test.inserttest2 o 
    join test.inserttest3 io on io.ordernumber = o.ordernumber
    join transportcost t on t.sku = io.sku and t.country = o.country
    where o.system_created > date '2017-12-01';
    

    因此,日期是我们限制要读取的数据量的唯一参数。因此,您应该在此列上有一个索引。可能的索引:

    create index idx_o on inserttest2 (system_created, ordernumber, country);
    create index idx_io on inserttest3 (ordernumber, sku);
    create index idx_t on transportcost (sku, country, transportprice);