我的SQL是我可以优化这个MySQL查询吗?
SELECT authors.*, COUNT(*) FROM authors
INNER JOIN resources_authors ON authors.author_id=resources_authors.author_id
WHERE
resource_id IN
(SELECT resource_id FROM resources_authors WHERE author_id = '1313')
AND authors.author_id != '1313'
GROUP BY authors.author_id`
我在查询所有字段的索引,但我仍然获得Using temporary; Using Filesort
。
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY authors ALL PRIMARY NULL NULL NULL 16025 Using where; Using temporary; Using filesort
1 PRIMARY resources_authors ref author_id author_id 4 authors.author_id 3 Using where
2 DEPENDENT SUBQUERY resources_authors unique_subquery resource_id,author_id,resource_id_2 resource_id 156 func,const 1 Using index; Using where
如何改进我的查询或表结构来加快查询速度?
这里有一个SQL小提琴,如果你想尝试:http://sqlfiddle.com/#!2/96d57/2/0
我不认为'author_id!= 1313'可以有效地使用索引。 – Barmar
@MKhalidJunaid - 这将无法正常工作,因为永远不会返回任何结果。 –
为什么你会说ID必须是1313,然后说它不= 1313? ...基本上你说的是把它拉出来,资源授权者是1313,作者不是1313 –