3

我在某处看到了从具有多个条件的多个表中进行选择的这种格式,并且我已经实施了很多年。我想知道是否有任何方法可以让它更快。查询的背景:我正在从消息表 b/w 学生和导师中为学生在工作表中发布的工作选择消息。Tutor的信息存储在tutorinfo表中,studentinfo存储在studentinfo表中。

也请告诉这是什么样的加入。有人告诉我这是一个交叉连接,但我不确定。直到最近我才知道加入,但从来没有必要。这种格式已经为我服务了很长一段时间了。我不确定使用连接格式会如何使它变得更好,如果有更好的话。

这是当前查询::

SELECT 
messages.* ,  
tutorinfo.id as tutor_id, 
jobs.student_id as student_id, jobs.job_title
from messages, tutorinfo, studentinfo, jobs where
messages.job_id= jobs.id and jobs.student_id = '3' and messages.thread_id = '2' and        messages.tutor_id = tutorinfo.id and messages.job_id = jobs.id and studentinfo.id=jobs.student_id 
4

3 回答 3

1

使用 MySQL JOINS

在此处输入图像描述

    SELECT
     jobs.`id` AS jobs_id,
     jobs.`job_title` AS jobs_job_title,
     messages.`id` AS messages_id,
     messages.`job_id` AS messages_job_id,
     messages.`thread_id` AS messages_thread_id,
     messages.`tutor_id` AS messages_tutor_id,
     messages.`student_id` AS messages_student_id,
     tutorinfo.`id` AS tutorinfo_id
FROM
     `messages` messages INNER JOIN `tutorinfo` tutorinfo ON messages.`tutor_id` = tutorinfo.`id`
     INNER JOIN `jobs` jobs ON messages.`job_id` = jobs.`id`  WHERE jobs.student_id = '3' AND messages.thread_id = '2'

也看看这些东西

SQL 连接的可视化解释

SQL 连接的可视化表示

于 2013-06-30T05:23:33.713 回答
1

Here is your select statement written in a more modern style. In this style you can see the joins (note they are all inner joins). You can tell they are inner joins (in both our queries) because the columns joined on are joined by an equal. In your method to have a cross join or an outer join you need special formatting (instead of just a =), with the "join" format you use special syntax.

SELECT m.*, 
       t.id as tutor_id, 
       j.student_id as student_id, j.job_title
from messages m
join jobs j on m.job_id = j.job_id
join studentinfo s on j.student_id = s.id
join tutorinfo t on m.tutor_id = t.id
where j.student_id = '3' and m.thread_id = '2' 

One thing is clear -- studentinfo is not used you can take that out. Other than that and adding indexes to all the columns which are used as joins I don't see much in the way of optimization.

SELECT m.*, 
       t.id as tutor_id, 
       j.student_id as student_id, j.job_title
from messages m
join jobs j on m.job_id = j.job_id
join tutorinfo t on m.tutor_id = t.id
where j.student_id = '3' and m.thread_id = '2' 
于 2013-06-30T05:16:02.463 回答
0

这将是补充@Hogan 答案的评论,但我将单独发布。在 MysqlJOIN中,INNER JOIN并且CROSS JOIN都是语法等价物。,并且INNER JOIN在没有连接条件的情况下在语义上是等效的,但是,优先级较低。

在原始查询中,连接条件已放在 where 子句中。INNER JOINSMysql 查询优化器可能会为当前查询提供优化的查询计划,并通过以优化的顺序使用来避免交叉连接和过滤产品。但是,通过稍微改写一下,您可以明确连接条件、强制读取顺序并减少查询优化器需要做的工作。也许这对性能的好处可以忽略不计。你必须配置文件才能看到。

SELECT m.*, 
       t.id as tutor_id, 
       j.student_id as student_id, j.job_title
from messages m
STRAIGHT_JOIN jobs j on m.job_id = j.job_id and j.student_id = '3'
STRAIGHT_JOIN tutorinfo t on m.tutor_id = t.id
where m.thread_id = '2'

STRAIGHT_JOIN类似于JOIN但左表在右表之前读取。从 Mysql文档

连接优化器计算应该连接表的顺序。LEFT JOIN 或 STRAIGHT_JOIN 强制的表读取顺序有助于连接优化器更快地完成工作,因为要检查的表排列更少

于 2013-06-30T06:35:04.427 回答