我有以下兩個(簡化例子的緣故)表在我的MySQL數據庫:MySQL:如何使用「JOIN」和「order_by」加速「Count()」查詢?
DESCRIBE appname_item;
-----------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(200) | NO | | NULL | |
+-----------------+---------------+------+-----+---------+----------------+
DESCRIBE appname_favorite;
+---------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | MUL | NULL | |
| item_id | int(11) | NO | MUL | NULL | |
+---------------+----------+------+-----+---------+----------------+
我試圖獲得通過的收藏數量排序的項目列表。下面的查詢工作正常,但是Item表中有成千上萬條記錄,查詢需要幾分鐘才能完成。
這裏有解釋的結果,它提供了一些見解,爲什麼查詢是如此緩慢(類型爲「ALL」,「使用臨時」和「使用文件排序」都應該,如果可能避免。)
+----+-------------+--------------------+------+-----------------------------+-----------------------------+---------+-------------------------------+------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------------+------+-----------------------------+-----------------------------+---------+-------------------------------+------+---------------------------------+
| 1 | SIMPLE | appname_item | ALL | NULL | NULL | NULL | NULL | 574 | Using temporary; Using filesort |
| 1 | SIMPLE | appname_favorite | ref | appname_favorite_67b70d25 | appname_favorite_67b70d25 | 4 | appname.appname_item.id | 1 | |
+----+-------------+--------------------+------+-----------------------------+-----------------------------+---------+-------------------------------+------+---------------------------------+
我知道,優化查詢的最簡單的方法是添加一個索引,但我似乎無法弄清楚如何添加一個索引,涉及加入一個計數()查詢和ORDER_BY 。我還應該提到,我通過Django ORM運行它,所以寧願不更改SQL查詢,只是修復和微調數據庫以最有效的方式運行查詢。
我一直在試圖弄清楚這一點,所以任何幫助將不勝感激!
UPDATE
下面是已經在數據庫索引:
+--------------------+------------+-----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------------------+------------+-----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| appname_favorite | 0 | PRIMARY | 1 | id | A | 594 | NULL | NULL | | BTREE | |
| appname_favorite | 1 | appname_favorite_fbfc09f1 | 1 | user_id | A | 12 | NULL | NULL | | BTREE | |
| appname_favorite | 1 | appname_favorite_67b70d25 | 1 | item_id | A | 594 | NULL | NULL | | BTREE | |
+--------------------+------------+-----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
分析來自兩個表appname_item,appname_favorite – Prix
做ID的表'appname_favorite'有什麼差距? – Karolis