How to read data faster in Qt?

How does Qt read the database more slowly than C #? It seems I'm missing something! To compare read speed, I wrote the following in 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()); 

and in 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); 

Both C # and Qt are for x86 and Release mode.

In the case of Qt, the result was "63653 elapsed ms 68213" and in C # "63653 elapsed ms 14210"


For the Qt version, I replaced

 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;"); 

The results were between 58 s - 71 s.

+5
source share
1 answer

For your C # version, you are not showing your connection string. Do you use the same drivers for both? I am not an expert in this, but my colleague who says that the "SQL Server" driver is very, very old and slow. You probably want to try the "native SQL client" or something like that. In any case, make sure that the test uses the same SQL driver.

0
source

Source: https://habr.com/ru/post/1261415/


All Articles