Home > database >  Query with small LIMIT gets slow with many hits
Query with small LIMIT gets slow with many hits

Time:09-27

I have a slow Postgres query and I may need better indexes. But I lack experience with indexing, so I just indexed foreign keys. I use Postgres 14.3 and Django 3.0.

This is the slow query. It's only slow when the chat room has a lot of messages (like 10k ).

SELECT
    chat_message.info_date_str,
    auth_user.username,
    chat_message.id,
    chat_message.text_msg,
    chat_message.type_message,
    chat_message.url_file,
    chat_message.url_file,
    chat_message.was_removed,
    chat_message.was_removed
FROM chat_message
INNER JOIN user_profile_userprofile
    ON chat_message.owner_id = user_profile_userprofile.id
INNER JOIN auth_user
    ON user_profile_userprofile.user_id = auth_user.id
WHERE chat_message.chat_room_id = 7204712
AND NOT chat_message.delete_by::text LIKE '           
  • Related