【问题标题】:Understanding EXPLAIN to create column index based on了解 EXPLAIN 以创建基于列的索引
【发布时间】:2016-06-04 13:50:44
【问题描述】:

我有这个update 查询:

explain UPDATE
qanda AS ans1
JOIN qanda AS ans2 ON ans2.related = ans1.related
JOIN qanda AS ques ON ans2.related = ques.id
SET ans1.acceptedanswer = IF( ans1.id <> 3, 0, IFNULL( ans1.acceptedanswer, 0 ) ^ b'1' ),
ans1.aadate = IF( ans1.id <> 3, ans1.aadate, 4353)
WHERE ques.author_id = 29
AND ans2.id = 3
AND ans2.author_id = 31 
AND (ques.amount IS NULL or ans1.acceptedanswer IS NULL)

这是EXPLAIN的结果:

+------+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------------+
| id   | select_type | table | type  |       possible_keys       |   key   | key_len | ref   | rows | Extra       |
+------+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------------+
|    1 | SIMPLE      | ans2  | const | PRIMARY,author_id,related | PRIMARY | 4       | const | 1    | NULL        |
|    2 | SIMPLE      | ques  | const | PRIMARY,author_id         | PRIMARY | 4       | const | 1    | NULL        |
|    3 | SIMPLE      | ans1  | ALL   | related                   | NULL    | NULL    | NULL  | 4    | Using where |
+------+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------------+

需要注意的是,我的表只包含 4 行数据。

我的结构还好吗?还是应该在这样的列上创建这样的索引?

【问题讨论】:

  • 使用具有大量行数的测试数据库向其抛出大量行。现在你不是。目前,您的解释毫无意义。

标签: mysql indexing explain


【解决方案1】:

您在同一张表中同时存在问题和多个答案?这似乎是“错误的”。建议一张表提问,一张表回答。

SHOW CREATE TABLE 会很有帮助。

考虑“复合”索引...

INDEX(author_id, id)(按此顺序)可能会有用。

我怀疑您的 JOINans1 不正确(在业务逻辑意义上,而不是在 SQL 意义上);检查一下。

【讨论】:

  • Stackoverflow 还将问题和答案保存在同一个表中。 data.stackexchange.com/stackoverflow/query/edit/421098
  • 我猜SO逻辑是问题和答案具有“相同”的结构等,所以它也可能是同一张表。也许它并不像我暗示的那样“错误”。
猜你喜欢
  • 2018-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-24
  • 1970-01-01
  • 2020-05-25
  • 1970-01-01
相关资源
最近更新 更多