php - How to optimize a SQL query using multiple tables -
I have this SQL query which captures 5 latest news posts. I want to make it so that it captures total likes and total news comments in the same question. But the query I created was slow in working with a large amount of data, so I am trying to see that I can get a better solution. Here's the following:
SELECT *, call `id` as` newcod`, (say select 'from' select number (*), where 'type' = "newspost" And `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` ` There is also a SQLfield: `limit of DESC 5
You can write it again by joining it, know mysql Especially when working with large data sets are:
SELECT n. *, `` `` `` `` `` `` `` `` `` `` `` `` `` `` `Id` as` `` `` `` `` `` `` `` `` `` ` 'NFETY Return' (code by WHERE `type '=" News Post "group by total choice of' choice 'as selection code, COUNT (*))` = N.`id` LEFT JOIN (SELECT post_id, COUNT (*) AS From the total comments, by `news_support 'group post_id) by ASCCC` `post_id` = n.`id` order n. DID `DESC border 5; The reason for this is that when you use joining above, MySQL will need to implement sub-class results when necessary for the first time, for example this query In the beginning, mySQL will give results:
, which when a largeSELECT post_id, COUNT (*) ASC as 'news_comments' by post_id group
memory tables and Hash to see fast in post_id Then for every row in
news,
, you should see only thetotal comments
from this hashed table, when you use a correlated subquery, it will be sent to < Code> Newsnews
is larger, will be punished in large numbers if the initial result set is small then you do not see the performance benefits and it is bad It is possible.Example on SQL Bedle
Finally, you want the related code to be
news-tags
andfavorites Directed in
. For this particular query I think the following indexes will help:Creating index IX_Likes_Code_Type on choice (code, type); Create news on news index IX_newcomments_post_id (post_id);
However you may need to split the index first into two:
create Index (code) on IX_Likes_Code; Create likes (types) at Indix IX_Likes_Type;
Comments
Post a Comment