Skip to content
Advertisement

get one record at a time from joint table

I want to get a record from a joint table at a time. But I don’t hope the tables are joined as a whole.

The actual tables are as follow.

By specifying a cluster_id, I want to get one content name at a time where contents.status=2 and (contents name, cluster_id) pair is in content_cluster. The query in sql is something like follow.

However, I don’t want the tables to be joined as a whole every time as I have to do it frequently and the tables are large. Is there any efficient way to do this? I can add some indices to the tables. What should I do?

Advertisement

Answer

I would try writing the query like this:

Then create the following indexes:

  • content(status, priority, last_registered_day, name, content_id, name)
  • content_cluster(content_id, cluster_id).

The goal is for the execution plan to scan the index for context and for each row, look up to see if there is a match in content_cluster. The query stops at the first match.

I can’t guarantee that this will generate that plan (avoiding the sort), but it is worth a try.

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