SQLite DB打开时间真的很长

发布于 2024-09-05 13:59:56 字数 989 浏览 4 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许久 2024-09-12 14:00:07

实际上,我不认为这会是缓存问题。我非常确定当您打开 SQLite 时,它​​不会将整个数据库加载到内存中 - 它只是获取一些相对少量的磁盘结构。

然而,一种可能性是它尚未使用 SQLITE_OMIT_AUTOINIT 预处理器定义进行编译。在这种情况下,调用 sqlite3_open16 将导致调用 sqlite3_initialize()

尽管我不确定需要多少时间,但该函数中发生了相当多的事情。 sqlite3_initialize() 函数维护一个标志,指示它之前被调用过,然后在后续调用中,它将(几乎)立即退出。这就是为什么我提到它可能是第一次打开和随后打开之间的差异的罪魁祸首。

我建议将您的代码从: 更改

sqlite3* mpDB;
nRet = sqlite3_open16(szFile, &mpDB);

为:

sqlite3* mpDB;
nRet = sqlite3_initialize();
if (nRet == SQLITE_OK)
    nRet = sqlite3_open16(szFile, &mpDB);

并独立计时两个函数调用。可能是初始化占用了时间。

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 to sqlite3_open16 will result in a call to sqlite3_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:

sqlite3* mpDB;
nRet = sqlite3_open16(szFile, &mpDB);

to:

sqlite3* mpDB;
nRet = sqlite3_initialize();
if (nRet == SQLITE_OK)
    nRet = sqlite3_open16(szFile, &mpDB);

and timing the two function calls independently. It may be that it's the initialisation taking up the time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文