如何解决C++ 在多线程中读取 Spatialite/Sqlite DB,与单线程相比性能下降,我应该使用多线程从 DB 读取吗?
我正在从存储在本地的 sqlite 文件(带空间) 中读取数据,我们称之为 DB.sqlite
。我需要读取DB中的所有数据,并将其存储在我自己定义的C++ struct中(顺便说一下,有些数据需要在存储之前进行处理)。我从数据库中读取的数据包含空间数据,如 wkt
等。
第一次,我只是用单线程读取数据库,我记录了持续时间。
第二次,我想减少持续时间,所以我创建了另一个线程来同时读取数据。另外,我记录了持续时间。
但是,我发现使用多线程读取DB.sqlite
,持续时间比单线程要长。我尝试将一些设置修改为sqlite,但它没有帮助结果。
这是代码。
和 DB 文件编译时选项 sqlite3_threadsafe() = 1
// my struct like this
struct map_data_struct {
vector<lane> lanelist;
vecotr<dt_line> dtlist;
// many vectors like above
bool Read_in_map_data_thread(sqlite3* db);
void read_dt_line(); // Multithread func
};
bool map_data_struct::Read_in_map_data_thread(sqlite3* db) {
int ret;
sqlite3_stmt* stmt = NULL;
sqlite3* handle = db;
const char *sqlSentence = NULL;
// get table 1
sqlSentence = "select XXXXX from XXXX";
sqlite3_prepare_v2(handle,sqlSentence,-1,&stmt,NULL);
int i = 1;
while(sqlite3_step(stmt) == sqlITE_ROW) {
this->lanelist[i].x = sqlite3_column_int64(stmt,0);
this->lanelist[i].y = sqlite3_column_int64(stmt,1);
// and so on......
i++;
}
sqlite3_finalize(stmt);
/*** start thread ***/
// if i use this part,the performance of each get table below will be slower than just one thread reading ??? does multithread reading to DB will effect the performance?
std::thread t1(&map_data_struct::read_dt_line,this);
/*** end ***/
// get table 2
sqlSentence = "select XXXXX from XXXX";
sqlite3_prepare_v2(handle,NULL);
i = 1;
while(sqlite3_step(stmt) == sqlITE_ROW) {
this->XXXlist[i].x = sqlite3_column_int64(stmt,0);
this->XXXlist[i].y = sqlite3_column_int64(stmt,1);
// and so on......
i++;
}
sqlite3_finalize(stmt);
// get table X ...... and so on
sqlSentence = "select XXXXX from XXXX";
sqlite3_prepare_v2(handle,1);
// and so on......
i++;
}
sqlite3_finalize(stmt);
//...... many read
if (t1.joinable()) {
t1.join();
AINFO << "Waiting for thread2 map_dt_line to finish......";
}
return true;
}
void map_data_struct::read_dt_line() {
sqlite3_stmt* stmt = NULL;
sqlite3* handle2 = NULL;
const char *sqlSentence = NULL;
// open another connection to DB
const void* pcache = spatialite_alloc_connection();
sqlite3_open_v2("DB.sqlite",&handle2,sqlITE_OPEN_READONLY|sqlITE_OPEN_NOMUTEX,NULL);
spatialite_init_ex(handle2,pcache,1);
sqlite3_exec(handle2,"PRAGMA synchronous = OFF;",0);
sqlite3_exec(handle2,"PRAGMA temp_store = MEMORY;","PRAGMA cache_size = 8000;",0);
// get table
sqlSentence = "select XXXXX from XXXX";
sqlite3_prepare_v2(handle2,NULL);
int i = 1;
while(sqlite3_step(stmt) == sqlITE_ROW) {
this->dtline[i].x = sqlite3_column_int64(stmt,0);
this->dtlist[i].y = sqlite3_column_int64(stmt,1);
// and so on......
i++;
}
sqlite3_finalize(stmt);
sqlite3_close(handle2);
spatialite_cleanup_ex(pcache);
}
// how i read DB and store data in struct with multi thread
int main() {
// open 1 connection to DB
sqlite3* handle = NULL;
sqlite3_config(sqlITE_CONfig_MULTITHREAD);
const void* pcache = spatialite_alloc_connection();
sqlite3_open_v2("DB.sqlite",&handle,NULL);
spatialite_init_ex(handle,1);
sqlite3_exec(handle,0);
sqlite3_exec(handle,0);
// DB data will be stored in struct map_data
map_data_struct map_data;
map_data.Read_in_map_data_thread(handle);
sqlite3_close(handle);
spatialite_cleanup_ex(pcache);
}
因此,如您所见,thread t1(&map_data_struct::read_dt_line,this)
使用 this
指针将数据存储在 map_data
中。而 Read_in_map_data_thread()
也使用 this
指针来存储数据。因为它们在结构体的不同部分存储数据,所以应该不会有任何互斥问题,对吧?
问题是,当我尝试使用另一个线程读取这样的数据时,Read_in_map_data()
中的原始读取数据过程会比单线程慢。性能下降,这让我很困惑。
有没有人知道这是什么原因?真的需要帮助,谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。