mysql在

时间:2018-05-22 14:30:05

标签: mysql

我有关于加入两个表的问题

TWD_CSD_NEWS_DETAIL(2亿行):

+-----------+------------+--------------+------------------+
| CSD_ID    | CSD_ID_DRI | CSD_PARTY_ID | CSD_PARTY_AMOUNT |
+-----------+------------+--------------+------------------+
| 1         | 1          | 1183         | 27870            |
+-----------+------------+--------------+------------------+
| 2         | 1          | 1723         | 12               |
+-----------+------------+--------------+------------------+
| 3         | 1          | 1243         | 87474            |
+-----------+------------+--------------+------------------+
.
.
.
+-----------+------------+--------------+------------------+
| 18575622  | 8881       | 1183         | 27870            |
+-----------+------------+--------------+------------------+

SHOW CREATE TABLE TWD_CSD_NEWS_DETAIL的结果:

CREATE TABLE `TWD_CSD_NEWS_DETAIL` (
  `CSD_ID` int(11) NOT NULL AUTO_INCREMENT,
  `CSD_ID_CREATED_BY` int(11) DEFAULT NULL,
  `CSD_DT_CREATED` datetime DEFAULT NULL,
  `CSD_DT_UPD` datetime DEFAULT NULL,
  `CSD_ID_DRI` int(11) DEFAULT NULL,
  `CSD_ID_UPD_BY` int(11) DEFAULT NULL,
  `CSD_PARTY_ID` int(11) DEFAULT NULL,
  `CSD_AMOUNT` decimal(26,0) DEFAULT NULL,
  `CSD_TIMESTAMP` datetime DEFAULT NULL,
  PRIMARY KEY (`CSD_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=184035984 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

TWD_DRI_NEWS_RESULT_HEADER(100万行):

+--------+---------------------+----------------+
| DRI_ID | DRI_DATE            | DRI_SYM_SYMBOL |
+--------+---------------------+----------------+
| 1      | 2011-11-08 00:00:00 | 1              |
+--------+---------------------+----------------+
| 2      | 2011-11-08 00:00:00 | 2              |
+--------+---------------------+----------------+
| 3      | 2011-11-08 00:00:00 | 3              |
+--------+---------------------+----------------+
.
.
+--------+---------------------+----------------+
| 10001  | 2011-11-11 00:00:00 | 8881           |
+--------+---------------------+----------------+

SHOW CREATE TABLE TWD_DRI_NEWS_RESULT_HEADER的结果:

CREATE TABLE `TWD_DRI_NEWS_RESULT_HEADER` (
  `DRI_ID` int(11) NOT NULL AUTO_INCREMENT,
  `DRI_DATE` datetime DEFAULT NULL,
  `DRI_SYM_SYMBOL` int(11) DEFAULT NULL,
  `DRI_TIMESTAMP` datetime DEFAULT NULL,
  PRIMARY KEY (`DRI_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1592193 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

我尝试使用以下sql加入它们,它可以工作但是当我继续在where cluase

中添加csd_id范围时,需要很长时间才能完成此查询
SELECT 
    csd.CSD_ID, csd.CSD_ID_DRI, csd.CSD_PARTY_ID, csd.CSD_AMOUNT ,  dri.DRI_DATE, dri.DRI_SYM_TICKER
FROM
    TWD_CSD_NEWS_DETAIL csd
        LEFT JOIN
    TWD_DRI_NEWS_RESULT_HEADER dri ON dri.DRI_ID = csd.CSD_ID_DRI
WHERE
   (  
     ( 
    ( csd_id between 1 and 426029) 
 || ( csd_id between 426030 and 851977) 
 || ( csd_id between 851978 and 1277890) 
   ..
...
...
     ) 
AND dri.DRI_SYM_SYMBOL = 1 

   )

我是否需要创建另一个视图来包含结果或任何更快的方法来查询?我尝试了between 1 and 200000000持续时间范围,获取时间需要0.197秒/ 26秒

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用单一范围进行查询?

 SELECT 
  csd.CSD_ID, csd.CSD_ID_DRI, csd.CSD_PARTY_ID, csd.CSD_SHAREHOLDING ,  
  dri.DRI_SHAREHOLDING_DATE, dri.DRI_SYM_TICKER
 FROM
  TWD_CSD_NEWS_DETAIL csd
 LEFT JOIN
  TWD_DRI_NEWS_RESULT_HEADER dri ON dri.DRI_ID = csd.CSD_ID_DRI
 WHERE
  csd_id between (1 and 1277890) AND dri.DRI_SYM_SYMBOL = 1;