sqlite 快速備份

Despite having “l(fā)ite” in its name, SQLite can handle very large databases quite efficiently. The single largest I am currently dealing with just passed 100 GB, and occasionally when backing it up with the SQLite standard Command Line utility it would take many hours.
After some asking on the sqlite-users list (thanks Clemens Madish & Simon Slavin), and some experimenting, I have gotten the backup time down to 4-5 minutes on a live 100 GB database, which is just fine in my book. Below are the findings and an utility, sql3bak, which wraps the results of the findings.

Backup Steps
The primary reason of the very long backup time is that when using the sqlite3_backup_step API function, if you pass a fixed number of pages, and the database is being updated before the backup completes, the backup will… restart from the beginning. And the command line utility backups in steps of 100 pages.
This behavior was useful for the “l(fā)egacy” journal modes like DELETE or TRUNCATE, where read transactions block write transactions and vice-versa. A smallish number of steps would prevent the backup from locking the databases for an extended period of time.
But if you use the Write-Ahead Logging mode, it is much less useful. For WAL datsbase, you can backup an arbitrarily sized database in just one step, without preventing other write (or read) transactions to happen, and without risk of the backup restarting.
Synchronous Mode
Another cause of performance loss is that the destination database in the backup is using the synchronous=NORMALmode by default. This mode is a Good Thing, but in the particular case of backup, it is only useful if you are replacing a backup and do not want the previous backup to be lost in case of failure during the backup.
If on the other hand you are backing up to a new file each time, it is possible to use synchronous=OFF for the backup, and protect the backup from being accessed while the backup is taking place by using DELETE or TRUNCATE journal mode instead.
Cache Size
The last speedup tweak comes from the cache_size option.
For the backup destination, using a cache_size of 1 provides the best performance: SQLite offloads pages to the disk directly and does not waste time maintaining a cache for pages that will never be accessed again.
For the source database, the best cache_size is a little more subtle: if the source WAL file is empty, using a cache_size of 1 also appears optimal, as every page will be read only once, so any greater cache would just be wasted.
However when the source database WAL file is large, a higher backup performance was achieved with a “l(fā)arge enough” cache. The exact value seems to depend on what is in the WAL exactly, but having a cache too small had a more negative impact than having a cache too large.
sql3bak
I wrapped up all the above logic in a small utility, creatively named “sql3bak”.
Note that it is really intended for backing up largish SQLite database (at least dozens of megabytes, up to hundreds of of gigabytes). It also assumes that for large databases with large WAL files, it is safe to allocate up to 1 GB of RAM for caches.
The source code can be found on bitbucket sql3bak repository, and a pre-compiled (signed) binary is available in thedownloads.
(note: to compile, you may have to adapt the SQLite3 calls to your favorite bindings, as I am using a custom binding based on a bundled dll, similar to what I used for dwsMPIR.Bundle.pas)

https://www.delphitools.info/2016/10/06/faster-backups-for-large-sqlite-databases/
https://bitbucket.org/egrange/sql3bak/downloads

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市姻檀,隨后出現(xiàn)的幾起案子导街,更是在濱河造成了極大的恐慌,老刑警劉巖踪蹬,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異臣咖,居然都是意外死亡跃捣,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進(jìn)店門夺蛇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來疚漆,“玉大人,你說我怎么就攤上這事刁赦∪⑵福” “怎么了?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵甚脉,是天一觀的道長丸升。 經(jīng)常有香客問我,道長牺氨,這世上最難降的妖魔是什么狡耻? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮猴凹,結(jié)果婚禮上夷狰,老公的妹妹穿的比我還像新娘。我一直安慰自己郊霎,他們只是感情好孵淘,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著歹篓,像睡著了一般瘫证。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上庄撮,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天背捌,我揣著相機(jī)與錄音,去河邊找鬼洞斯。 笑死毡庆,一個(gè)胖子當(dāng)著我的面吹牛坑赡,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播么抗,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼毅否,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蝇刀?” 一聲冷哼從身側(cè)響起螟加,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎吞琐,沒想到半個(gè)月后捆探,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡站粟,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年黍图,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片奴烙。...
    茶點(diǎn)故事閱讀 39,795評論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡助被,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出切诀,到底是詐尸還是另有隱情揩环,我是刑警寧澤,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布趾牧,位于F島的核電站检盼,受9級特大地震影響肯污,放射性物質(zhì)發(fā)生泄漏翘单。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一蹦渣、第九天 我趴在偏房一處隱蔽的房頂上張望哄芜。 院中可真熱鬧,春花似錦柬唯、人聲如沸认臊。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽失晴。三九已至,卻和暖如春拘央,著一層夾襖步出監(jiān)牢的瞬間涂屁,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工灰伟, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留拆又,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像帖族,于是被迫代替她去往敵國和親栈源。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,495評論 0 23
  • 俗話說:“男怕入錯(cuò)行竖般,女怕嫁錯(cuò)郎”甚垦。但是對于當(dāng)今時(shí)代來說,不論男女都怕入錯(cuò)行捻激,選錯(cuò)另一半制轰。 踏入社會短短幾個(gè)月,看...
    智書閱讀 252評論 0 3
  • 義解九型之五十五 女人是水胞谭,水最智慧垃杖。女人有時(shí)把自己的男人當(dāng)兒子培養(yǎng),有時(shí)把自己的男人當(dāng)父親依靠丈屹。就像水一樣變幻形...
    王慶義閱讀 250評論 0 0
  • 生活再忙调俘,再累,身體是生命的本錢旺垒,大家一定要注意身體彩库。愛家人要從愛自己開始,好好愛自己才能夠愛別人先蒋,有健康的身體才...