简体   繁体   English

SQL 查询优化在执行中花费大量时间

[英]SQL query optimization taking lot of time in execution

在此处输入图像描述

在此处输入图像描述

We have two tables one is properties and another one is property meta when we are getting data from one table "properties" , query only take less then one second in execution but when we are use join to get the data using bellow query from both tables its taking more then 5 second to fetch the data although we have only 12000 record in the tables , i think there is an issue in the sql query any help or suggestion will be appreciated.我们有两张表,一张是属性,另一张是属性元,当我们从一张表“属性”中获取数据时,查询只需要不到一秒钟的时间执行,但是当我们使用连接从两个表中使用波纹管查询获取数据时尽管我们在表中只有 12000 条记录,但它需要 5 秒以上的时间来获取数据,我认为 sql 查询中存在问题,任何帮助或建议将不胜感激。

SELECT 
        u.id, 
        u.property_title, 
        u.description, 
        u.city, 
        u.area,
        u.address, 
        u.slug, 
        u.latitude, 
        u.longitude, 
        u.sync_time, 
        u.add_date, 
        u.is_featured, 
        u.pre_construction, 
        u.move_in_date,
        u.property_status, 
        u.sale_price, 
        u.mls_number, 
        u.bedrooms, 
        u.bathrooms, 
        u.kitchens, 
        u.sub_area, 
        u.property_type, 
        u.main_image, 
        u.area_size as land_area, 
        pm7.meta_value as company_name, 
        pm8.meta_value as virtual_tour, 
        u.year_built, 
        u.garages 
    FROM 
        tbl_properties u 
            LEFT JOIN tbl_property_meta pm7 
                ON u.id = pm7.property_id 
            LEFT JOIN tbl_property_meta pm8 
                ON u.id = pm8.property_id 
    WHERE 
            u.status = 1 
        AND (pm7.meta_key = 'company_name') 
        AND (pm8.meta_key = 'virtual_tour') 
        AND (
                (
                        (   u.city = 'Delta' 
                        OR  u.post_code LIKE '%Delta%' 
                        OR  u.sub_area LIKE '%Delta%' 
                        OR  u.state LIKE '%Delta%') 
                    AND country = 'Canada'
                ) 
            OR  (
                        (   u.city = 'Metro Vancouver Regional District' 
                        OR  u.post_code LIKE '%Metro Vancouver Regional District%'
                        OR  u.sub_area LIKE '%Metro Vancouver Regional District%' 
                        OR  u.state LIKE '%Metro Vancouver Regional District%' )
                    AND country = 'Canada'
                )
            ) 
        AND 
            u.pre_construction ='0' 
    GROUP BY 
        u.id 
    ORDER BY 
        u.is_featured DESC, 
        u.add_date DESC

Try adding this compound index:尝试添加此复合索引:

ALTER TABLE tbl_property_meta ADD INDEX id_key (property_id, meta_key);

If it doesn't help make things faster, try this one.如果它不能帮助加快速度,试试这个。

ALTER TABLE tbl_property_meta ADD INDEX key_id (meta_key, property_id);

And, you should know that column LIKE '%somevalue' (with a leading % ) is a notorious performance antipattern, resistant to optimization via indexes.而且,您应该知道column LIKE '%somevalue' (带有前导% )是一种臭名昭著的性能反模式,无法通过索引进行优化。 (There's a way to create indexes for that shape of filter in PostgreSQL, but not in MariaDB / MySQL.) (有一种方法可以在 PostgreSQL 中为这种形状的过滤器创建索引,但在 MariaDB / MySQL 中没有。)

Add another column with the meta stuff;添加另一个包含元数据的列; throw city, post_code, sub_area, and state and probably some other things into it.将 city、post_code、sub_area 和 state 以及可能的其他一些东西扔进去。 Then build a FULLTEXT index on that column.然后在该列上建立一个FULLTEXT索引。 Then use MATCH(..) AGAINST("Delta Metro Vancouver Regional District") in the WHERE clause _instead of the LEFT JOINs (which are actually INNER JOINs ) and the really messy part of the WHERE clause.然后在 WHERE 子句中使用MATCH(..) AGAINST("Delta Metro Vancouver Regional District") _而不是LEFT JOINs JOINs(实际上是INNER JOINs )和 WHERE 子句中真正混乱的部分。

Also, the GROUP BY is probably unnecessary, thereby eliminating extra sort on the intermediate set of rows.此外,可能不需要GROUP BY ,从而消除了对中间行集的额外排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM