mysql - 如何在 SQL 中使用依赖子查询使查询执行得更好?

标签 mysql sql

我正在尝试从两个大型数据表运行查询。我正在尝试加入他们,但同时过滤掉了最短日期,过滤日期似乎减慢了很多。但这是必须的,有什么办法可以加快速度吗?就查询而言,它只是不断加载和加载

这是我在 EXPLAIN 中得到的内容

enter image description here

查询是——

SELECT T1.id_no, 
       T1.condition_code, 
       Count(T1.condition_code) AS COUNT, 
       T1.doe, 
       T2.id_no, 
       T2.trans_time, 
       T2.from_routing_pos 
FROM   attrcoll_month T1 
       JOIN live_trans T2 
         ON T1.id_no = T2.id_no 
WHERE  T2.trans_time = (SELECT Min(trans_time) 
                        FROM   live_trans T2_MIN 
                        WHERE  T2_MIN.id_no = T2.id_no) 
       AND T1.doe BETWEEN '2014-09-01 00:00:01' AND '2014-09-02 23:59:59' 
       AND T1.unique_code = 'XXY' 
GROUP  BY T2.from_routing_pos, 
          T1.condition_code 

每个表数据的片段 -

ATTRCOLL_MONTH T1

ID_NO   DOE                CONDITION_CODE   UNQIUE_CODE
8442    25/09/2014 22:49    NEND             XXY
8442    25/09/2014 22:49    SEND             XXY
8442    25/09/2014 22:49    BS               XXY
8442    25/09/2014 22:49    BS               XXY
8442    25/09/2014 22:49    BS               XXY
8442    25/09/2014 22:49    TD               XXY
8511    25/09/2014 22:49    NEND             XXY
8511    25/09/2014 22:49    SEND             XXY
8511    25/09/2014 22:49    BS               XXY
8511    25/09/2014 22:49    BS               XXY
8511    25/09/2014 22:49    BS               XXY
8511    25/09/2014 22:49    TD               XXY
8511    24/09/2014 12:49    OF               XXY
8511    24/09/2014 12:49    OF               XXY
8675    24/09/2014 12:49    NEND             XXY
8675    24/09/2014 12:49    SEND             XXY
9081    24/09/2014 12:49    NEND             XXY

LIVE_TRANS T2

ID_NO   TRANS_TIME  UNQIUE_CODE FROM_ROUTING_POS
8442    2.12276E+17 XXY          OD1
8442    2.12276E+17 XXY          OD2
8445    2.12276E+17 XXY          OD3
8214    2.12276E+17 XXY          OD2
8325    2.12276E+17 XXY          OD1
842     2.12276E+17 XXY          OD3
2444    2.12276E+17 XXY          OD3

抱歉表格数据格式问题!

希望这解释得很好,如果您需要更多信息,请告诉我

最佳答案

  1. 首先仅从 t1 获取记录到临时表中。
  2. 然后应用临时表与 T2 和 t2_min 的连接并获取所有最小时间和 ID
  3. 然后将#1、#2 和 t2 合并到 join 中并应用 group by。 这将提高性能。

基本思想是限制将成为 Join 一部分的记录并删除子查询。

这是示例:-

--Fetch records from Table one based on all filtering conditions
        -- this will reduce the logical read when we apply join
        SELECT
            T1.id_no,
            T1.condition_code,
            T1.doe
        INTO
            #Temp
        FROM
            attrcoll_month T1
        WHERE
            T1.doe >= '01/09/2014'
            AND T1.doe < '03/01/2014'
            AND T1.unique_code = 'XXY';

        -- Get all the min time for only required ids. This will avoid the sub query and also read get reduced since records in #temp are
    limited
        SELECT
            MIN(trans_time) MinTime,
            T.id_no
        INTO
            #tempMinTime
        FROM
            #Temp T
            JOIN live_trans T2_MIN ON T.id_no = T2_MIN.id_no;
        --Merging #1 and #2
        SELECT
            T1.id_no,
            T1.condition_code,
            COUNT(T1.condition_code) AS count,
            T1.doe,
            T2.id_no,
            T2.trans_time,
            T2.from_routing_pos
        FROM
            #Temp T1
            JOIN #tempMinTime T ON T1.id_no = T.id_no
            JOIN live_trans T2 ON T.id_no = T2.id_no
        WHERE
            T2.trans_time = T.MinTime
        GROUP BY
            T2.from_routing_pos,
            T1.condition_code;

关于mysql - 如何在 SQL 中使用依赖子查询使查询执行得更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26379881/

相关文章:

mysql - 返回第一个表中的所有数据,如果存在,则仅返回第二个表中的 1 个数据

php - 获取3个表的最后一条记录

java - 何时使用 JDBC 对连接、语句和结果集调用 getWarnings()?

sql - postgres中的递归路径查找

sql - ISNULL 等效于空字段

php - 警告:mysqli_error()期望恰好有1个参数,在register.php文件中给出的0个参数

mysql - 是否可以将这些复杂的 MYSQL 查询组合起来?

mysql - fatal error : require_once() [function. 需要] : Failed opening required global. php

当用户和设备存储在同一个表中并且关联存储在另一个表中时,SQL/SQLite 检索属于用户的设备列表

sql - 如何在 SQL Server 2008 上安装全文搜索