求:多线程查询数据库的解决方法,最好有DEMO ( 积分: 100 )

  • 主题发起人 主题发起人 niewenlong
  • 开始时间 开始时间
N

niewenlong

Unregistered / Unconfirmed
GUEST, unregistred user!
由于数据量实在是大得惊人
单线程跑 我不知道要跑多久~~~
数据量在8000万-1亿
按月分表(有重复记录)
我想用多线程去查询
将数据统计最终放在另一个表里
有个字段是记录重复的次数 各线程查询数据库的同时 需要对一个指定表进行更新
求核心代码~~~~~~
 
由于数据量实在是大得惊人
单线程跑 我不知道要跑多久~~~
数据量在8000万-1亿
按月分表(有重复记录)
我想用多线程去查询
将数据统计最终放在另一个表里
有个字段是记录重复的次数 各线程查询数据库的同时 需要对一个指定表进行更新
求核心代码~~~~~~
 
难道就没人可以搞定?
但线程跑 恐怕要跑几天了~~~~~~ 哎~~~
 
这个我也想知道呀,
我现在也遇到这样的问题,
我是两个表都是2000多万的进行比较.
速度慢得忍受不了~~~~~~~~~~~~
吃了饭,洗了澡,再喝杯茶就差不多可以查询完了。

关注中~~~~~~~~
 
http://www.delphibbs.com/keylife/iblog_show.asp?xid=13224
 
fly兄的笔记很好对数据库进行相应的优化可以大大的提高效率
多线程查询数据库代码如下
后台查询主窗体
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, ExtCtrls;
type
TMainForm = class(TForm)
pnlBottom: TPanel;
pnlButtons: TPanel;
btnGo: TButton;
btnExit: TButton;
memQuery: TMemo;
pnlTop: TPanel;
Label1: TLabel;
cbAlias: TComboBox;
Label3: TLabel;
edUserName: TEdit;
Label4: TLabel;
edPassword: TEdit;
Label2: TLabel;
procedure btnExitClick(Sender: TObject);
procedure btnGoClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
MainForm: TMainForm;
implementation
{$R *.DFM}
uses QryU, DB, DBTables;
var
FQueryNum: Integer = 0;
procedure TMainForm.btnExitClick(Sender: TObject);
begin
Close;
end;

procedure TMainForm.btnGoClick(Sender: TObject);
begin
Inc(FQueryNum);
// keep querynum unique
{ invoke new query }
NewQuery(FQueryNum, memQuery.Lines, cbAlias.Text, edUserName.Text,
edPassword.Text);
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
{ fill drop-down list with BDE Aliases }
Session.GetAliasNames(cbAlias.Items);
end;

end.

多线程查询子窗体
unit QryU;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Grids, DBGrids, DB, DBTables, StdCtrls;
type
TQueryForm = class(TForm)
Query: TQuery;
DataSource: TDataSource;
QuerySession: TSession;
QueryDatabase: TDatabase;
dbgQueryGrid: TDBGrid;
memSQL: TMemo;
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

procedure NewQuery(QryNum: integer;
Qry: TStrings;
const Alias, UserName,
Password: string);
implementation
{$R *.DFM}
type
TDBQueryThread = class(TThread)
private
FQuery: TQuery;
FDataSource: TDataSource;
FQueryException: Exception;
procedure HookUpUI;
procedure QueryError;
protected
procedure Execute;
override;
public
constructor Create(Q: TQuery;
D: TDataSource);
virtual;
end;

constructor TDBQueryThread.Create(Q: TQuery;
D: TDataSource);
begin
inherited Create(True);
// create suspended thread
FQuery := Q;
// set parameters
FDataSource := D;
FreeOnTerminate := True;
Resume;
// thread that puppy!
end;

procedure TDBQueryThread.Execute;
begin
try
FQuery.Open;
// open the query
Synchronize(HookUpUI);
// update UI from main thread
except
FQueryException := ExceptObject as Exception;
Synchronize(QueryError);
// show exception from main thread
end;
end;

procedure TDBQueryThread.HookUpUI;
begin
FDataSource.DataSet := FQuery;
end;

procedure TDBQueryThread.QueryError;
begin
Application.ShowException(FQueryException);
end;

procedure TQueryForm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Action := caFree;
end;

procedure NewQuery(QryNum: integer;
Qry: TStrings;
const Alias, UserName,
Password: string);
begin
{ Create a new Query form to show query results }
with TQueryForm.Create(Application)do
begin
{ Set a unique session name }
QuerySession.SessionName := Format('Sess%d', [QryNum]);
with QueryDatabasedo
begin
{ set a unique database name }
DatabaseName := Format('DB%d', [QryNum]);
{ set alias parameter }
AliasName := Alias;
{ hook database to session }
SessionName := QuerySession.SessionName;
{ user-defined username and password }
Params.Values['USER NAME'] := UserName;
Params.Values['PASSWORD'] := Password;
end;
with Querydo
begin
{ hook query to database and session }
DatabaseName := QueryDatabase.DatabaseName;
SessionName := QuerySession.SessionName;
{ set up the query strings }
SQL.Assign(Qry);
end;
{ display query strings in SQL Memo }
memSQL.Lines.Assign(Qry);
{ show query form }
Show;
{ open query in its own thread }
TDBQueryThread.Create(Query, DataSource);
end;
end;

end.
 
这个线程查询是很不错的例子,可是小弟有一事不明,当我的程序将TQuery控件改为TADOQuery后发现创建线程处出错:
在Create(Qry:TADOQuery)中:
FADOQuery := Qry;
//就在这里出错
不知道是什么原因?
另外我也加了TADOConnection控件,请老兄指点指点,谢谢!
 
用存储过程,使用多线程效果不明显
 
后退
顶部