mysql通过id选择使用between

8fsztsew  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(366)

我有一个关于联接二表的问题
twd\ csd\新闻\详情(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\新闻\结果\标题(百万行):

+--------+---------------------+----------------+
| 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\u dri\u news\u result\u 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连接它们,这是可行的,但是当我继续在中添加csd\u id range时,完成这个查询需要很长时间 where 克莱斯

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秒

8zzbczxx

8zzbczxx1#

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

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;

相关问题