I have two tables, one with store data:
Table "public.store"
Column | Type | Modifiers
-----------+------------------------+-----------
id | integer | not null
hul_code | character varying |
latitude | numeric |
longitude | numeric |
name | character varying(100) |
Indexes:
"store_pkey" PRIMARY KEY, btree (id)
"store-hlcode-idx" btree (hul_code)
Another with the sales data:
Table "public.sale_data"
Column | Type | Modifiers
-----------+-------------------+-----------
id | integer | not null
hul_code | character varying |
partyname | character varying |
rscode | character varying |
rsname | character varying |
cluster | character varying |
channel | character varying |
basepack | character varying |
total_16 | numeric |
total_17 | numeric |
total_18 | numeric |
Indexes:
"sale_data_pkey" PRIMARY KEY, btree (id)
"hul-code-idx" btree (hul_code)
"sd-bp-idx" btree (basepack)
"sd-rscd-idx" btree (rscode)
The second table has over 11 million rows.
I'm trying to get the list of all stores which had items coming from a particular Regional Supplier (identified by rscode
) by using a query like:
Select s.hul_code, s.name, s.latitude, s.longitude
from store s
Where hul_code in
(Select Distinct(hul_code) as hulcode
from sale_data
where rscode='133955')
AND s.latitude is not null;
This query takes more than 1.5 to 2 seconds on average.
How to increase the speed of this query?
IN
.-
in them.(rscode, hul_code)
might help.rscode
? How many rows in tablestore
? Is referential integrity betweensale_date.hul_code
andstore.hul_code
enforced (with a FK constraint)? How many distincthul_code
insale_data
?