gpt4 book ai didi

mysql - 为什么仅使用索引不能解决此结果?

转载 作者:行者123 更新时间:2023-11-29 01:45:14 25 4
gpt4 key购买 nike

架构:

CREATE TABLE IF NOT EXISTS `tx_hep_homes_attributes_mm` (
`uid_local` int(11) NOT NULL DEFAULT '0',
`uid_foreign` int(11) NOT NULL DEFAULT '0',
`tablenames` varchar(30) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`sorting` int(11) NOT NULL DEFAULT '0',
KEY `uid_local` (`uid_local`),
KEY `uid_foreign` (`uid_foreign`),
KEY `uid_local_foreign` (`uid_local`,`uid_foreign`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

示例输入:

INSERT INTO `tx_hep_homes_attributes_mm` (`uid_local`, `uid_foreign`, `tablenames`, `sorting`) VALUES
(2, 4, '', 3),
(2, 1, '', 2),
(2, 2, '', 1),
(1, 2, '', 5),
(1, 3, '', 4),
(1, 4, '', 3),
(1, 7, '', 2),
(1, 8, '', 1);

查询:

SELECT amm.uid_local, 
amm.uid_foreign
FROM tx_hep_homes_attributes_mm amm
JOIN (SELECT 1 AS att_id
UNION
SELECT 4 AS att_id
UNION
SELECT 13 AS att_id
UNION
SELECT 22 AS att_id
UNION
SELECT 12 AS att_id)d1
ON d1.att_id = amm.uid_foreign
JOIN (SELECT 1 AS home_id
UNION
SELECT 2 AS home_id)d2
ON d2.home_id = amm.uid_local
ORDER BY uid_local

产生:

+------+---------------+-------------------+-------+------------------------------------------+--------------------+----------+-------------+-------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+---------------+-------------------+-------+------------------------------------------+--------------------+----------+-------------+-------+---------------------------------+
| 1 | PRIMARY | <derived7> | ALL | NULL | NULL | NULL | NULL | 2 | Using temporary; Using filesort |
| 1 | PRIMARY | amm | ref | uid_local,uid_foreign,uid_local_foreign | uid_local_foreign | 4 | d2.home_id | 1 | Using where; Using index |
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 5 | Using where; Using join buffer |
| 7 | DERIVED | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
| 8 | UNION | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
| NULL | UNION RESULT | <union7,8> | ALL | NULL | NULL | NULL | NULL | NULL | |
| 2 | DERIVED | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
| 3 | UNION | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
| 4 | UNION | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
| 5 | UNION | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
| 6 | UNION | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
| NULL | UNION RESULT | <union2,3,4,5,6> | ALL | NULL | NULL | NULL | NULL | NULL | |
+------+---------------+-------------------+-------+------------------------------------------+--------------------+----------+-------------+-------+---------------------------------+

如您所见,使用 where,使用文件排序,尽管它应该从复合索引中解析。

最佳答案

如果 tx_hep_homes_attributes_mm 只是这里显示的 9 行,那么我希望索引会被忽略,因为所有数据都将在缓存中并且使用索引会花费更多。

索引主要用于索引选择的数据量远小于表中行数的情况。结果中有 75% 的行。

关于mysql - 为什么仅使用索引不能解决此结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8713280/

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