My system is using PostgreSQL as the database. I am encountering an issue when using a query to join two large tables together. I’ll briefly describe the tables as follows:
Location:
- id: uuid
- name: string (indexes)
- country: string (indexes)
- number: string
Product:
- id: uuid
- name: string
- score: number (indexes)
- rate: number (indexes)
- report: number (indexes)
- lock: boolean (indexes)
- location_id: uuid (not null) (indexes)
Location and Product will be 1-1 relation unique. Location can be has product or not.
I used the following query:
select l.id, l.name, l.number, p.id as pId, p.name, p.score, p.rate
from location l
left join product p on p.location_id = l.id
where l.country = 'US'
and l.id < 'xxx-yyy-zzz'
and (p.name is not null or l.name is not null)
and p.score > 1 and p.rate > 4 and p.lock = false
order by id desc
limit 100
Note that both the product and location tables have a very large number of records. Each table contains around several million to tens of millions of records. How can I speed up the query execution? For some countries, it runs very fast, but for others, it runs slowly, even though there are fewer records.
2
Answers
You can try creating indicies on:
Note:
Further you can also try table partitioning based on you usage
Move the right table’s conditions into the
join
clause:and create a covering index (has all columns needed, in optimal order) on
product
:and this index on
location
is assist bothwhere
andorder by
clauses: