次のようなクエリの例があります。
SELECT
rest.name, rest.shortname
FROM
restaurant AS rest
INNER JOIN specials ON rest.id=specials.restaurantid
WHERE
specials.dateend >= CURDATE()
AND
rest.state='VIC'
AND
rest.status = 1
AND
specials.status = 1
ORDER BY
rest.name ASC;
以下の2つのインデックスについて疑問に思っていますが、どちらがレストランのテーブルに最適でしょうか。
id,state,status,name
state,status,name
結合で使用される列を含める必要があるかどうかわからない場合は、
面白いことに、私はテスト用に両方のタイプを作成しましたが、どちらの場合もMySQLはプライマリインデックスを選択します。これはちょうどid
です。何故ですか?
出力の説明:
1,'SIMPLE','specials','index','NewIndex1\,NewIndex2\,NewIndex3\,NewIndex4','NewIndex4','11',\N,82,'Using where; Using index; Using temporary; Using filesort',
1,'SIMPLE','rest','eq_ref','PRIMARY\,search\,status\,state\,NewIndex1\,NewIndex2\,id-suburb\,NewIndex3\,id-status-name','PRIMARY','4','db_name.specials.restaurantid',1,'Using where'
現時点では行が少ないので、おそらくそれがPRIMARYを選択している理由です!?