慢查询......需要更快

时间:2016-03-26 15:54:29

标签: mysql sql

为了解释下面的查询,您应该知道我在查询中使用了当前用户的三个变量...

$radius用户希望在

中搜索的偏好设置

$lat用户的纬度

$lon用户的经度

zipData表中的相关列是zipcode lonlat

我有这个查询,可以报告所需半径范围内的所有其他用户......

$query="
    SELECT username FROM zipData,seekers
    WHERE (POW((69.1*(lon-\"$lon\")*cos($lat/57.3)),\"2\")+POW((69.1*(lat-\"$lat\")),\"2\"))<($radius*$radius)
    AND replace(seekers.postal,' ','') = zipData.zipcode;
";

我也有此查询,只返回符合特定条件的特定用户...

$query="
    SELECT * 
    FROM
    (
        SELECT a.username, MATCH(a.highlight) AGAINST (\"{$keywords}\" IN BOOLEAN MODE) AS score 
        FROM resume_highlights a
        JOIN seekers ON a.username = seekers.username and seekers.resume_status = 1
        HAVING score>0 

            UNION ALL

        SELECT b.username, MATCH(b.skill,b.skill_list) AGAINST (\"{$keywords}\" IN BOOLEAN MODE) AS score 
        FROM resume_skills b
        JOIN seekers ON b.username = seekers.username and seekers.resume_status = 1
        HAVING score>0 

            UNION ALL

        SELECT c.username, MATCH(c.education_title,c.education_organization) AGAINST (\"{$keywords}\" IN BOOLEAN MODE) AS score 
        FROM resume_education c
        JOIN seekers ON c.username = seekers.username and seekers.resume_status = 1
        HAVING score>0 

            UNION ALL

        SELECT d.username, MATCH(d.employer_title,d.employer_organization) AGAINST (\"{$keywords}\" IN BOOLEAN MODE) AS score 
        FROM resume_employer d
        JOIN seekers ON d.username = seekers.username and seekers.resume_status = 1
        HAVING score>0 

            UNION ALL

        SELECT e.username, MATCH(e.volunteer_title,e.volunteer_organization) AGAINST (\"{$keywords}\" IN BOOLEAN MODE) AS score 
        FROM resume_volunteer e
        JOIN seekers ON e.username = seekers.username and seekers.resume_status = 1
        HAVING score>0 

    ) AS X
    ORDER BY score desc
";

两个查询都独立工作;但是,我将它们组合成一个查询所做的每一次尝试都导致执行时间非常慢。

更新

我在seekers.postal zipData.lonzipData.lat上有索引。我会认为这样做会有所作为,但事实并非如此。可能只是我构建了错误的查询,所以我想看看如何组合2个查询。

1 个答案:

答案 0 :(得分:1)

我认为link有你的答案:

  

提高SELECT操作性能的最佳方法是   在一个或多个在中测试的列上创建索引   查询。索引条目就像指向表行的指针一样,允许   查询以快速确定哪些行匹配条件   WHERE子句,并检索这些行的其他列值。所有   MySQL数据类型可以编入索引。