1

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

Michał Szkudlarek
  • 1,443
  • 1
  • 21
  • 35

1 Answers1

0

It is a simple case. You need to use index on (https://dev.mysql.com/doc/refman/5.7/en/create-index.html):

messages.author_id

You do not need index on users, since primary key is by default indexed in Mysql.

Consider making use of query plan, which can help you in identifying performance issues: https://dev.mysql.com/doc/workbench/en/wb-performance-explain.html

With all above setup, your query should look like*:

SELECT 
  m.id
  , m.post_description
  , m.author_id
  , m.timeline_id
  , u.username 
FROM message m
join users u 
 on m.author_id = u.id
WHERE m.author_id NOT IN (24974,7920,1498,9020,0)
     AND m.timeline_id ='1868'
     AND m.id <= '28190' 
ORDER BY m.id 
DESC LIMIT 0 ,51;

*your original query will also be faster with above index changes, however I think that using implicit joins makes query more readable.

Edited:

As per @Panagiotis Kanavos, thanks Panagiotis, you might have to see in query plan what else causes slowness. It might occur that you need an index on m.timeline_id also.

And of course, you need to analyze table after creating an index (https://dev.mysql.com/doc/refman/5.7/en/analyze-table.html)

Edit2:

You can also consider using a foreign key, but this is not a performance tool, it is an integrity concept: https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

You can refer to that answer about FK and performance: Does introducing foreign keys to MySQL reduce performance

Community
  • 1
  • 1
Michał Szkudlarek
  • 1,443
  • 1
  • 21
  • 35