我试图在 mySQL 工作台或 dbForge 甚至 asp.net 网站中运行以下查询,但是我总是遇到“内存不足异常”。
数据库很大,需要返回的记录很多。我已经阅读了一些其他问题,我需要减少查询以使用更少的内存,但这是一个带有 2 个简单选择语句的 UNION JOIN,来自 3 个不同的表。我怎样才能减少这种情况?
SELECT
t.TICKET as 'Ticket',
t.LOGIN as 'Login Id',
u.Name,
t.OPEN_Time as 'Open Time',
(CASE WHEN t.CLOSE_Time>@CONST_EMPTY_DATE THEN t.CLOSE_time ELSE NULL END) as 'Close Time/Liquidation Time',
(CASE WHEN t.CMD=@CMD_OP_BUY then 'Buy' ELSE 'Sell' END) as 'Type/Liquidation Action',
t.SYMBOL as 'Symbol',
t.VOLUME/100.0 as 'Volume',
t.OPEN_PRICE as 'Open Price',
t.SL as 'S/L',
t.TP as 'T/P',
t.CLOSE_PRICE as 'Closed Price',
t.SWAPS as 'Swap',
(t.VOLUME/100.0)*lots.`Lot Size`*t.OPEN_PRICE as 'Open amount of money',
(t.VOLUME/100.0)*lots.`Lot Size`* t.CLOSE_PRICE as 'Closed amount of money',
t.PROFIT as 'Profit',
t.COMMISSION as 'Commission',
(CASE
WHEN u.GROUP like '%USD%' THEN 'USD'
WHEN u.GROUP like '%EUR%' THEN 'EUR'
WHEN u.GROUP like '%JPY%' THEN 'JPY'
WHEN u.GROUP like '%GBP%' THEN 'GBP'
ELSE '???' END) as 'Currency',
t.SERVER_ID as 'server ID'
FROM
trades t
INNER JOIN users u ON t.LOGIN=u.LOGIN
AND t.SERVER_ID = u.SERVER_ID
and t.CMD IN (@CMD_OP_BUY,@CMD_OP_SELL)
#and t.CLOSE_TIME between fromdate and todate
#and u.LOGIN = inlogin
and t.HISTORY = 'N'
inner join lotsize lots on t.SYMBOL = lots.Name
where t.close_time > '2012-09--17 00:00:00' and t.close_time < '2012-09-24 00:00:00'
UNION ALL
Select
Null as 'Ticket',
l.LOGIN as 'Login Id',
u.Name,
Null as 'Open Time',
l.liquidation_timestamp as 'Close Time/Liquidation Time',
l.liquidation_action as 'Type/Liquidation Action',
Null as 'Symbol',
Null as 'Volume',
Null as 'Open Price',
Null as 'S/L',
Null as 'T/P',
Null as 'Closed Price',
Null as 'Swap',
Null as 'Open amount of money',
Null as 'Closed amount of money',
Null as 'Profit',
Null as 'Commission',
(CASE
WHEN u.GROUP like '%USD%' THEN 'USD'
WHEN u.GROUP like '%EUR%' THEN 'EUR'
WHEN u.GROUP like '%JPY%' THEN 'JPY'
WHEN u.GROUP like '%GBP%' THEN 'GBP'
ELSE '???' END) as 'Currency',
l.SERVER_ID as 'MT4 server ID'
From liquidations as l
INNER JOIN users as u ON l.LOGIN=u.LOGIN
AND l.SERVER_ID = u.SERVER_ID;
我想我需要提一下,如果我单独运行任何一个选择查询,那么它会给我正确的结果。只是在使用 UNION 将它们连接在一起时,我遇到了内存不足的错误。
交易表
'mt4_trades', 'CREATE TABLE `mt4_trades` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`TICKET` int(11) DEFAULT NULL,
`LOGIN` int(11) DEFAULT NULL,
`SYMBOL` char(16) DEFAULT NULL,
`DIGITS` int(11) DEFAULT NULL,
`CMD` int(11) DEFAULT NULL,
`VOLUME` int(11) DEFAULT NULL,
`OPEN_TIME` datetime DEFAULT NULL,
`OPEN_PRICE` double DEFAULT NULL,
`SL` double DEFAULT NULL,
`TP` double DEFAULT NULL,
`CLOSE_TIME` datetime DEFAULT NULL,
`EXPIRATION` datetime DEFAULT NULL,
`CONV_RATE1` double DEFAULT NULL,
`CONV_RATE2` double DEFAULT NULL,
`CONV_RATE3` double DEFAULT NULL,
`COMMISSION` decimal(18,2) DEFAULT NULL,
`COMMISSION_AGENT` decimal(18,2) DEFAULT NULL,
`SWAPS` decimal(18,2) DEFAULT NULL,
`CLOSE_PRICE` double DEFAULT NULL,
`PROFIT` decimal(18,2) DEFAULT NULL,
`TAXES` decimal(18,2) DEFAULT NULL,
`COMMENT` char(128) DEFAULT NULL,
`INTERNAL_ID` int(11) DEFAULT NULL,
`MARGIN_RATE` double DEFAULT NULL,
`TIMESTAMP` int(11) DEFAULT NULL,
`MODIFY_TIME` datetime DEFAULT NULL,
`MODIFY_REASON` varchar(256) DEFAULT NULL,
`HISTORY` char(1) DEFAULT NULL,
`BALANCE` decimal(18,2) DEFAULT NULL,
`MT4_SERVER_ID` varchar(30) DEFAULT '''',
PRIMARY KEY (`ID`),
KEY `mt4_trades_ticket_history_server_id_i` (`TICKET`,`HISTORY`,`MT4_SERVER_ID`),
KEY `mt4_trades_login_server_id_history_i` (`LOGIN`,`MT4_SERVER_ID`,`HISTORY`),
KEY `mt4_cmd_i` (`CMD`),
KEY `mt4_open_time_i` (`OPEN_TIME`),
KEY `mt4_close_time_i` (`CLOSE_TIME`)
) ENGINE=InnoDB AUTO_INCREMENT=16427471 DEFAULT CHARSET=utf8'
用户表
'mt4_users', 'CREATE TABLE `mt4_users` (
`MODIFY_TIME` datetime DEFAULT NULL,
`LOGIN` int(11) NOT NULL DEFAULT ''0'',
`USER_GROUP` varchar(30) DEFAULT NULL,
`BALANCE` decimal(18,2) DEFAULT NULL,
`PREVMONTHBALANCE` decimal(18,2) DEFAULT NULL,
`PREVBALANCE` decimal(18,2) DEFAULT NULL,
`PREVMONTHEQUITY` decimal(18,2) DEFAULT NULL,
`PREVEQUITY` decimal(18,2) DEFAULT NULL,
`BALANCE_STATUS` int(11) DEFAULT NULL,
`EQUITY` decimal(18,2) DEFAULT NULL,
`MARGIN` decimal(18,2) DEFAULT NULL,
`MARGIN_LEVEL` decimal(18,2) DEFAULT NULL,
`MARGIN_FREE` decimal(18,2) DEFAULT NULL,
`CREDIT` decimal(18,2) DEFAULT NULL,
`MODIFY_REASON` varchar(128) DEFAULT NULL,
`NAME` varchar(128) DEFAULT NULL,
`EMAIL` varchar(48) DEFAULT NULL,
`COMMENT` varchar(64) DEFAULT NULL,
`MT4_SERVER_ID` varchar(30) NOT NULL DEFAULT '''',
`GROUP` varchar(30) DEFAULT NULL,
`ADDRESS` varchar(128) DEFAULT NULL,
PRIMARY KEY (`LOGIN`,`MT4_SERVER_ID`),
UNIQUE KEY `mt4_users_uk` (`GROUP`,`LOGIN`,`MT4_SERVER_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8'
清算表
'mt4_liquidations', 'CREATE TABLE `mt4_liquidations` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`LOGIN` int(11) DEFAULT NULL,
`LIQUIDATION_ACTION` char(1) DEFAULT NULL,
`LIQUIDATION_TIMESTAMP` datetime DEFAULT NULL,
`EQUITY_BEFORE_LIQUIDATION` decimal(18,2) DEFAULT NULL,
`MARGIN_BEFORE_LIQUIDATION` decimal(18,2) DEFAULT NULL,
`BALANCE_BEFORE_LIQUIDATION` decimal(18,2) DEFAULT NULL,
`MT4_SERVER_ID` varchar(30) NOT NULL DEFAULT '''',
PRIMARY KEY (`ID`,`MT4_SERVER_ID`),
KEY `Login` (`LOGIN`),
KEY `liquidation_timestamp` (`LOGIN`,`LIQUIDATION_TIMESTAMP`)
) ENGINE=InnoDB AUTO_INCREMENT=10526248 DEFAULT CHARSET=utf8'
解释查询的输出
'1', 'PRIMARY', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'Impossible WHERE noticed after reading const tables'
'2', 'UNION', 'l', 'ALL', 'Login,liquidation_timestamp', NULL, NULL, NULL, '10502418', ''
'2', 'UNION', 'u', 'eq_ref', 'PRIMARY', 'PRIMARY', '96', 'l.LOGIN,l.MT4_SERVER_ID', '1', ''
NULL, 'UNION RESULT', '<union1,2>', 'ALL', NULL, NULL, NULL, NULL, NULL, ''