我有以下SQL
选择计数(*)从A内连接B在A.evnumber=B.evnumber内连接D在B.userid=D.userid上
这是解释结果。
[
{
"id": "1",
"select_type": "SIMPLE",
"table": "A",
"type": "index",
"possible_keys": "evnumber",
"key": "evnumber",
"key_len": "768",
"ref": null,
"rows": "13926",
"Extra": "Using where; Using index"
},
{
"id": "1",
"select_type": "SIMPLE",
"table": "B",
"type": "ref",
"possible_keys": "evnumber,UserID",
"key": "evnumber",
"key_len": "768",
"ref": "A.evnumber",
"rows": "1",
"Extra": "Using where"
},
{
"id": "1",
"select_type": "SIMPLE",
"table": "D",
"type": "ref",
"possible_keys": "mdl_userinfodata_usefie_ix",
"key": "mdl_userinfodata_usefie_ix",
"key_len": "8",
"ref": "B.UserId",
"rows": "134",
"Extra": "Using where; Using index"
}]
当我执行这个SQL时,需要40秒。根据行列值(13926x134x1=1,866,084)的乘积,我认为这是不可能的。请帮帮我,我该怎么改进呢?MySQL版本为5.6
发布于 2022-05-12 13:53:05
查询已经为联接使用索引,但它没有为B使用覆盖索引,这是我看到的唯一可能的改进。
我会为列(evnumber, user_id)在B上添加一个复合索引。这应该允许查询获得表B的“使用索引”,这表明它只使用索引,不需要读取表行。它不会减少表B在rows: 1中看到的数目,但它将有助于B更容易地连接到D。
有时这种类型的优化已经取得了很好的效果。对于非常大的表,它可以大大提高性能。但是你的桌子很小,所以可能没那么大的差别。
https://stackoverflow.com/questions/72214572
复制相似问题