I have query like this:
SELECT msg.id, CONTENTS, UNIX_TIMESTAMP(msg.created_at) 'created_at',
category, msg.`uid`,name, user_name, profile_pic, STATUS,
isNotificationOn, onesignal_id, email, gender, phone,
users.id userId, UNIX_TIMESTAMP(users.createdAt) 'createdAt',
( SELECT COUNT(1)
FROM message_likes
WHERE uid = '006GA968vpb0O62xaOpKLuhR1cC3'
AND message_id = msg.id
) AS isLiked,
( SELECT COUNT(*)
FROM follower_list
WHERE follower_uid = '006GA968vpb0O62xaOpKLuhR1cC3'
AND following_uid = msg.uid) AS isFollowing,
COUNT(DISTINCT share.uid) + msg.shares 'shares',
COUNT(DISTINCT likes.uid) 'likes',
msg.`application`,
`isModified`, `modificationReason`, `isVoilation`, `voilationReason`,
`followers_count`, `following_count`
FROM user_messages msg
JOIN users ON msg.uid = users.uid
LEFT JOIN message_shares SHARE ON msg.id = share.message_id
left JOIN message_likes likes ON msg.id = likes.message_id
LEFT JOIN followers_count ON msg.uid = followers_count.following_uid
LEFT JOIN following_count ON msg.uid = following_count.follower_uid
GROUP BY msg.id
HAVING created_at > '0'
AND category LIKE '%%%'
AND msg.uid LIKE '%'
AND msg.application LIKE 'demo'
AND isLiked='1'
AND msg.id <= '999999'
AND isVoilation <= '1'
ORDER BY msg.id DESC
LIMIT 10 OFFSET 0
This will take more than 8 seconds where my data is about 20k rows per table.
But if I replace isLiked='1'
with isLiked='%'
it will execute within 1 second (60 - 70 ms) which is 1000 times faster. So what the mistake I am doing?
UPDATE: After moving conditions from having clause to where clause, i am getting responce in 2 seconds. Here is updated query:
SELECT msg.id, CONTENTS, UNIX_TIMESTAMP(msg.created_at) 'created_at',
category, msg.`uid`,name, user_name, profile_pic, STATUS,
isNotificationOn, onesignal_id, email, gender, phone,
users.id userId, UNIX_TIMESTAMP(users.createdAt) 'createdAt',
( SELECT COUNT(1)
FROM message_likes
WHERE uid = 'CqITnqfe5iZHT8x74zYxuEwzGMY2'
AND message_id = msg.id
) AS isLiked,
( SELECT COUNT(*)
FROM follower_list
WHERE follower_uid = 'CqITnqfe5iZHT8x74zYxuEwzGMY2'
AND following_uid = msg.uid) AS isFollowing,
COUNT(DISTINCT share.uid) + msg.shares 'shares',
COUNT(DISTINCT likes.uid) 'likes',
msg.`application`,
`isModified`, `modificationReason`, `isVoilation`, `voilationReason`,
`followers_count`, `following_count`
FROM user_messages msg
JOIN users ON msg.uid = users.uid
LEFT JOIN message_shares SHARE ON msg.id = share.message_id
left JOIN message_likes likes ON msg.id = likes.message_id
LEFT JOIN followers_count ON msg.uid = followers_count.following_uid
LEFT JOIN following_count ON msg.uid = following_count.follower_uid
WHERE category LIKE '%'
AND msg.uid like '%'
AND msg.application = 'shayari'
AND msg.id <= '999999'
AND isVoilation <= '1'
GROUP BY msg.id HAVING isLiked=1
ORDER BY likes.created_at desc
Now the glitch is if i remove order by
clause it will execute in few milliseconds and with order by
clause it is taking about 2-3 seconds.
Here is explain query:
SHOW CREATE TABLE
andEXPLAIN SELECT ...