优化MariaDB简单查询以及解释json格式语句中的实际成本是什么

问题描述 投票:0回答:1

我有两个问题:

  1. 有什么方法可以优化或改写这个查询以便我能够执行它吗?我等了 2 天多,查询仍在执行...

  2. “成本”(我读到这些不是单位)如何转换为相对的东西,我可以预测查询有多大或查询将执行多长时间。

这是我的餐桌计划:

CREATE TABLE IF NOT EXISTS alb (
  id INT NOT NULL AUTO_INCREMENT,
  iid VARCHAR(169) UNIQUE NOT NULL,
  name TEXT,
  grp VARCHAR(255),
  prod VARCHAR(255),
  barcode VARCHAR(255),
  pr_ic DECIMAL(10,2),
  pr_al DECIMAL(10,2),
  pr_ap DECIMAL(10,2),
  pr_mp DECIMAL(10,2),
  q_ic INT,
  q_al INT,
  q_ap INT,
  q_mp INT,
  u_pr DECIMAL(10,2),
  ic VARCHAR(169),
  al VARCHAR(169),
  ap VARCHAR(169),
  mp VARCHAR(169),
  td VARCHAR(169),
  td_p INT,
  INDEX( iid, ic, al, ap, mp, td ),
  PRIMARY KEY( id )
);

CREATE TABLE IF NOT EXISTS al (
  id INT NOT NULL AUTO_INCREMENT,
  calc_pr DECIMAL(10,2),
  calc_pr_in_lc DECIMAL(10,2),
  def_barcode VARCHAR(255),
  def_barcode_quantity INT,
  name TEXT,
  grp VARCHAR(255),
  name1 TEXT,
  name2 TEXT,
  pr1 DECIMAL(10,2),
  pr1_in_lc DECIMAL(10,2),
  pr2 DECIMAL(10,2),
  pr2_in_lc DECIMAL(10,2),
  pr_in_lc DECIMAL(10,2),
  product_id VARCHAR(255),
  iid VARCHAR(255),
  iid2 VARCHAR(255),
  supplier_iid VARCHAR(255),
  tax INT,
  INDEX( iid, iid2 ),
  PRIMARY KEY( id )
);

这是执行计划:

SELECT a.iid, al.iid
  FROM alb a
 INNER JOIN al ON a.al LIKE concat( al.iid, '%' );
MariaDB [db]> EXPLAIN FORMAT=JSON SELECT a.iid, al.iid FROM alb a INNER JOIN al ON a.al LIKE concat( al.iid, '%')\G
*************************** 1. row ***************************
EXPLAIN: {
  "query_block": {
    "select_id": 1,
    "cost": 301476723.4,
    "nested_loop": [
      {
        "table": {
          "table_name": "a",
          "access_type": "index",
          "key": "iid_2",
          "key_length": "3059",
          "used_key_parts": ["iid", "ic", "al", "ap", "mp", "td"],
          "loops": 1,
          "rows": 1744202,
          "cost": 627.4988767,
          "filtered": 100,
          "using_index": true
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "al",
            "access_type": "index",
            "key": "iid",
            "key_length": "1536",
            "used_key_parts": ["iid", "iid2"],
            "loops": 1744202,
            "rows": 1859159,
            "cost": 301476095.9,
            "filtered": 100,
            "using_index": true
          },
          "buffer_type": "flat",
          "buffer_size": "256Kb",
          "join_type": "BNL",
          "attached_condition": "a.al like concat(al.iid,'%')"
        }
      }
    ]
  }
}
1 row in set (0.003 sec)

样本值:

+---------------+-----------------+
| alb.al        | al.iid          |
+---------------+-----------------+
| 0 001 107 438 | 0 001 107 438 3 |
| 0 001 107 444 | 0 001 107 444   |
| 0 001 107 446 | 0 001 107 446   |
| 0 001 107 476 | 0 001 107 476   |
| 0 001 108 021 | 0 001 108 021   |
| 0 001 108 230 | 0 001 108 230   |
| 0 001 108 237 | 0 001 108 237   |
| 0 001 108 239 | 0 001 108 239   |
| 0 001 108 241 | 0 001 108 241   |
| 0 001 108 405 | 0 001 108 405   |
+---------------+-----------------+
mariadb query-optimization sql-execution-plan
1个回答
0
投票
alb:  INDEX(al,  iid)

对于该查询来说,这将是一个更好的“覆盖”索引,请添加它。这可能会显着加快查询速度。

© www.soinside.com 2019 - 2024. All rights reserved.