I am working on MySQL and facing an issue in server load time. here is my table's structure and query:
I need to fetch 50 rows from message
(more than 5.6 Million rows) table. The message
table has attributes like (id, description, author_id, timeline_id) and the condition is, each author_id
exist in users
(more than 38K rows) table.
The table creation script is:
CREATE TABLE IF NOT EXISTS `messages` (
`id` int(11) NOT NULL,
`post_description` text() NOT NULL
`author_id` int(11) NOT NULL,
`timeline_id` int(11) NOT NULL,
);
ALTER TABLE `messages`
ADD PRIMARY KEY (`id`);
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL,
`username` text() NOT NULL
);
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);
For this, I am using following SQL query:
SELECT
m.id
, m.post_description
, m.author_id
, m.timeline_id
, u.username
FROM message m, users u
WHERE m.timeline_id ='1868'
AND m.id <= '28190'
AND m.author_id NOT IN (24974,7920,1498,9020,0)
AND u.id=m.author_id
ORDER BY m.id
DESC LIMIT 0 ,51;
please give me suggestion to get required rows in quickest possible time.
Thanks