Mysql “可能的_键”中缺少索引…但仅在某些环境中
我有一个自己运行的软件栈,但也部署到客户场所 有一个特定的查询在我的环境中运行得非常好,但在客户的环境中运行得非常糟糕 我已经使用EXPLAIN确认,我的环境的查询计划器看到有一个很好的索引可用,并使用它。而客户环境中的同一查询在可能的_键下不提供该索引 以下是完整的查询,有些匿名: 选择t0.*, t1.*, t2.*, t3.3价值 从表0到表0 左外连接表1 t1 在t0.id=t1.table0\u id上 左外连接表2 t2 关于t1.id=t2.table1\u id t2.0=0 左外连接表3 t3 在t0.id=t3.table0\u id上 t3.type='whatever' “workcorp”中的t0.business在哪里Mysql “可能的_键”中缺少索引…但仅在某些环境中,mysql,Mysql,我有一个自己运行的软件栈,但也部署到客户场所 有一个特定的查询在我的环境中运行得非常好,但在客户的环境中运行得非常糟糕 我已经使用EXPLAIN确认,我的环境的查询计划器看到有一个很好的索引可用,并使用它。而客户环境中的同一查询在可能的_键下不提供该索引 以下是完整的查询,有些匿名: 选择t0.*, t1.*, t2.*, t3.3价值 从表0到表0 左外连接表1 t1 在t0.id=t1.table0\u id上 左外连接表2 t2 关于t1.id=t2.table1\u id t2.0=0
而“2016-11-01 00:00:00”结果是字符集和/或排序规则 我使用此查询来显示每个环境中的字符集: 从信息模式中选择表名、列名、字符集名 其中table\u schema=my\u cool\u数据库 t0、t3中的表_名称 按1说明订购,2 对于奖励积分,我检查了每个环境中的字符排序: 显示t0的完整列; 显示t3的完整列; 在我的环境中:两个表中的所有列都有utf8字符集和utf8\U unicode\U ci排序规则 在客户的环境中:t0完全符合我的环境,而t3是一个独特的雪花;它的列有拉丁字符集和拉丁-瑞典-ci排序 因此,我们看到的是,t3.table0\u id a latin1列上存在的索引不能用于连接到utf8表。因此,该指数在以下方面运行良好: 挑选* 来自表3 其中table0_id=‘随便什么’ 但该指数不能用于: 选择 t0.id, t3.3价值 从t0开始 左外连接t3
和上描述了类似的症状。不要从客户的前提中提取太多的查询。在这种情况下,详细信息很重要。我对这里的查询所做的唯一简化是重命名列和表以实现匿名性,并且为了简洁和匿名性,将所选列从每个表扩展到通配符,然后再选择各种特定列。我认为这应该是同等的。我明白细节很重要,但我必须小心,因为客户有严格的安全要求。我想到了这一点——尽管客户和我有相同的索引——也许他们的模式有细微的不同。就像table0一样,id在他们的环境中是一种不同的数据类型、编码或排序规则,在某种意义上,这使得它不适合加入。我会要求他们描述t0和t3。如果你的建议是关于我过滤查询结果的:我同意我省略了一些细节,但我认为我已经包括了所有显示相关佐证或差异的部分。不,不,我的意思是,紧接着就是查询,所以从理论上讲,您可以忽略大量的查询,并像这样认为它似乎不完整-不存在t1或t2表
+----+-------------+-------+------+--------------------------+-----------+---------+------+-------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------+--------------------------+-----------+---------+------+-------+----------+-------------+
| 1 | SIMPLE | t3 | ref | table0_id,type_and_value | table0_id | 108 | func | 2 | 100.00 | Using where |
+----+-------------+-------+------+--------------------------+-----------+---------+------+-------+----------+-------------+
+----+-------------+-------+------+----------------+----------------+---------+-------+----------------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------+----------------+----------------+---------+-------+----------------+----------+-------------+
| 1 | SIMPLE | t3 | ref | type_and_value | type_and_value | 257 | const | (far too many) | 100.00 | Using where |
+----+-------------+-------+------+----------------+----------------+---------+-------+----------------+----------+-------------+
EXPLAIN EXTENDED SELECT t0.*,
t1.*,
t2.*,
t3.value
FROM table0 t0
LEFT OUTER JOIN table1 t1
ON t0.id = t1.table0_id
LEFT OUTER JOIN table2 t2
ON t1.id = t2.table1_id
AND t2.deleted = 0
LEFT OUTER JOIN table3 t3 FORCE INDEX (table0_id)
ON t0.id = t3.table0_id
AND t3.type = 'whatever'
WHERE t0.business IN ('workcorp')
AND '2016-11-01 00:00:00' <= t0.date
AND t0.date < '2016-12-01 00:00:00'
ORDER BY t0.date DESC
+----+-------------+-------+------+---------------+-----------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+-----------+---------+------+------+-------------+
| 1 | SIMPLE | t3 | ref | table0_id | table0_id | 108 | func | 2 | Using where |
+----+-------------+-------+------+---------------+-----------+---------+------+------+-------------+
+----+-------------+-------+------+---------------+------+---------+-------+---------+----------+----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------+---------------+------+---------+-------+---------+----------+----------------------------------------------------+
| 1 | SIMPLE | t3 | ALL | NULL | NULL | NULL | NULL | (loads) | 100.00 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------+---------------+------+---------+-------+---------+----------+----------------------------------------------------+
+--------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| table3 | 0 | PRIMARY | 1 | id | A | 16696 | NULL | NULL | | BTREE | | |
| table3 | 1 | table0_id | 1 | table0_id | A | 16696 | NULL | NULL | | BTREE | | |
| table3 | 1 | type_and_value | 1 | type | A | 14 | NULL | NULL | | BTREE | | |
| table3 | 1 | type_and_value | 2 | value | A | 8348 | NULL | NULL | | BTREE | | |
+--------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
+--------+------------+-----------------+--------------+-----------------+-----------+-------------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------+------------+-----------------+--------------+-----------------+-----------+-------------------+----------+--------+------+------------+---------+---------------+
| table3 | 1 | table0_id | 1 | table0_id | A | (same as PRIMARY) | NULL | NULL | | BTREE | | |
+--------+------------+-----------------+--------------+-----------------+-----------+-------------------+----------+--------+------+------------+---------+---------------+
+----+-------------+--------+------+---------------+-----------+---------+-------+------+----------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+------+---------------+-----------+---------+-------+------+----------+-----------------------+
| 1 | SIMPLE | table3 | ref | table0_id | table0_id | 108 | const | 1 | 100.00 | Using index condition |
+----+-------------+--------+------+---------------+-----------+---------+-------+------+----------+-----------------------+