SELECT a.* FROM `tblog` as a
INNER JOIN `structroot` as b ON a.`ModID`=b.`ModuleID` AND a.`SrcGuid`=b.`DataGUID`
INNER JOIN (SELECT * FROM `tblog` WHERE `ModID`=357 AND `ID`<2147483647 ORDER BY `ID` desc limit 100) as f ON f.`ModID`=b.`RootModuleID` AND f.`SrcGuid` = b.`RootDataGUID`
WHERE a.`ID`<2147483647
UNION ALL
(SELECT * FROM `tblog` WHERE `ModID`=357 AND `ID`<2147483647 ORDER BY `ID` desc limit 100)
如图,有括号的俩句实际都是一个查询。所以我想优化成一句 这是我想象的语法,但是就是跑不通,所以让大佬看看呢
SELECT a.* FROM `tblog` as a
INNER JOIN `structroot` as b ON a.`ModID`=b.`ModuleID` AND a.`SrcGuid`=b.`DataGUID`
INNER JOIN (SELECT * FROM `tblog` WHERE `ModID`=357 AND `ID`<2147483647 ORDER BY `ID` desc limit 100) as f ON f.`ModID`=b.`RootModuleID` AND f.`SrcGuid` = b.`RootDataGUID`
WHERE a.`ID`<2147483647
UNION ALL
f
1
lianyue 322 天前
手写 sql ?
这不影响性能 优化他干嘛。 |
3
xuecat OP 影响啊,因为有俩句一模一样的查询,我时间就 double 了。所以我想看看能不能只查一次
|
4
codingadog 322 天前
mysql8 以上可以用 with 语句抽出公共部分,低版本就不知道了。
|
5
mightybruce 322 天前
mysql 将这个查询语句创建为一个视图,
这两句改写为在视图上操作。 |
6
dog82 322 天前 1
用 with 子句
|
7
declandragon 322 天前
CREATE TEMPORARY TABLE TempTable AS
(SELECT * FROM `tblog` WHERE `ModID`=357 AND `ID`<2147483647 ORDER BY `ID` desc limit 100); SELECT a.* FROM `tblog` as a INNER JOIN `structroot` as b ON a.`ModID`=b.`ModuleID` AND a.`SrcGuid`=b.`DataGUID` INNER JOIN TempTable as f ON f.`ModID`=b.`RootModuleID` AND f.`SrcGuid` = b.`RootDataGUID` WHERE a.`ID`<2147483647 UNION SELECT * FROM TempTable; |
9
alwaysdazz 322 天前 via Android 1
WITH FilteredTBlog AS (
SELECT * FROM `tblog` WHERE `ModID`=357 AND `ID`<2147483647 ORDER BY `ID` DESC LIMIT 100 ) SELECT a.* FROM `tblog` AS a INNER JOIN `structroot` AS b ON a.`ModID` = b.`ModuleID` AND a.`SrcGuid` = b.`DataGUID` INNER JOIN FilteredTBlog AS f ON f.`ModID` = b.`RootModuleID` AND f.`SrcGuid` = b.`RootDataGUID` WHERE a.`ID` < 2147483647 UNION ALL SELECT * FROM FilteredTBlog |