gpt4 book ai didi

即使在索引后 MySQL 加入速度也很慢

转载 作者:行者123 更新时间:2023-11-29 04:02:00 25 4
gpt4 key购买 nike

我有两个表 members 和 dep,它们的描述如下:

TABLE members:
+-----------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------+------+-----+---------+-------+
| MemberID_M | varchar(8) | YES | MUL | NULL | |
| Age | varchar(5) | YES | | NULL | |
| Sex | varchar(1) | YES | | NULL | |
| SomeInfo | int(11) | YES | | NULL | |
+-----------------+------------+------+-----+---------+-------+
TABLE dep:
+-----------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------+------+-----+---------+-------+
| MemberID_t | int(11) | YES | MUL | NULL | |
| YEAR | varchar(2) | NO | | | |
| Days | tinyint(4) | YES | | NULL | |
| train | bigint(20) | NO | | 0 | |
+-----------------+------------+------+-----+---------+-------+

我想执行以下查询:

CREATE TABLE table2 
SELECT a.*,b.*
FROM dep AS a LEFT OUTER JOIN members AS b
ON a.MemberID_t = b.Memberid_M;

最初,两个表中的 ID 都没有被索引,并且查询没有返回数小时。现在,即使在编制索引之后也需要花费大量时间。

EXPLAIN for the SELECT part of the query is:
+----+-------------+-------+------+---------------+------+---------+------+--------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+--------+-------+
| 1 | SIMPLE | a | ALL | NULL | NULL | NULL | NULL | 436689 | |
| 1 | SIMPLE | b | ALL | memid2 | NULL | NULL | NULL | 226127 | |
+----+-------------+-------+------+---------------+------+---------+------+--------+-------+

最佳答案

问题是 MemberID_M 是一个 varchar 而 MemberID_t 是一个 INT

这个字段的转换是为什么要花这么长时间

为了获得最佳结果,关键字段必须是同一类型

关于即使在索引后 MySQL 加入速度也很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13034163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com