mysql - Is there any way to simplify union query while using multiple tables, with reference to main table ( Performance Optimization ) -
i wonder there way improve query, speed / simplify while using multiple tables union
clause reference 1 main table.
following scenario, here table reference
main table , geo_loc1
,geo_loc2
,geo_loc2
other tables, joined based on key1
, key2
, these 2 keys exists in table.
select reference.*, geo_loc1.bathy, geo_loc1.gravity, geo_loc1.magnet, 'data1' type reference,geo_loc1 reference.latitude between -30 , -10 , reference.longitude between 10 , 50 , date( reference.st_date ) between '2000-07-05' , '2011-11-10' , reference.key1 = geo_loc1.key1 , reference.key2 = geo_loc1.key2 union select reference.*, geo_loc2.bathy, null gravity, geo_loc2.magnet, 'data2' type reference,geo_loc2 reference.latitude between -30 , -10 , reference.longitude between 10 , 50 , date( reference.st_date ) between '2000-07-05' , '2011-11-10' , reference.key1 = geo_loc2.key1 , reference.key2 = geo_loc2.key2 union select reference.*, null bathy, null gravity, geo_loc3.magnet, 'data3' type reference,geo_loc3 reference.latitude between -30 , -10 , reference.longitude between 10 , 50 , date( reference.st_date ) between '2000-07-05' , '2011-11-10' , reference.key1 = geo_loc3.key1 , reference.key2 = geo_loc3.key2
whether query can simplified or improved ?
for performance can see 2 things might change.
1) change union
union all
. union
check results duplicates adds overhead don't need.
2) consider changing date( reference.st_date ) between '2000-07-05' , '2011-11-10'
not use date function.
if reference.st_date proper date column , didn't need type conversion db use index based on date quick lookup. doing type conversion db have scan conversion each row.
you index on key1,key2,st_date,latitude,longitude
. using date function you're using first 2 columns of index. either change st_date date or change values you're comparing conversion isn't required.
also consider using proper inner join
rather adding join conditions where
clauses. it's more readable using proper syntax , less error prone.
example using union all
, inner join
should this. note untested should idea.
edit removed use of date() function should speed further
select reference.*, geo_loc1.bathy, geo_loc1.gravity, geo_loc1.magnet, 'data1' type reference inner join geo_loc1 on reference.key1 = geo_loc1.key1 , reference.key2 = geo_loc1.key2 reference.latitude between -30 , -10 , reference.longitude between 10 , 50 , reference.st_date between '2000-07-05 00:00:00' , '2011-11-10 23:59:59' union select reference.*, geo_loc2.bathy, null gravity, geo_loc2.magnet, 'data2' type reference inner join geo_loc2 on reference.key1 = geo_loc2.key1 , reference.key2 = geo_loc2.key2 reference.latitude between -30 , -10 , reference.longitude between 10 , 50 , reference.st_date between '2000-07-05 00:00:00' , '2011-11-10 23:59:59' union select reference.*, null bathy, null gravity, geo_loc3.magnet, 'data3' type reference inner join geo_loc3 on reference.key1 = geo_loc3.key1 , reference.key2 = geo_loc3.key2 reference.latitude between -30 , -10 , reference.longitude between 10 , 50 , reference.st_date between '2000-07-05 00:00:00' , '2011-11-10 23:59:59'
Comments
Post a Comment