mysql - 查询速度慢...需要更快

标签 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 上有索引。我本以为这样就能解决问题,但事实并非如此。可能只是我构建的查询不好,所以我想看看如何组合这两个查询。

最佳答案

我认为这个link有你的答案:

The best way to improve the performance of SELECT operations is to create indexes on one or more of the columns that are tested in the query. The index entries act like pointers to the table rows, allowing the query to quickly determine which rows match a condition in the WHERE clause, and retrieve the other column values for those rows. All MySQL data types can be indexed.

关于mysql - 查询速度慢...需要更快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36237379/

相关文章:

c# - LINQ to Entities 尝试投影两个表并将它们聚合

php - 意外异常 : SQLSTATE[HY000] [1045] Access denied for user **** @'localhost' (using password: YES)

mysql - 在 RDS 的 mysql 数据库上创建用户

sql - 在SQL中将INT转换为FLOAT

c# - 如何处理 SQL DB 中的乐观并发冲突?

sql server 将选择的结果存储在变量中?

php - 在sql中使用连接 - 没有得到所有想要的结果

Java Play 2.2.2 BoneCp 0.8.0.RELEASE MySql 超时连接

mysql - 安装 MySQL JDBC 驱动程序 msi

php - 如何将错误编码的数据转换为 UTF-8?