LoginSignup
0
0

More than 1 year has passed since last update.

SQLの実行計画のインデックス表記を調べたけど、細かいところがわからなかった

Posted at

SQLのパフォーマンスチューニングをしていたのですが、
explainで実行計画をみていて、なんとなく解消できるんだけど
後輩にうまく説明できないところを調べてみました。(結局まだちゃんと理解していないですが...)

ちなみに遅い原因は複合インデックスが設定されていないことでした。

実行計画をみてみる

とりあえず実行計画をみる。(select文はサンプル)

select * from user from name = "サンプル" and age < 20
explain select * from user from name = "サンプル" and age < 20

Extraのところに、indexの使用状況が書いています。ケースとしては大体以下の3つをよくみます。

  • Using index
  • Using where
  • Using index condition

今回の遅いケースはUsing index conditionでした。

複合インデックスが効いていないことはなんとなくわかったのですが、そういえばUsing index conditionってなんだっけと。

公式をちょっと調べてもすぐに出てこなかったので、一旦別の記事で調べてみることに。

https://stackoverflow.com/questions/28759576/mysql-using-index-condition-vs-using-where-using-index
https://qiita.com/aidy91614/items/f17ab862986e9e5cdea6

Using index conditionはインデックスは一部効いているのですが、検索対象のカラムを網羅していないので、SQLが遅くなってしまうという理解です。

Using index > Using Where
Using index > Using Index Condition

と感じで良いかと。

Using whereについては結局ちゃんとまだ理解できていないです。
MySQLの公式に以下のような説明があります。

https://dev.mysql.com/doc/refman/8.0/en/explain-output.html#explain_extra

Using where (JSON property: attached_condition`)

WHERE clause is used to restrict which rows to match against the next table or send to the client. Unless you specifically intend to fetch or examine all rows from the table, you may have something wrong in your query if the Extra value is not Using where and the table join type is [ALL](https://dev.mysql.com/doc/refman/8.0/en/explain-output.html#jointype_all) or [index](https://dev.mysql.com/doc/refman/8.0/en/explain-output.html#jointype_index).
- Using where has no direct counterpart in JSON-formatted output; the attached_condition property contains any WHERE condition used.

読んだけど、よくわかりませんでした...
SQLでwhere句使っているよ。くらいの認識で良いのでしょうか。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0