mysql查询需要很长时间

时间:2014-12-20 15:51:26

标签: php mysql views drupal-views

我的下面的查询花费了超过10秒。我如何优化此查询。请建议添加索引的字段。 此查询是距离搜索的drupal视图。

    SELECT DISTINCT location.lid AS location_lid, node.nid AS nid, location.name AS location_name, location_phone.phone AS location_phone_phone, location_fax.fax AS location_fax_fax, node.title AS node_title, node.language AS node_language, location.additional AS location_additional, location.city AS location_city, location.latitude AS location_latitude, location.longitude AS location_longitude, location.country AS location_country, location.postal_code AS location_postal_code, location.province AS location_province, location.street AS location_street, field_data_field_showroom_level.field_showroom_level_value AS field_data_field_showroom_level_field_showroom_level_value, field_data_field_showroom_type.field_showroom_type_value AS field_data_field_showroom_type_field_showroom_type_value, field_data_field_showroom_inventory_records.field_showroom_inventory_records_value AS field_data_field_showroom_inventory_records_field_showroom_i, field_data_field_record_type_id.field_record_type_id_value AS field_data_field_record_type_id_field_record_type_id_value, field_data_field_toto_gallery.field_toto_gallery_value AS field_data_field_toto_gallery_field_toto_gallery_value, 'node' AS field_data_field_showroom_location_node_entity_type, 'node' AS field_data_field_day_of_operation_node_entity_type, 'node' AS field_data_field_day_of_operation_comments_node_entity_type, 'node' AS field_data_field_showroom_open_node_entity_type, 'node' AS field_data_field_showroom_close_node_entity_type, 'node' AS field_data_field_appointment_needed_node_entity_type, (COALESCE(ACOS(0.83308162381476*COS(RADIANS(location.latitude))*(0.098658826854837*COS(RADIANS(location.longitude)) + -0.99512131716873*SIN(RADIANS(location.longitude))) + 0.55315007734083*SIN(RADIANS(location.latitude))), 0.00000)*6371570.9190939) AS location_distance, 'node' AS field_data_field_salesforce_id_node_entity_type, 'node' AS field_data_field_multiple_product_sku_node_entity_type, 'node' AS field_data_field_product_types_node_entity_type, 'node' AS field_data_field_toto_gallery_node_entity_type, 'node' AS field_data_field_showroom_level_node_entity_type
FROM 
node node
LEFT JOIN location_instance location_instance ON node.vid = location_instance.vid
LEFT JOIN location location ON location_instance.lid = location.lid
INNER JOIN field_data_field_toto_gallery field_data_field_toto_gallery ON node.nid = field_data_field_toto_gallery.entity_id AND (field_data_field_toto_gallery.entity_type = 'node' AND field_data_field_toto_gallery.deleted = '0')
LEFT JOIN field_data_field_multiple_product_sku field_data_field_multiple_product_sku ON node.nid = field_data_field_multiple_product_sku.entity_id AND (field_data_field_multiple_product_sku.entity_type = 'node' AND field_data_field_multiple_product_sku.deleted = '0')
LEFT JOIN location_phone location_phone ON location_instance.lid = location_phone.lid
LEFT JOIN location_fax location_fax ON location_instance.lid = location_fax.lid
LEFT JOIN field_data_field_showroom_level field_data_field_showroom_level ON node.nid = field_data_field_showroom_level.entity_id AND (field_data_field_showroom_level.entity_type = 'node' AND field_data_field_showroom_level.deleted = '0')
LEFT JOIN field_data_field_showroom_type field_data_field_showroom_type ON node.nid = field_data_field_showroom_type.entity_id AND (field_data_field_showroom_type.entity_type = 'node' AND field_data_field_showroom_type.deleted = '0')
LEFT JOIN field_data_field_showroom_inventory_records field_data_field_showroom_inventory_records ON node.nid = field_data_field_showroom_inventory_records.entity_id AND (field_data_field_showroom_inventory_records.entity_type = 'node' AND field_data_field_showroom_inventory_records.deleted = '0')
LEFT JOIN field_data_field_record_type_id field_data_field_record_type_id ON node.nid = field_data_field_record_type_id.entity_id AND (field_data_field_record_type_id.entity_type = 'node' AND field_data_field_record_type_id.deleted = '0')
WHERE ((    (node.status = '1') 
        AND (node.type IN  ('showrooms')) 
           AND (location.latitude > '32.859795378699'
           AND location.latitude < '34.306986221301'
           AND location.longitude > '-85.206632058424'
           AND location.longitude < '-83.469478341576')
        AND ((COALESCE(ACOS(0.83308162574562*COS(RADIANS(location.latitude))*(0.098658823381208*COS(RADIANS(location.longitude)) + -0.99512131751311*SIN(RADIANS(location.longitude))) + 0.55315007443282*SIN(RADIANS(location.latitude))), 0.00000)*6371570.9191628) < '80467.35')
        AND (location.province = 'GA') 
        AND (location.city LIKE 'Atlanta' ESCAPE '\\') )
        AND( (field_data_field_toto_gallery.field_toto_gallery_value = '1')
           OR (field_data_field_multiple_product_sku.field_multiple_product_sku_value NOT LIKE 'NULL' ESCAPE '\\') ))

ORDER BY field_data_field_showroom_level_field_showroom_level_value DESC, 
         field_data_field_showroom_type_field_showroom_type_value DESC, 
         location_distance ASC, 
         field_data_field_showroom_inventory_records_field_showroom_i DESC,
         field_data_field_record_type_id_field_record_type_id_value ASC,          
         field_data_field_toto_gallery_field_toto_gallery_value DESC
LIMIT 10 OFFSET 0

2 个答案:

答案 0 :(得分:0)

我建议您了解MySQL的PROCEDURE ANALYSE功能:http://dev.mysql.com/doc/refman/5.0/en/procedure-analyse.html

它会告诉您哪些联接可能没有可以使用的索引。从仅查看您的查询但不知道表格的结构或数据,我们无法给您一个很好的adivce。通常,您应该在每个列上都有一个索引,在查询的JOIN和ORDER BY部分中使用,并且您可能对WHERE子句中使用的列有一些索引。

答案 1 :(得分:0)

以下是一些可以立即尝试查看是否可以提高性能的方法。

  1. location上的(province, city, latitude, longitude, lid)表格上尝试复合索引。希望这个复合索引可以加速您所在位置的WHERE条件计算。
  2. 确保您的latitudelongitude列包含FLOATDOUBLE数据类型。
  3. node上的(status, type, vid)表格上尝试复合索引。该索引应有助于找到node子句中的确切WHERE条记录,并加快您的加入速度。
  4. WHERE子句中消除球面几何计算。您已经有了边界框范围限制,并且您已经按距离进行排序,因此您不应该通过这种方式获得太多额外位置。而且,如果你这样做,他们的距离不应超过4 / pi。
  5. 你可以摆脱SELECT DISTINCT并使用SELECT吗?查找不同的值是数据密集型操作。
  6. 有关距离计算和边界框优化的一些有用信息,请参阅此内容。 http://www.plumislandmedia.net/mysql/haversine-mysql-nearest-loc/