Skip to content
Advertisement

SQL Select query optimization with indexing

A posts table contains 1 million rows. This table has a field with the name poster_id.

I have a list of followers by this poster_id.

I am trying to get a list of all activities from this followers (35 in this case but less or more is possible) in the last 48 hours.

I use this query:

Problem:

This query takes too long (0.4000 seconds) to execute.

The poster_id has an index of the post table.

How can I make this query faster?

Advertisement

Answer

try avoid the IN clause and use a join

could be the value in the IN clause are form some subquery in this case ypou could use the related subquery instead of the UNION …..

WHERE IN clase is the same as serverl OR condition (several internal subquery) .. instead INNER JOIN just check the values in a single operation

for better performance, instead of you actual index on poster_id, you could try using a redundant index adding all the columns you select in your index eg:

create index my_index on posts (poster_id, post_id, topic_id , post_time )

in this way all the query value are obtained using the index and the query don’t need to access at the table ..

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement