我有一个数据库,我需要尽可能快地查询一遍又一遍。我的查询执行得很快,但似乎有一些额外的滞后。从外部应用程序查询MYSQL(是我的代码效率低下)?
我有一种感觉,这种滞后是由于我每次启动和取消启动连接连接的事实。有没有办法避免这种情况?
我没有使用libmysql(至少不是直接)。我在Lazarus/FreePascal(类似于delphi)中使用“mysql50”包,后者又使用libmysql(我认为)。
如果有人看了我的代码并指出(或者甚至是固定的)一些低效率,我将非常感激。
这个库的目的是传递从MQL4发出的查询(一种用于金融交易市场的类似C语言),并从我的MYSQL数据库(通过管道连接到它)返回一行, 。
{$CALLING STDCALL}
library D1Query;
{$mode objfpc}{$H+}
uses
cmem,
Windows,
SysUtils,
profs_win32exceptiontrap,
mysql50;
var
sock: PMYSQL;
qmysql: st_mysql;
type
VArray = array[0..100] of Double;
PArray = ^VArray;
procedure InitSQL; stdcall;
begin
mysql_init(PMySQL(@qmysql));
sock :=
mysql_real_connect(PMysql(@qmysql), '.', 'root', 'password', 'data', 3306, 'mysql', CLIENT_MULTI_STATEMENTS);
if sock = nil then
begin
OutputDebugString(PChar(' Couldn''t connect to MySQL.'));
OutputDebugString(PChar(mysql_error(@qmysql)));
halt(1);
end;
end;
procedure DeInitSQL; stdcall;
begin
mysql_close(sock);
end;
function SQL_Query(QRY: PChar; output: PArray): integer; stdcall;
var
rowbuf: MYSQL_ROW;
recbuf: PMYSQL_RES;
i: integer;
nfields: LongWord;
begin
InitSQL();
if (mysql_query(sock, QRY) < 0) then
begin
OutputDebugString(PChar(' Query failed '));
OutputDebugString(PChar(' ' + mysql_error(sock)));
end;
recbuf := mysql_store_result(sock);
nfields := mysql_num_fields(recbuf);
rowbuf := mysql_fetch_row(recbuf);
if (rowbuf <> nil) then
begin
for i:=0 to nfields-1 do
output^[i] := StrToFloatDef(rowbuf[i], -666);
end;
mysql_free_result(recbuf);
DeInitSQL();
Result := i;
end;
exports
SQL_Query,
InitSQL,
DeInitSQL;
begin
end.
本应该去代码审查 – Ibu 2011-05-25 05:08:41
嗯,我根本不知道存在。 – 2011-05-25 05:29:48