我使用多个内连接构建了一个查询,执行时间太长。
我认为这是因为表索引
查询语句是:
SELECT DISTINCT
`result_authors`.`id`,
`result_authors`.`name`,
`result_authors`.`author_id`,
`platforms`.`platform`
FROM
`result_authors`
INNER JOIN
`results` ON `result_authors`.`result_id` = `results`.`id`
INNER JOIN
`queries` ON `queries`.`id` = `results`.`query_id`
INNER JOIN
`wall_queries` ON `wall_queries`.`query_id` = `queries`.`parent_id`
INNER JOIN
`walls` ON `walls`.`id` = `wall_queries`.`wall_id`
INNER JOIN
`platforms` ON `result_authors`.`platform_id` = `platforms`.`id`
WHERE
`walls`.`id` = 2
GROUP BY `result_authors`.`author_id` , `result_authors`.`id` , `result_authors`.`name` , `result_authors`.`profile` , `result_authors`.`platform_id` , `platforms`.`platform`
ORDER BY `result_authors`.`name` ASC;
查询说明
+------+-------------+----------------------------------------------------------------------------------------------------------------+----------------------------------+-------------+------------------------------------+------+-------------+----------------------------------------------+
| id | select_type | table | id | select_type | table | id | select_type | table |
+------+-------------+----------------------------------------------------------------------------------------------------------------+----------------------------------+-------------+------------------------------------+------+-------------+----------------------------------------------+
| NULL | const | PRIMARY | PRIMARY | 8 | const | 1 | 100 | Using index; Using temporary; Using filesort |
| NULL | ref | wall_queries_query_id_foreign,wall_queries_wall_id_foreign | wall_queries_wall_id_foreign | 8 | const | 1 | 100 | NULL |
| NULL | ref | PRIMARY,queries_parent_id_index | queries_parent_id_index | 8 | db_name.wall_queries.query_id | 1 | 100 | Using where; Using index |
| NULL | ref | PRIMARY,results_query_id_foreign,results_query_id_index | results_query_id_foreign | 9 | db_name.queries.id | 6402 | 100 | NULL |
| NULL | ref | result_authors_platform_id_foreign,result_authors_result_id_foreign,result_authors_result_id_platform_id_index | result_authors_result_id_foreign | 8 | db_name.results.id | 1 | 100 | Using where |
| NULL | eq_ref | PRIMARY | PRIMARY | 4 | db_name.result_authors.platform_id | 1 | 100 | NULL |
+------+-------------+----------------------------------------------------------------------------------------------------------------+----------------------------------+-------------+------------------------------------+------+-------------+----------------------------------------------+
此代码需要超过15分钟才能获得28000行。
表格
表:result_authors
+----+------------+---------------+-----------------------------------+-----------+-------------+---------------------+---------------------+
| id | author_id | name | profile | result_id | platform_id | created_at | updated_at |
+----+------------+---------------+-----------------------------------+-----------+-------------+---------------------+---------------------+
| 1 | 420000242 | Author Name 1 | https://twitter.com/profile_slug1 | 15452 | 2 | 2017-11-24 13:55:41 | 2017-11-24 14:13:13 |
| 2 | 400070242 | Author Name 2 | https://twitter.com/profile_slug2 | 5225 | 2 | 2017-11-24 13:58:03 | 2017-11-24 14:13:13 |
| 3 | 2200059092 | Author Name 3 | https://twitter.com/profile_slug3 | 487 | 2 | 2017-11-24 19:48:55 | 2017-11-24 19:48:55 |
+----+------------+---------------+-----------------------------------+-----------+-------------+---------------------+---------------------+
表:查询
+----+---------------+---------+-----------+-------------+---------------------+
| id | title | type | parent_id | category_id | updated_at |
+----+---------------+---------+-----------+-------------+---------------------+
| 1 | Query title 1 | public | 1 | 5 | 2017-10-24 13:21:17 |
| 2 | Query title 2 | private | 2 | 1 | 2017-10-31 16:23:09 |
| 3 | Query title 3 | public | 1 | 5 | 2017-10-31 16:20:38 |
+----+---------------+---------+-----------+-------------+---------------------+
表:墙壁
+----+--------------+---------------------+
| id | name | updated_at |
+----+--------------+---------------------+
| 10 | Wall title 1 | 2017-11-16 13:42:32 |
| 11 | Wall title 2 | 2017-11-16 15:01:49 |
| 12 | Wall title 3 | 2017-11-19 12:05:18 |
+----+--------------+---------------------+
表:平台
+----+-----------+------------------------------+---------------------+---------------------+
| id | platform | module | created_at | updated_at |
+----+-----------+------------------------------+---------------------+---------------------+
| 1 | facebook | \platforms\FacebookPlatform | 2017-09-15 00:54:49 | 2017-09-15 00:54:49 |
| 2 | twitter | \platforms\TwitterPlatform | 2017-09-15 00:54:49 | 2017-09-15 00:54:49 |
| 3 | wordpress | \platforms\WordpressPlatform | 2017-09-15 00:54:49 | 2017-09-15 00:54:49 |
+----+-----------+------------------------------+---------------------+---------------------+
结果如
+----+----------+-----------+----------+
| id | name | author_id | platform |
+----+----------+-----------+----------+
| 1 | Author 1 | 123 | twitter |
| 3 | Author 2 | 124 | facebook |
| 8 | Author 3 | 125 | twitter |
+----+----------+-----------+----------+
有助于提高SQL查询性能吗?
注意:我正在使用laravel查询构建器。