I have two tables with about 2.5 million rows each. One has address information and the other has name information. I've indexed the LOWER() value of the last name column and also the zip code column:
CREATE INDEX last_name_idx ON providers(LOWER(Last_Name));
CREATE INDEX zip_idx ON provider_addresses(zip);
I need to perform a query similar to this (the zip code list is all of the zip codes within x amount of miles):
My thought process for designing the query this way is that the list returned by the inner query is only about 45,000 rows which should speed up the WHERE clause of the outer query. However, the problem I am having is that this query takes too long (about 25-35 seconds). Is there any way that I can speed up the execution?
-Greg :-Q
CREATE INDEX last_name_idx ON providers(LOWER(Last_Name));
CREATE INDEX zip_idx ON provider_addresses(zip);
I need to perform a query similar to this (the zip code list is all of the zip codes within x amount of miles):
Code:
SELECT id FROM (SELECT p.id, p.last_name FROM provider_addresses pa INNER JOIN providers p ON p.id = pa.id WHERE pa.practice_zip IN ('19130','19188','19121','19196','19173','19162','19187','19093','19197','19193','19194','19192','19110','19190','19191','19183','19092',
'19195','19184','19102','19176','19178','19099','19170','19123','19171','19255','19105','19104','19103','19109','19107','19175','19122',
'19244','19182','19132','19177','19181','19106','19172','19133','19146','19147','19131','19125','19129','19139','19101','08101','19145',
'19160','19148','19143','19140','08102','19134','08103','19027','19151','19144','19179','19137','19004','19142','08105','08104','19141',
'19066','19127','19124','19050','19185','08110','19112','19082','19120','19128','19096','19072','19023','08107','19138','08030','19126',
'19153','08109','19064','19108','08108','19079','19150','19012','19083','19003','19119','08063','19149','08106','08059','19135','19018',
'19032','08093','19036','19035','19041','19111','08099','19026','19095','19118','19161','08031','19098','08035','08002','19074','19043',
'08033','08076','08065','19070','19152','08077','19076','19444','19113','08086','08052','19010','19136','08007','19029','08097','19038',
'19031','19046','19033','08078','08034','08096','19008','19075','08066','08057','19078','08045','19428','19429','19081','19085','08029',
'08049','19001','19115','19462','19088','08027','19114','19094','08083','08061','08020','19022','19034','08090','19086','19091','19065',
'19025','19087','08003','19080','08051','19089','19090','08084','19006','19073','19154','19405','19489','19009','19015','08075','08056',
'19019','19116','19424','19016','19063','19155','19013','08054','19409','19422','19488','08043','08021','19487','08014','19333','08032',
'08026','19401','19406','19044','19040','19483','08053','19028','19037','08010','19052','19002','19437','19039','19485','19014','19484',
'08080','19020','19404','08046','19312','19053','08081','08091','08073','19403','19021','08071','19017','08085','19436','08074','08062',
'08012','19061','18974','18966','19301','19477','19455','19454','19407','19397','19399','19482','19496','18991','19486','19481','19408',
'19342','08036','19415','19339','19490','19319','08025','19456','08016','18976','08028','19703','19340','19007','19331','18954','19446',
'18936','08060','19048','08009','08048','19049','19373','19056','19420','19395','19355','19423','08067','18929','08039','19426','08055',
'19047','19057','19810','19887','18915','19432','08004','19453','19443','19493','19494','19495','19430','19474','19058','08312','19055',
'18956','08518','18914','19809','19440','19460','19030','18925','18943','19382','18980','19803','19451','18940','19381','08343','19380',
'19388','18932','19383','08018','19897','19054','08554','08069','18912','08098','19438','18922','19896','08089','18946','19802','19450',
'19067','18964','19441','08011','08322','19366','19732','19317','19442','19473','18927','19894','19886','19710','19468'))
WHERE LOWER(LAST_NAME) LIKE LOWER('smi%')
My thought process for designing the query this way is that the list returned by the inner query is only about 45,000 rows which should speed up the WHERE clause of the outer query. However, the problem I am having is that this query takes too long (about 25-35 seconds). Is there any way that I can speed up the execution?
-Greg :-Q