SQLite DB打开时间真的很长
我在c++ windows中使用sqlite,并且我的数据库大小约为60M, 当我打开 sqlite 数据库时,大约需要 13 秒。
sqlite3* mpDB;
nRet = sqlite3_open16(szFile, &mpDB);
如果我关闭我的应用程序并再次重新打开它。仅需不到 1 秒。
首先,我认为这是因为磁盘缓存。所以我在sqlite打开之前预加载了60M的db文件,并使用CFile读取该文件,但是预加载后,第一次仍然很慢。
BOOL CQFilePro::PreLoad(const CString& strPath)
{
boost::shared_array<BYTE> temp = boost::shared_array<BYTE>(new BYTE[PRE_LOAD_BUFFER_LENGTH]);
int nReadLength;
try
{
CFile file;
if (file.Open(strPath, CFile::modeRead) == FALSE)
{
return FALSE;
}
do
{
nReadLength = file.Read(temp.get(), PRE_LOAD_BUFFER_LENGTH);
} while (nReadLength == PRE_LOAD_BUFFER_LENGTH);
file.Close();
}
catch(...)
{
}
return TRUE;
}
我的问题是第一次打开和第二次打开有什么区别。 如何加速 sqlite 打开过程。
I am using sqlite in c++ windows, And I have a db size about 60M,
When I open the sqlite db, It takes about 13 second.
sqlite3* mpDB;
nRet = sqlite3_open16(szFile, &mpDB);
And if I closed my application and reopen it again. It takse only less then 1 second.
First, I thought It is because of disk cache. So I preload the 60M db file before sqlite open, and read the file using CFile, However, after preloading, the first time is still very slow.
BOOL CQFilePro::PreLoad(const CString& strPath)
{
boost::shared_array<BYTE> temp = boost::shared_array<BYTE>(new BYTE[PRE_LOAD_BUFFER_LENGTH]);
int nReadLength;
try
{
CFile file;
if (file.Open(strPath, CFile::modeRead) == FALSE)
{
return FALSE;
}
do
{
nReadLength = file.Read(temp.get(), PRE_LOAD_BUFFER_LENGTH);
} while (nReadLength == PRE_LOAD_BUFFER_LENGTH);
file.Close();
}
catch(...)
{
}
return TRUE;
}
My question is what is the difference between first open and second open.
How can I accelerate the sqlite open-process.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,我不认为这会是缓存问题。我非常确定当您打开 SQLite 时,它不会将整个数据库加载到内存中 - 它只是获取一些相对少量的磁盘结构。
然而,一种可能性是它尚未使用 SQLITE_OMIT_AUTOINIT 预处理器定义进行编译。在这种情况下,调用
sqlite3_open16
将导致调用sqlite3_initialize()
。尽管我不确定需要多少时间,但该函数中发生了相当多的事情。 sqlite3_initialize() 函数维护一个标志,指示它之前被调用过,然后在后续调用中,它将(几乎)立即退出。这就是为什么我提到它可能是第一次打开和随后打开之间的差异的罪魁祸首。
我建议将您的代码从: 更改
为:
并独立计时两个函数调用。可能是初始化占用了时间。
Actually, I don't imagine it would be a caching issue. I'm pretty certain SQLite doesn't load the entire database into memory when you open it - it just gets some relatively small amount of on-disk structures.
One possibility, however, is if it has not been compiled with the
SQLITE_OMIT_AUTOINIT
preprocessor define. In that case, a call tosqlite3_open16
will result in a call tosqlite3_initialize()
.Quite a bit happens within that function though I'm unsure as to how much time it will take. The
sqlite3_initialize()
function maintains a flag indicating it's been called before and then, on subsequent calls, it will exit (almost) immediately. That's why I mention it as a possible culprit in the difference between first and subsequent opens.I'd suggest changing your code from:
to:
and timing the two function calls independently. It may be that it's the initialisation taking up the time.