我读过其他几篇文章和页面,我想不出一种方法来加速和使用索引,而不是全表扫描
问题中的
select f.*,ci.item_name
from forecastpro.current_item_forecast f
left join cat_items as ci on f.item_number=ci.item_number创建表current\_item\_forecast ( Axe3 varchar(128)默认空,Axe5 varchar(128)默认空,Axe7 varchar(128)默认空,Axe11 varchar(128)默认空,Item\_Number varchar(40) NOT空,Item\_Name varchar(128)默认空,fc\_year int(4) NULL,fc\_period int(2) NULL,Qty\_Forecast双非空'0‘’,D 17双非空默认值'0‘’,Stock\_Start浮点数非空默认值'0',Stock\_Transit浮点数不为空默认值'0',主键(Item\_Number,fc\_year DESC,fc\_period),密钥Item (Item\_Number),密钥Axe3 (Axe3),密钥Axe5 (Axe5),键Axe7 (Axe7) ) ENGINE=InnoDB默认CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci创建表<>D31( Item\_Number char(32) ),默认NULL、CHARSET=utf8
Explain (12 8)默认NULL、Axe3 char(10)默认NULL、Item\_Group varchar(32)默认NULL、Axe5 char(10)默认NULL、Axe7 char(10)默认NULL、Axe11 char(10)默认NULL、Date\_Created datetime缺省空、主键(Item\_Number)、KEY Item (Item\_Number) ) ENGINE=InnoDB默认NULLid select_type表类型: possible_keys键key_len引用行,额外的1简单f全部121882 100.00 1简单ci全部36838 100.00使用where;使用连接缓冲区(块嵌套循环)
其他尝试**这两个结果是相同的:**更新forecastpro.current_item_forecast f左联接cupfsa.cat_items ci on f.item_number=ci.item_number set f.item_name=ci.item_name UPDATE forecastpro.current_item_forecast f左forecastpro.current_item_forecast cupfsa.cat_items强制键用于f.item_number=ci.item_number set f.item_name=ci.item_name id select_type表类型possible_keys f.item_name=ci.item_name select_type表类型possible_keys KEY key_len key_len行额外1次更新F所有76953 100.00 1简单ci全部36838 100.00使用其中
任何运行的尝试都会永远运行。
你会推荐什么?
发布于 2020-01-16 18:19:38
对于第一个选择查询,可以尝试将以下索引添加到cat_items表中:
CREATE INDEX idx ON cat_items (item_number, item_name);理想情况下,这将加快左连接过程,因为current_item_forecast表中的每条记录都会使用cat_items表作为查找对象与item_number表匹配。注意,我们还在索引中包含了item_name,以便覆盖SELECT子句。
发布于 2020-01-17 07:02:11
(感谢您提供了CREATE TABLEs;如果没有它们,您的问题的根本原因就很难找出来。)
不要混合CHARACTER SETs,至少不要当JOINing由静脉曲张。这会导致昂贵的表格扫描。
current_item_forecast:
`Item_Number` varchar(40) NOT NULL ... DEFAULT CHARSET=utf8mb4
cat_items:
`Item_Number` char(32) ... DEFAULT CHARSET=utf8长度一致(40比32)。
由于PRIMARY KEY是唯一的键,所以不要为同一列冗余地包含KEY:
PRIMARY KEY (`Item_Number`),
KEY `Item` (`Item_Number`)除非列的宽度是固定的,否则使用VARCHAR。
https://stackoverflow.com/questions/59775628
复制相似问题