为什么解释键的SQL结果为null

发布于 2025-01-22 06:18:55 字数 1621 浏览 3 评论 0原文

我已经为我的表创建了索引,但是当使用解释查询时,键的结果为null。

我的表如下:表

ID

  • ID
  • id_tx
  • FK到ID to ref_country in Ref_country
  • cceiling
  • for

List_country:ID

  • 唯一
  • ID_REF_REF_REF_REF_REF_CONTRITIR
  • ID_REF_COUNTRITY
  • 密钥

  • list_country

ID_TX

  • 成本

ID_REF_COUNTRITY

EXPLAIN 
SELECT ctr.id_tx
, GROUP_CONCAT(rctr.country_name,':',cost) AS cost_country
, GROUP_CONCAT(rctr.country_name,':',cceiling) AS ceiling_country
, GROUP_CONCAT(rctr.country_name) AS country 
FROM list_country ctr
LEFT JOIN ref_country rctr ON rctr.id = ctr.id_ref_country 
GROUP BY id_tx

ID_TX 对于表list_country type = all,key = null

为什么键为list_country null,即使我指定索引?

该表的DDL:

CREATE TABLE `list_country` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `id_tx` INT NOT NULL,
  `id_ref_country` INT NOT NULL,
  `cost` DECIMAL(15,2) DEFAULT NULL,
  `cceiling` DECIMAL(15,2) DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE KEY `country_unik` (`id_tx`,`id_ref_country`) USING BTREE,
  KEY `id_tx` (`id_tx`) USING BTREE,
  KEY `id_ref_country` (`id_ref_country`) USING BTREE,
  CONSTRAINT `list_country_ibfk_1` FOREIGN KEY (`id_tx`) REFERENCES `ep_tx` (`id_tx`) ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `list_country_ibfk_2` FOREIGN KEY (`id_ref_country`) REFERENCES `ref_country` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=INNODB AUTO_INCREMENT=55609 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

I have created INDEX for my table but when use explain QUERY the result for key is NULL.

my table as below:

TABLE list_country

  • id
  • id_tx
  • id_ref_country FK TO id in ref_country
  • cost
  • cceiling

INDEX FOR list_country:

  • id PRIMARY
  • id_tx,id_ref_country UNIQUE
  • id_tx KEY
  • id_ref_country KEY

TABLE ref_country

  • id
  • country_name

INDEX for ref_country:

  • id PRIMARY

i run explain query as below:

EXPLAIN 
SELECT ctr.id_tx
, GROUP_CONCAT(rctr.country_name,':',cost) AS cost_country
, GROUP_CONCAT(rctr.country_name,':',cceiling) AS ceiling_country
, GROUP_CONCAT(rctr.country_name) AS country 
FROM list_country ctr
LEFT JOIN ref_country rctr ON rctr.id = ctr.id_ref_country 
GROUP BY id_tx

RESULT EXPLAIN FOR TABLE list_country TYPE = ALL, KEY = NULL

Why the key is null for list_country even i specify the index?

The DDL for this table:

CREATE TABLE `list_country` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `id_tx` INT NOT NULL,
  `id_ref_country` INT NOT NULL,
  `cost` DECIMAL(15,2) DEFAULT NULL,
  `cceiling` DECIMAL(15,2) DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE KEY `country_unik` (`id_tx`,`id_ref_country`) USING BTREE,
  KEY `id_tx` (`id_tx`) USING BTREE,
  KEY `id_ref_country` (`id_ref_country`) USING BTREE,
  CONSTRAINT `list_country_ibfk_1` FOREIGN KEY (`id_tx`) REFERENCES `ep_tx` (`id_tx`) ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `list_country_ibfk_2` FOREIGN KEY (`id_ref_country`) REFERENCES `ref_country` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=INNODB AUTO_INCREMENT=55609 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻风 2025-01-29 06:18:56

为了获得查询结果,MySSQL需要获取以下字段的信息:

  • CTR.ID_TX
  • CTR.ID_REF_COUNTRY,

因此,只有index country> country_unik,它包含两个字段,或MySQL可以读取完整的表。

说明输出格式全部:

对从
以前的表。如果桌子是第一个,通常这是不好的
表不标记为const,通常在所有其他情况下都非常糟糕。
通常,您可以通过添加启用行的索引来避免全部
根据常数值或列值从表中检索
从早期的表。

MySQL避免使用索引,因为它需要该表的所有记录。

To get the results for you query, MySSQL needs to get the info of the following fields:

  • ctr.id_tx
  • ctr.id_ref_country

Because of this, only the index country_unik can be used, it contains both fields, or MySQL can just read the complete table.

EXPLAIN Output Format says, about Type=ALL:

A full table scan is done for each combination of rows from the
previous tables. This is normally not good if the table is the first
table not marked const, and usually very bad in all other cases.
Normally, you can avoid ALL by adding indexes that enable row
retrieval from the table based on constant values or column values
from earlier tables.

MySQL is avoiding the use of the index, because it needs all records for that table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文