【发布时间】:2021-06-15 19:04:27
【问题描述】:
我有 sql 查询,必须快速运行
查询正在工作,但速度很慢我怎样才能让它们工作得更快
这是我的第一个查询,对于我尝试做的事情来说非常慢
select
distinct symbol as s
,(select count(distinct login) from orders where symbol = s and cmd < 2)as users
,(select count(cmd) from orders where symbol=s and cmd=1) as sat
,(select count(cmd) from orders where symbol=s and cmd=0) as al
,(select ifnull(sum(volume),0) from orders where symbol=s and cmd=0) as al_lot
,(select ifnull(sum(volume),0) from orders where symbol=s and cmd=1) as sat_lot
,(select count(distinct login) from orders where symbol=s and cmd=0) as a
,(select count(distinct login) from orders dd where symbol=s and (select sum(volume) from orders where symbol=dd.symbol and orders.login=dd.login and cmd = 0) < (select sum(volume) from orders where symbol=dd.symbol and dd.login=orders.login and cmd=1)) as b
from orders where symbol in("EURUSD","XAUUSD","GAUTRY","GBPUSD","USDJPY","USDTRY","BRENT","CRUDE","DAX30","DM#","ES#","BTCUSD") group by s,cmd;
这是我目前正在使用的查询(我试图让它更快我的第一个查询)
Select
src.symbol as Symbol
,IFNULL(count(distinct deals.LOGIN),0) as Users
,IFNULL(count(mybuy.LOGIN),0) as BuyDeals
,IFNULL(count(mysell.LOGIN),0) as SellDeals
,IFNULL(sum(mybuy.volume),0) as BuyVolume
,IFNULL(sum(mysell.volume),0) as SellVolume
,(select count(distinct login) from orders where symbol=deals.symbol and cmd =0) as a
,(select count(distinct login) from orders dd where symbol=src.symbol and (select sum(volume) from orders where symbol=dd.symbol and login=dd.login and cmd = 0) < (select sum(volume) from orders where symbol=dd.symbol and dd.login=login and cmd=1)) as b
from
(Select * from orders Where cmd<2)src
LEFT OUTER JOIN(Select `order`, symbol, login, volume from orders where cmd<2 group by `order`,symbol,login)deals on deals.`order`=src.`order`
LEFT OUTER JOIN(Select `order`, symbol, login, volume from orders where cmd=0 group by `order`,symbol,login)mybuy on mybuy.`order`=src.`order`
LEFT OUTER JOIN(Select `order`, symbol, login, volume from orders where cmd=1 group by `order`,symbol,login)mysell on mysell.`order`=src.`order`
where src.symbol in ('EURUSD','XAUUSD','GAUTRY','GBPUSD','USDJPY','USDTRY','BRENT','CRUDE','DAX30','DM#','ES#','BTCUSD') group by src.symbol order by symbol;
但它仍然太慢,它必须更快地工作
这是我的桌子
CREATE TABLE `orders` (
`order` int(11) NOT NULL,
`login` int(11) DEFAULT NULL,
`symbol` varchar(13) DEFAULT NULL,
`digits` int(11) DEFAULT NULL,
`cmd` int(11) DEFAULT NULL,
`volume` int(11) DEFAULT NULL,
`open__time` varchar(45) DEFAULT NULL,
`state` int(11) DEFAULT NULL,
`open_price` double DEFAULT NULL,
`sl` double DEFAULT NULL,
`tp` double DEFAULT NULL,
`close_time` varchar(45) DEFAULT NULL,
`gw_volume` int(11) DEFAULT NULL,
`expiration` varchar(45) DEFAULT NULL,
`reason` varchar(45) DEFAULT NULL,
`conv_reserv` varchar(3) DEFAULT NULL,
`conv_rate1` double DEFAULT NULL,
`conv_rate2` double DEFAULT NULL,
`commission` double DEFAULT NULL,
`commission_agent` double DEFAULT NULL,
`storage` double DEFAULT NULL,
`close_price` double DEFAULT NULL,
`profit` double DEFAULT NULL,
`taxes` double DEFAULT NULL,
`magic` int(11) DEFAULT NULL,
`comment` varchar(45) DEFAULT NULL,
`gw_order` int(11) DEFAULT NULL,
`activation` int(11) DEFAULT NULL,
`gw_open_price` int(11) DEFAULT NULL,
`gw_close_price` int(11) DEFAULT NULL,
`margin_rate` double DEFAULT NULL,
`timestamp` varchar(45) DEFAULT NULL,
PRIMARY KEY (`order`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
【问题讨论】:
-
您必须删除所有子查询。
-
但我需要他们给我的价值观@Akina
-
您必须将它们转换为 JOIN 形式。
-
第二个我删除了其中一些,但我不知道如何在我的第二个查询子查询@Akina 中删除和获取变量
-
我尝试使用 LEFT OUTER JOIN 获取数据,所以我不需要使用子查询 @Akina
标签: mysql sql database performance time