Analysis of the Usage of mysql Multiple left join Join Queries

  • 2021-11-01 05:12:05
  • OfStack

This article illustrates the use of mysql multiple left join join queries. Share it for your reference, as follows:

When querying mysql, you need to connect multiple tables, such as querying the commodity table of the order, querying other information of the commodity, other information is not in the commodity table of the order, and you need to connect tables of other libraries, but the connection conditions are basically commodity ID. First, give an error statement (nesting between queries, which is very inefficient):


SELECT
  A.order_id,
  A.wid,
  A.work_name,
  A.supply_price,
  A.sell_price,
  A.total_num,
  A.sell_profit,
  A.sell_percent,
  A.goods_id,
  A.goods_name,
  A.classify,
  B.gb_name
FROM
  (
    SELECT
      A.sub_order_id AS order_id,
      A.photo_id AS wid,
      A.photo_name AS work_name,
      A.supply_price,
      A.sell_price,
      sum(A.num) AS total_num,
      (
        A.sell_price - A.supply_price
      ) AS sell_profit,
      (
        A.sell_price - A.supply_price
      ) / A.sell_price AS sell_percent,
      A.goods_id,
      A.goods_name,
      B.goods_name AS classify
    FROM
      order_goods AS A
    LEFT JOIN (
      SELECT
        A.goods_id,
        A.parentid,
        B.goods_name
      FROM
        test_qyg_goods.goods AS A
      LEFT JOIN test_qyg_goods.goods AS B ON A.parentid = B.goods_id
    ) AS B ON A.goods_id = B.goods_id
    WHERE
      A.createtime >= '2016-09-09 00:00:00'
    AND A.createtime <= '2016-10-16 23:59:59'
    AND FROM_UNIXTIME(
      UNIX_TIMESTAMP(A.createtime),
      '%Y-%m-%d'
    ) != '2016-09-28'
    AND FROM_UNIXTIME(
      UNIX_TIMESTAMP(A.createtime),
      '%Y-%m-%d'
    ) != '2016-10-07'
    GROUP BY
      A.photo_id
    ORDER BY
      A.goods_id ASC
  ) AS A
LEFT JOIN (
  SELECT
    A.wid,
    A.brand_id,
    B.gb_name
  FROM
    test_qyg_user.buser_goods_list AS A
  LEFT JOIN test_qyg_supplier.brands AS B ON A.brand_id = B.gbid
) AS B ON A.wid = B.wid

The query result takes more than 4 seconds. explain analysis shows that two sub-queries are all scans, and multiple of mysql can be used left join Optimization


SELECT
  A.sub_order_id,
  A.photo_id AS wid,
  A.photo_name AS work_name,
  A.supply_price,
  A.sell_price,
  sum(A.num) AS total_num,
  (
    A.sell_price - A.supply_price
  ) AS sell_profit,
  (
    A.sell_price - A.supply_price
  ) / A.sell_price AS sell_percent,
  A.goods_id,
  A.goods_name,
  B.parentid,
  C.goods_name AS classify,
  D.brand_id,
  E.gb_name,
  sum(
    CASE
    WHEN F.buy_type = 'yes' THEN
      A.num
    ELSE
      0
    END
  ) AS total_buy_num,
  sum(
    CASE
    WHEN F.buy_type = 'yes' THEN
      A.num
    ELSE
      0
    END * A.sell_price
  ) AS total_buy_money,
  sum(
    CASE
    WHEN F.buy_type = 'no' THEN
      A.num
    ELSE
      0
    END
  ) AS total_give_num,
  sum(
    CASE
    WHEN F.buy_type = 'no' THEN
      A.num
    ELSE
      0
    END * A.sell_price
  ) AS total_give_money
FROM
  order_goods AS A
LEFT JOIN test_qyg_goods.goods AS B ON A.goods_id = B.goods_id
LEFT JOIN test_qyg_goods.goods AS C ON B.parentid = C.goods_id
LEFT JOIN test_qyg_user.buser_goods_list AS D ON A.photo_id = D.wid
LEFT JOIN test_qyg_supplier.brands AS E ON D.brand_id = E.gbid
LEFT JOIN order_info_sub AS F ON A.sub_order_id = F.order_id
WHERE
  A.createtime >= '2016-09-09 00:00:00'
AND A.createtime <= '2016-10-16 23:59:59'
AND FROM_UNIXTIME(
  UNIX_TIMESTAMP(A.createtime),
  '%Y-%m-%d'
) != '2016-09-28'
AND FROM_UNIXTIME(
  UNIX_TIMESTAMP(A.createtime),
  '%Y-%m-%d'
) != '2016-10-07'
GROUP BY
  A.photo_id
ORDER BY
  A.goods_id ASC

Query results took 0.04 seconds

More readers interested in MySQL can check out the topics on this site: "MySQL Common Function Summary", "MySQL Log Operation Skills Collection", "MySQL Transaction Operation Skills Collection", "MySQL Stored Procedure Skills Collection" and "MySQL Database Lock Related Skills Collection"

I hope this article is helpful to everyone's MySQL database.


Related articles: