Forum Webscript.Ru

Общие => Базы данных => Тема начата: volod от 14 Февраля 2005, 11:59:55

Название: Почему не используется индекс?
Отправлено: volod от 14 Февраля 2005, 11:59:55
Запрос:
-----------------------{
SELECT
p.*,
s.short AS state_id,s.full AS state_full,
c.short AS country_id,c.full AS country_full
FROM billing_preferences AS p
LEFT JOIN states AS s ON (s.short=p.state)
LEFT JOIN countries AS c ON (c.short=p.country)
WHERE p.uid=25
}-----------------------
Его explain:
+----+-------------+-------+--------+---------------+-------+---------+-------+------+-------+
| id | select_type | table | type   | possible_keys | key   | key_len | ref   | rows | Extra |
+----+-------------+-------+--------+---------------+-------+---------+-------+------+-------+
|  1 | SIMPLE      | p     | const  | uid,uid_2     | uid   |       4 | const |    1 |       |
|  1 | SIMPLE      | s     | ALL    | NULL          | NULL  |    NULL | NULL  |  817 |       |
|  1 | SIMPLE      | c     | eq_ref | short         | short |       6 | const |    1 |       |
+----+-------------+-------+--------+---------------+-------+---------+-------+------+-------+

Индексы таблицы states:
+--------+------------+-----------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+
| Table  | Non_unique | Key_name        | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------+------------+-----------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+
| states |          0 | PRIMARY         |            1 | id_state      | A         |         269 |     NULL | NULL   |      | BTREE      |         |
| states |          0 | id_country      |            1 | id_country    | A         |         269 |     NULL | NULL   |      | BTREE      |         |
| states |          0 | id_country      |            2 | short         | A         |         269 |     NULL | NULL   |      | BTREE      |         |
| states |          0 | short_country   |            1 | short_country | A         |         269 |     NULL | NULL   |      | BTREE      |         |
| states |          0 | short_country   |            2 | short         | A         |         269 |     NULL | NULL   |      | BTREE      |         |
| states |          1 | id_country_2    |            1 | id_country    | A         |         269 |     NULL | NULL   |      | BTREE      |         |
| states |          1 | short_country_2 |            1 | short_country | A         |         269 |     NULL | NULL   |      | BTREE      |         |
+--------+------------+-----------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+

*****Вопрос*****

Интресно, почему для таблицы  states AS s при выборке не используются индексы.
Знает кто?
Название: Почему не используется индекс?
Отправлено: volod от 15 Февраля 2005, 10:32:33
Кажись, нашел ответ. Может, кому интересно будет.
Вот здесь (http://www.databasejournal.com/features/mysql/article.php/10897_1382791_2), похоже, почти та же ситуация (ну, +-).
А в моем случае там в запросе нужно было еще одно условие дописать, проверки еквивалентности даных с таблиц states & countries :). Недосмотрел малость.