gpt4 book ai didi

indexing - Mariadb:为什么我的查询没有使用索引?

转载 作者:行者123 更新时间:2023-12-04 17:45:30 26 4
gpt4 key购买 nike

我想弄清楚为什么这个查询没有使用索引(所以它很长)。

explain select count(*) as aggregate 
from `megabase_sms_data_1`
left join `megabase_sms_thematic_repulse_1`
on `megabase_sms_data_1`.`contact_id` = `megabase_sms_thematic_repulse_1`.`megabase_data_id`
where not (megabase_sms_thematic_repulse_1.thematic_id <=> 6)
and `age` in ('2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '-1');


+------+-------------+---------------------------------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+---------+-------------------------------------------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+---------------------------------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+---------+-------------------------------------------+----------+-------------+
| 1 | SIMPLE | megabase_sms_data_1 | ALL | tmp_megabase_sms_data_1_age_index,tmp_megabase_sms_data_1_age_incomes_index,tmp_megabase_sms_data_1_age_housing_zipcode_sex_index,tmp_megabase_sms_data_1_age_zipcode_type_sex_index,tmp_megabase_sms_data_1_age_family_zipcode_sex_index,tmp_megabase_sms_data_1_age_couple_zipcode_sex_index,tmp_megabase_sms_data_1_age_department_sex_housing_index,tmp_megabase_sms_data_1_age_type_department_sex_index,tmp_megabase_sms_data_1_age_family_department_sex_index,tmp_megabase_sms_data_1_age_sex_couple_department_index | NULL | NULL | NULL | 20508191 | Using where |
| 1 | SIMPLE | megabase_sms_thematic_repulse_1 | ref | tmp_megabase_sms_thematic_repulse_1_megabase_data_id_index | tmp_megabase_sms_thematic_repulse_1_megabase_data_id_index | 8 | adsconsole.megabase_sms_data_1.contact_id | 1 | Using where |

我试图将索引添加到 contact_id 上的表,但它没有使用它。可能是我误解了一些概念。

我有那两个表:

 DESCRIBE megabase_sms_data_1;
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| contact_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| sms_md5 | varchar(255) | NO | MUL | NULL | |
| sms | varchar(255) | NO | MUL | NULL | |
| age | tinyint(4) | NO | MUL | NULL | |
| sex | tinyint(4) | NO | MUL | NULL | |
| couple | tinyint(4) | NO | MUL | NULL | |
| family | tinyint(4) | NO | MUL | NULL | |
| type | tinyint(4) | NO | MUL | NULL | |
| housing | tinyint(4) | NO | MUL | NULL | |
| gather_date | date | NO | | NULL | |
| freshness_date | date | NO | | NULL | |
| zipcode | char(5) | NO | MUL | NULL | |
| department | char(2) | NO | MUL | NULL | |
| address | varchar(255) | NO | | NULL | |
| city | varchar(255) | NO | | NULL | |
| latitude | double | NO | | NULL | |
| longitude | double | NO | | NULL | |
| incomes | tinyint(4) | NO | MUL | NULL | |
| csp | tinyint(4) | NO | | NULL | |
| adr_iris | varchar(255) | NO | | NULL | |
| adr_inse | varchar(255) | NO | | NULL | |
+----------------+------------------+------+-----+---------+----------------+
21 rows in set (0.00 sec)


SHOW INDEX FROM megabase_sms_data_1;
+---------------------+------------+----------------------------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ ---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------------------+------------+----------------------------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ ---------------+
| megabase_sms_data_1 | 0 | PRIMARY | 1 | contact_id | A | 20508191 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 0 | tmp_megabase_sms_data_1_contact_id | 1 | contact_id | A | 20508191 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_sms_md5_index | 1 | sms_md5 | A | 20508191 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_index | 1 | age | A | 13 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_sex_index | 1 | sex | A | 3 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_couple_index | 1 | couple | A | 2 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_family_index | 1 | family | A | 2 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_type_index | 1 | type | A | 2 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_housing_index | 1 | housing | A | 2 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_department_index | 1 | department | A | 99 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_incomes_index | 1 | incomes | A | 10 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_zipcode_index | 1 | zipcode | A | 21497 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_incomes_index | 1 | age | A | 13 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_incomes_index | 2 | incomes | A | 131 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_type_age_index | 1 | type | A | 2 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_type_age_index | 2 | age | A | 39 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_couple_age_index | 1 | couple | A | 2 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_couple_age_index | 2 | age | A | 39 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_family_age_index | 1 | family | A | 2 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_family_age_index | 2 | age | A | 39 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_sex_age_index | 1 | sex | A | 3 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_sex_age_index | 2 | age | A | 52 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_incomes_age_index | 1 | incomes | A | 10 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_incomes_age_index | 2 | age | A | 131 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_zipcode_sex_index | 1 | zipcode | A | 21497 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_zipcode_sex_index | 2 | sex | A | 43266 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_zipcode_age_index | 1 | zipcode | A | 21497 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_zipcode_age_index | 2 | age | A | 110259 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_zipcode_couple_index | 1 | zipcode | A | 21497 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_zipcode_couple_index | 2 | couple | A | 37085 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_zipcode_family_index | 1 | zipcode | A | 21497 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_zipcode_family_index | 2 | family | A | 38261 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_zipcode_type_index | 1 | zipcode | A | 21497 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_zipcode_type_index | 2 | type | A | 38549 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_zipcode_housing_index | 1 | zipcode | A | 21497 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_zipcode_housing_index | 2 | housing | A | 38841 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_department_sex_index | 1 | department | A | 99 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_department_sex_index | 2 | sex | A | 396 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_department_age_index | 1 | department | A | 99 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_department_age_index | 2 | age | A | 1393 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_department_couple_index | 1 | department | A | 99 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_department_couple_index | 2 | couple | A | 299 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_department_family_index | 1 | department | A | 99 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_department_family_index | 2 | family | A | 299 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_department_type_index | 1 | department | A | 99 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_department_type_index | 2 | type | A | 299 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_department_housing_index | 1 | department | A | 99 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_department_housing_index | 2 | housing | A | 299 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_housing_zipcode_sex_index | 1 | age | A | 13 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_housing_zipcode_sex_index | 2 | housing | A | 39 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_housing_zipcode_sex_index | 3 | zipcode | A | 253187 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_housing_zipcode_sex_index | 4 | sex | A | 539689 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_zipcode_type_sex_index | 1 | age | A | 13 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_zipcode_type_sex_index | 2 | zipcode | A | 110259 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_zipcode_type_sex_index | 3 | type | A | 256352 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_zipcode_type_sex_index | 4 | sex | A | 554275 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_family_zipcode_sex_index | 1 | age | A | 13 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_family_zipcode_sex_index | 2 | family | A | 39 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_family_zipcode_sex_index | 3 | zipcode | A | 256352 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_family_zipcode_sex_index | 4 | sex | A | 554275 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_couple_zipcode_sex_index | 1 | age | A | 13 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_couple_zipcode_sex_index | 2 | couple | A | 39 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_couple_zipcode_sex_index | 3 | zipcode | A | 250099 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_couple_zipcode_sex_index | 4 | sex | A | 525851 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_department_sex_housing_index | 1 | age | A | 13 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_department_sex_housing_index | 2 | department | A | 1393 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_department_sex_housing_index | 3 | sex | A | 5067 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_department_sex_housing_index | 4 | housing | A | 12192 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_type_department_sex_index | 1 | age | A | 13 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_type_department_sex_index | 2 | type | A | 39 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_type_department_sex_index | 3 | department | A | 3913 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_type_department_sex_index | 4 | sex | A | 12207 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_family_department_sex_index | 1 | age | A | 13 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_family_department_sex_index | 2 | family | A | 39 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_family_department_sex_index | 3 | department | A | 3931 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_family_department_sex_index | 4 | sex | A | 11759 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_sex_couple_department_index | 1 | age | A | 13 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_sex_couple_department_index | 2 | sex | A | 52 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_sex_couple_department_index | 3 | couple | A | 132 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_age_sex_couple_department_index | 4 | department | A | 10868 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | tmp_megabase_sms_data_1_sms_index | 1 | sms | A | 6836063 | NULL | NULL | | BTREE | | |
| megabase_sms_data_1 | 1 | contact_id | 1 | contact_id | A | 20508191 | NULL | NULL | | BTREE | | |

第二个:

describe megabase_sms_thematic_repulse_1;
+------------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------+------+-----+---------+-------+
| megabase_data_id | bigint(20) | NO | MUL | NULL | |
| thematic_id | int(11) | NO | MUL | NULL | |
+------------------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

SHOW INDEX FROM megabase_sms_thematic_repulse_1;
+---------------------------------+------------+------------------------------------------------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------------------------------+------------+------------------------------------------------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| megabase_sms_thematic_repulse_1 | 1 | tmp_megabase_sms_thematic_repulse_1_megabase_data_id_index | 1 | megabase_data_id | A | 3 | NULL | NULL | | BTREE | | |
| megabase_sms_thematic_repulse_1 | 1 | tmp_megabase_sms_thematic_repulse_1_thematic_id_index | 1 | thematic_id | A | 1 | NULL | NULL | | BTREE | | |
+---------------------------------+------------+------------------------------------------------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

我做错了什么?我可以做些什么来优化查询或数据库结构?

非常感谢

最佳答案

根据查询返回的行数,mysql 可能估计使用全扫描比使用索引更有效。

要缩小原因范围,请运行不带 where 子句的查询并查看是否正在使用索引。然后一个一个的加上where子句,看是哪一个导致索引不能用。该 where 子句可能过于通用 - 使其更有效地进行全表扫描,而不是使用索引。

如果假设年龄导致索引无法使用,您可以在 contact_id + age 上创建索引。

关于indexing - Mariadb:为什么我的查询没有使用索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48719673/

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