Qt读取数据库比C#慢吗?我觉得我漏掉了什么!为了比较读取速度,我在Qt中编写了以下内容:
QElapsedTimer t;
t.start();
int count = 0;
QString cs = "Driver={SQL Server}; Server=EMON;Database=FAODB;User=Test;Password=Test";
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC",cs);
db.setDatabaseName(cs);
db.open();
QSqlQuery query(db);
query.setForwardOnly(true);
query.exec("SELECT * FROM FAOCropsLivestock");
while(query.next()){
if(query.value("Country").toString() == "\"Bangladesh\"")
count++;
}
db.close();
qDebug()<< QString::number(count) + " elapsed ms " + QString::number(t.elapsed());
以及在C#中
Stopwatch s = new Stopwatch();
s.Start();
int count = 0;
string cs = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
string query = "SELECT * FROM FAOCropsLivestock";
using(SqlConnection con = new SqlConnection(cs)){
using(SqlCommand cmd = new SqlCommand(query, con)){
con.Open();
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read()){
if(rd["Country"].ToString() == "\"Bangladesh\"")
count++;
}
}
}
s.Stop();
Console.WriteLine(count + " elapsed ms " + s.ElapsedMilliseconds);
无论是C#还是Qt都支持x86架构,并且在发布模式下运行。
在Qt的情况下,结果为"63653 elapsed ms 68213",而在C#中为"63653 elapsed ms 14210"。
对于Qt版本,我已经进行了替换。
QString cs = "Driver={SQL Server}; Server=EMON;Database=FAODB;User=Test;Password=Test";
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC",cs);
db.setDatabaseName(cs);
by
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC" /*QODBC3*/);
db.setDatabaseName("Driver={SQL Server Native Client 11.0};"\
"Server=EMON;Database=FAODB;Uid=Test;Pwd=Test;");
结果在58秒至71秒之间。
54秒
,而C#则为13秒
。 - user6283344