如何根据解释优化mysql查询。 (类型:全部

时间:2012-06-06 19:27:05

标签: mysql optimization sql-execution-plan

我运行以下mysql查询,并在第一个查询中看到类型为ALL。

   mysql> EXPLAIN  
           SELECT one.language_id as filter_id, 
                  one.language_name as filter_name, 
                  two.count as count 
           FROM   books_f9_languages one 
           INNER JOIN (SELECT language_id, 
                       count(*) as count 
                       FROM link_f9_books_lists 
                       WHERE books_list_id IN (1691,1,2,3,4,6,7,8,9,10,11,12,13,14,17,18,19,20,21,22,23,24,25,26,28,29,30,31,32,33,34,35,36,37,43,44,47,51,54,57,58,59,68,71,76,77,86,88,93,94,99,120,125,126,127,133,146,147,148,257,260,261,262,263,264,266,267,268,269,270,271,272,275,276,286,767,768,769,771,772,774,777,779,783,785,786,790,792,799,808,811,813,814,815,819,825,828,829,847,850,852,853,855,856,857,858,862,863,866,869,873,875,882,891,900,907,917,925,930,935,1092,1531,1532,1533,1534,1535,1536,1537,1538,1540,1541,1542,1543,1544,1545,1546,1547,1548,1549,1550,1551,1552,1553,1554,1556,1557,1558,1560,1561,1563,1564,1565,1567,1568,1569,1570,1571,1572,1574,1575,1576,1577,1578,1579,1580,1581,1582,1583,1584,1586,1588,1589,1590,1591,1592,1595,1597,1599,1600,1601,1603,1604,1605,1606,1607,1608,1609,1610,1612,1613,1614,1615,1616,1617,1620,1621,1622,1623,1624,1625,1627,1628,1629,1630,1632,1636,1637,1638,1639,1640,1642,1643,1644,1645,1646,1648,1649,1651,1652,1653,1654,1659,1660,1662,1665,1675,1677,1679,1680,1689,1692,1693,1696,1698,1699,1702,1703,1705,1711,1713,1714,1716,1717,1719,1720,1726,1728,1729,1732,1734,1735,1736,1738,1743,1744,1753,1754,1755,1756,1759,1786,1787,1788,1789,1790,1791,1792,1793,1794,1795,1797,1837,827) GROUP BY language_id) two 
          WHERE one.language_id = two.language_id 
          ORDER BY filter_name;

这是输出,

+----+-------------+---------------------+--------+---------------+---------------+---------+-----------------+------+-----------------------------------------------------------+
| id | select_type | table               | type   | possible_keys | key           | key_len | ref             | rows | Extra                                                     |
+----+-------------+---------------------+--------+---------------+---------------+---------+-----------------+------+-----------------------------------------------------------+
|  1 | PRIMARY     | <derived2>          | ALL    | NULL          | NULL          | NULL    | NULL            |    5 | Using temporary; Using filesort                           |
|  1 | PRIMARY     | one                 | eq_ref | PRIMARY       | PRIMARY       | 4       | two.language_id |    1 |                                                           |
|  2 | DERIVED     | link_f9_books_lists | range  | books_list_id | books_list_id | 4       | NULL            |  273 | Using where; Using index; Using temporary; Using filesort |
+----+-------------+---------------------+--------+---------------+---------------+---------+-----------------+------+-----------------------------------------------------------+
3 rows in set (0.01 sec)

我必须索引哪一列才能生成ALL,请使用索引。

感谢。

1 个答案:

答案 0 :(得分:1)

派生的临时表不能使用索引。

删除ORDER BY子句或按language_id排序应该解决这个问题。

或者,您可以尝试在books_f9_languages表上的language_id和language_name上添加复合索引。

要改进内部查询,请在book_list_id和language_id上​​为link_f9_books_lists表添加复合索引。