MySQL 8.4実機検証済み · 2026-11-30

MySQL ERROR 1176 (42000): 索引 'idx_cust' は表 'orders' には存在しません。

インデックスヒントに書いた索引がその表にありません。

MySQL 8.4.13
1176 (42000): 索引 'idx_cust' は表 'orders' には存在しません。
1176 (42000): Key 'idx_cust' doesn't exist in table 'orders'

原因へ移動

  1. 01FORCE INDEX の索引名のタイプミス
  2. 02索引を削除・置き換えたのに、クエリに古いヒントが残っている
  3. 03索引名ではなく列名を書いた(主キーは PRIMARY)

実機検証済みMySQL 8.4.13 で再現・解決を確認 · 2026-11-30 · コンテナイメージ mysql:8.4

ERROR 1176 は FORCE INDEX (...) や USE INDEX (...) などのインデックスヒントで発生します。ヒントの名前を表の索引名と照合し、一致しなければクエリを止めます。

いちばん早い確認方法は information_schema.statistics の索引名一覧です。以下はMySQL 8.4で再現した3つのケースです。

01FORCE INDEX の索引名のタイプミス

クエリは FORCE INDEX (idx_cust) を指定しましたが、cust_id の索引の実際の名前は idx_orders_cust です。

事前準備
事前準備lab_a で実行
CREATE TABLE orders (order_id INT PRIMARY KEY, cust_id INT, created_at DATE, KEY idx_orders_cust (cust_id));
INSERT INTO orders VALUES (1, 10, '2026-11-01'), (2, 20, '2026-11-02'), (3, 10, '2026-11-03');
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
再現lab_a で実行
SELECT order_id FROM orders FORCE INDEX (idx_cust) WHERE cust_id = 10;
✕ エラー
ERROR 1176 (42000): Key 'idx_cust' doesn't exist in table 'orders'
原因の確認lab_a で実行
SELECT index_name, column_name FROM information_schema.statistics
 WHERE table_schema = DATABASE() AND table_name = 'orders'
 ORDER BY index_name, seq_in_index;
OUTPUT
+-----------------+-------------+
| INDEX_NAME      | COLUMN_NAME |
+-----------------+-------------+
| idx_orders_cust | cust_id     |
| PRIMARY         | order_id    |
+-----------------+-------------+
2 rows in set (0.00 sec)

表にあるのは idx_orders_cust(cust_id)と PRIMARY だけです。idx_cust はありません。

確認lab_a で実行
EXPLAIN FORMAT=TREE SELECT order_id FROM orders FORCE INDEX (idx_orders_cust) WHERE cust_id = 10\G
OUTPUT
*************************** 1. row ***************************
EXPLAIN: -> Covering index lookup on orders using idx_orders_cust (cust_id=10)  (cost=0.35 rows=1)

1 row in set (0.00 sec)

正しい名前にすると EXPLAIN に Covering index lookup on orders using idx_orders_cust と表示され、注文1と3が取得できました。

対処後の再実行lab_a で実行
SELECT order_id FROM orders FORCE INDEX (idx_orders_cust) WHERE cust_id = 10;
✓ 成功
+----------+
| order_id |
+----------+
|        1 |
|        3 |
+----------+
2 rows in set (0.00 sec)

02索引を削除・置き換えたのに、クエリに古いヒントが残っている

idx_orders_cust を複合索引 ix_cust_created (cust_id, created_at) に置き換えました。アプリのクエリにはまだ USE INDEX (idx_orders_cust) があるため、索引変更の直後から失敗します。

事前準備
事前準備lab_a で実行
CREATE TABLE orders (order_id INT PRIMARY KEY, cust_id INT, created_at DATE, KEY idx_orders_cust (cust_id));
INSERT INTO orders VALUES (1, 10, '2026-11-01'), (2, 20, '2026-11-02'), (3, 10, '2026-11-03');
ALTER TABLE orders DROP INDEX idx_orders_cust, ADD INDEX ix_cust_created (cust_id, created_at);
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0
再現lab_a で実行
SELECT order_id FROM orders USE INDEX (idx_orders_cust) WHERE cust_id = 10 ORDER BY created_at;
✕ エラー
ERROR 1176 (42000): Key 'idx_orders_cust' doesn't exist in table 'orders'
原因の確認lab_a で実行
SELECT index_name, column_name FROM information_schema.statistics
 WHERE table_schema = DATABASE() AND table_name = 'orders'
 ORDER BY index_name, seq_in_index;
OUTPUT
+-----------------+-------------+
| INDEX_NAME      | COLUMN_NAME |
+-----------------+-------------+
| ix_cust_created | cust_id     |
| ix_cust_created | created_at  |
| PRIMARY         | order_id    |
+-----------------+-------------+
3 rows in set (0.00 sec)

idx_orders_cust はなく、ix_cust_created が cust_id と created_at を含んでいます。

確認lab_a で実行
EXPLAIN FORMAT=TREE SELECT order_id FROM orders WHERE cust_id = 10 ORDER BY created_at\G
OUTPUT
*************************** 1. row ***************************
EXPLAIN: -> Covering index lookup on orders using ix_cust_created (cust_id=10)  (cost=0.45 rows=2)

1 row in set (0.00 sec)

ヒントを外すだけで解決しました。EXPLAIN ではオプティマイザが自分で ix_cust_created を選んでいます。ヒントを新しい名前に変えても構いません。

対処後の再実行lab_a で実行
SELECT order_id FROM orders WHERE cust_id = 10 ORDER BY created_at;
✓ 成功
+----------+
| order_id |
+----------+
|        1 |
|        3 |
+----------+
2 rows in set (0.00 sec)

03索引名ではなく列名を書いた(主キーは PRIMARY)

FORCE INDEX (order_id) は列名です。ヒントには索引名を書く必要があり、主キーの索引名は常に PRIMARY です。

事前準備
事前準備lab_a で実行
CREATE TABLE orders (order_id INT PRIMARY KEY, cust_id INT, created_at DATE, KEY idx_orders_cust (cust_id));
INSERT INTO orders VALUES (1, 10, '2026-11-01'), (2, 20, '2026-11-02'), (3, 10, '2026-11-03');
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0
再現lab_a で実行
SELECT order_id, cust_id FROM orders FORCE INDEX (order_id) WHERE order_id BETWEEN 1 AND 2;
✕ エラー
ERROR 1176 (42000): Key 'order_id' doesn't exist in table 'orders'
原因の確認lab_a で実行
SELECT index_name, column_name FROM information_schema.statistics
 WHERE table_schema = DATABASE() AND table_name = 'orders'
 ORDER BY index_name, seq_in_index;
OUTPUT
+-----------------+-------------+
| INDEX_NAME      | COLUMN_NAME |
+-----------------+-------------+
| idx_orders_cust | cust_id     |
| PRIMARY         | order_id    |
+-----------------+-------------+
2 rows in set (0.00 sec)

order_id の索引名は order_id ではなく PRIMARY です。

確認lab_a で実行
EXPLAIN FORMAT=TREE SELECT order_id, cust_id FROM orders FORCE INDEX (PRIMARY) WHERE order_id BETWEEN 1 AND 2\G
OUTPUT
*************************** 1. row ***************************
EXPLAIN: -> Filter: (orders.order_id between 1 and 2)  (cost=0.46 rows=1)
    -> Index range scan on orders using PRIMARY over (1 <= order_id <= 2)  (cost=0.46 rows=1)

1 row in set (0.00 sec)

FORCE INDEX (PRIMARY) にすると実行でき、EXPLAIN に Index range scan on orders using PRIMARY と表示されました。

対処後の再実行lab_a で実行
SELECT order_id, cust_id FROM orders FORCE INDEX (PRIMARY) WHERE order_id BETWEEN 1 AND 2;
✓ 成功
+----------+---------+
| order_id | cust_id |
+----------+---------+
|        1 |      10 |
|        2 |      20 |
+----------+---------+
2 rows in set (0.00 sec)

よくある質問

ERROR 1176 を直すには索引名をどう確認しますか?

information_schema.statistics でその表の index_name と column_name を検索するか、SHOW INDEX FROM orders を使います。ヒントにはその名前をそのまま書きます。

MySQLの主キーの索引名は何ですか?

PRIMARY です。列名ではなく FORCE INDEX (PRIMARY) と書きます。

インデックスヒントは必要ですか?

不要なことも多いです。索引を置き換えたケースでは、ヒントを外すとオプティマイザが新しい索引を自分で選びました(EXPLAIN FORMAT=TREE で確認)。

検証方法

シナリオごとに新しいDBユーザーを作成して開始します。スクリプトが事前準備 → エラー再現(エラーコードの一致を確認)→ 対処 → 元の文の再実行の順に実行し、表示している出力はDBが実際に返した結果をスクリプトがそのまま保存したものです。

lab_a・lab_b はテスト用の一般ユーザー(同名のデータベースがデフォルトで、その中で全権限)、root は管理者です。 MySQL 8.4.13