I have a query (see below) that I have a custom UDF that is used to calculate whether certain points are inside the polygon (first query in UNION) or circular (second query in UNION).
select e.inquiry_match_type_id , a.geo_boundary_id , GeoBoundaryContains(c.tpi_geo_boundary_coverage_type_id, 29.287437, -95.055807, a.lat, a.lon, a.geo_boundary_vertex_id ) in_out , e.inquiry_id , e.external_id , COALESCE(f.inquiry_device_id,0) inquiry_device_id , b.external_info1 , b.external_info2 , b.geo_boundary_id , b.geo_boundary_type_id from geo_boundary_vertex a join geo_boundary b on b.geo_boundary_id = a.geo_boundary_id join trackpoint_index_geo_boundary_mem c on c.geo_boundary_id = b.geo_boundary_id join trackpoint_index_mem d on d.trackpoint_index_id = c.trackpoint_index_id join inquiry_mem e on e.inquiry_id = b.inquiry_id left outer join inquiry_device_mem f on f.inquiry_id = e.inquiry_id and f.device_id = 3201 where d.trackpoint_index_id = 3127 and b.geo_boundary_type_id = 3 and e.expiration_date >= now() group by a.geo_boundary_id UNION select e.inquiry_match_type_id , b.geo_boundary_id , GeoBoundaryContains( c.tpi_geo_boundary_coverage_type_id, 29.287437, -95.055807, b.centroid_lat, b.centoid_lon, b.radius ) in_out , e.inquiry_id , e.external_id , COALESCE(f.inquiry_device_id,0) inquiry_device_id , b.external_info1 , b.external_info2 , b.geo_boundary_id , b.geo_boundary_type_id from geo_boundary b join trackpoint_index_geo_boundary_mem c on c.geo_boundary_id = b.geo_boundary_id join trackpoint_index_mem d on d.trackpoint_index_id = c.trackpoint_index_id join inquiry_mem e on e.inquiry_id = b.inquiry_id left outer join inquiry_device_mem f on f.inquiry_id = e.inquiry_id and f.device_id = 3201 where d.trackpoint_index_id = 3127 and b.geo_boundary_type_id = 2 and e.expiration_date >= now() group by b.geo_boundary_id
When I run the explanation for the request, I get the following:
id select_type table type possible_keys key key_len ref rows Extra ------ -------------- ---------- ------- --------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------- ---------- ------------------------ ------- ------------------------------- 1 PRIMARY d const PRIMARY PRIMARY 4 const 1 Using temporary; Using filesort 1 PRIMARY c ref PRIMARY,fk_mtp_idx_geo_boundary_mtp_idx,fk_mtp_idx_geo_boundary_geo_boundary,fk_mtp_idx_geo_boundary_mtp_mem_idx,fk_mtp_idx_geo_boundary_geo_boundary_mem fk_mtp_idx_geo_boundary_mtp_idx 4 const 9 1 PRIMARY b eq_ref PRIMARY,fk_geo_boundary_inquiry,fk_geo_boundary_geo_boundary_type PRIMARY 4 gothim.c.geo_boundary_id 1 Using where 1 PRIMARY e eq_ref PRIMARY PRIMARY 4 gothim.b.inquiry_id 1 Using where 1 PRIMARY f ref fk_inquiry_device_mem_inquiry fk_inquiry_device_mem_inquiry 4 gothim.e.inquiry_id 2 1 PRIMARY a ref fk_geo_boundary_vertex_geo_boundary fk_geo_boundary_vertex_geo_boundary 4 gothim.b.geo_boundary_id 11 Using where 2 UNION d const PRIMARY PRIMARY 4 const 1 Using temporary; Using filesort 2 UNION c ref PRIMARY,fk_mtp_idx_geo_boundary_mtp_idx,fk_mtp_idx_geo_boundary_geo_boundary,fk_mtp_idx_geo_boundary_mtp_mem_idx,fk_mtp_idx_geo_boundary_geo_boundary_mem fk_mtp_idx_geo_boundary_mtp_idx 4 const 9 2 UNION b eq_ref PRIMARY,fk_geo_boundary_inquiry,fk_geo_boundary_geo_boundary_type PRIMARY 4 gothim.c.geo_boundary_id 1 Using where 2 UNION e eq_ref PRIMARY PRIMARY 4 gothim.b.inquiry_id 1 Using where 2 UNION f ref fk_inquiry_device_mem_inquiry fk_inquiry_device_mem_inquiry 4 gothim.e.inquiry_id 2 (null) UNION RESULT <union1,2> ALL (null) (null) (null) (null) (null) Using filesort 12 record(s) selected [Fetch MetaData: 1ms] [Fetch Data: 5ms]
Now I can split the queries and use the ORDER BY NULL trick to get rid of filesort, however, when I try to add that until the end of UNION it does not work.
I am considering the possibility of splitting a query into 2 queries or perhaps rewriting it completely so as not to use UNION (although this, of course, is a bit more complicated). Another thing that I work against me is that we have this in production, and I would like to limit the changes - I would really like to add ORDER BY NULL to the end of the query and make it with it, but it does not work with UNION .
Any help would be greatly appreciated.