MySQL question: indexes on columns!
I have a MySQL question
I have two tables (posts and authors) in a one to many relationship (since every post is written by an author and an author can write multiple posts).
So here are the tables:
Authors: id: BIGINT, name: VARCHAR (255) Posts: id: BIGINT, author_id: BIGINT, body: TEXT
I have 700,000 posts and 60,000 authors.
If I select an author (e.g. author_id = 45) and I want a random post to write him, I write:
SELECT * FROM Posts WHERE author_id = 45 ORDER BY RAND() LIMIT 1;
I know this is correct, but when I got 4000 concurrent people on the internet it takes about 6 seconds ..
Maybe indexing the author_id column in the Posts table can speed things up?
Thanks everyone! :)
source to share
Indexing should reflect the most popular WHERE scenarios.
In this particular case, create your index and then change your query to this:
SELECT id,author_id,body
FROM Posts
WHERE author_id = 45
ORDER BY RAND()
LIMIT 1;
This will prevent searching for the schema before searching, thereby improving performance.
SELECT * is evil for high frequency queries.
source to share
An index [possibly grouped] on Author_id will help definitively.
An additional risk factor appears in the ORDER BY RAND () part. Essentially this clause forces SQL to dynamically assign a random number to each row (for a given Author_id) and order them. This can become a bottleneck as some prolific authors start to have hundreds and thousands of posts.
source to share