better-sqlite3

Node.js中最快、最简单的SQLite3库。「The fastest and simplest library for SQLite3 in Node.js.」

Github stars Tracking Chart

better-sqlite3

Node.js中最快,最简单的SQLite3库。

  • 完整的事务支持
  • 高性能、高效、安全
  • 易于使用的同步API(比异步API更快...是的,您没有看错)
  • 支持用户定义的函数,集合和扩展
  • 64位整数(在需要它们之前是不可见的)

帮助这个项目保持强大!

数以千计的开发人员和工程师每天都在使用better-sqlite3。漫长的夜晚和周末让这个项目变得强大和可靠,直到现在都没有要求任何补偿或资金。如果你的公司使用better-sqlite3,请让你的经理考虑支持这个项目:

其他库如何比较

select 1 row get() select 100 rows all() select 100 rows iterate() 1-by-1 insert 1 row run() insert 100 rows in a transaction
better-sqlite3 1x 1x 1x 1x 1x
sqlite and sqlite3 11.7x slower 2.9x slower 24.4x slower 2.8x slower 15.6x slower

您可以通过自己运行基准测试来验证这些结果。

安装

npm install --save better-sqlite3

如果安装有问题,请参阅故障排除指南

用法

const db = require('better-sqlite3')('foobar.db', options);
const row = db.prepare('SELECT * FROM users WHERE id=?').get(userId);
console.log(row.firstName, row.lastName, row.email);

为什么要使用它代替node-sqlite3?

  • node-sqlite3 将异步API用于CPU绑定或序列化的任务。那不仅是糟糕的设计,而且浪费了大量的资源。它还会导致互斥锁抖动,这会对性能产生毁灭性的影响。
  • node-sqlite3 公开了低级(C语言)内存管理功能。 better-sqlite3以JavaScript方式实现,从而使垃圾回收器不必担心内存管理。
  • better-sqlite3 更易于使用,它为某些在node-sqlite3中非常困难或不可能完成的操作提供了很好的实用工具。
  • 在大多数情况下,better-sqlite3比node-sqlite3快得多,而在所有其他情况下也一样快。

该库什么时候不合适使用?

在大多数情况下,如果您尝试使用Better-sqlite3无法合理完成的事情,那么一般来说,使用SQLite3也无法合理地完成这些任务。例如,如果您正在执行需要一秒钟才能完成的查询,并且您希望有许多并发用户在执行这些查询,那么再多的异步性也无法将您从SQLite3的序列化特性中摆脱出来。幸运的是,SQLite3非常快。通过适当的索引,我们就能在60 GB的数据库中通过5路联接实现每秒2000条查询,其中每个查询处理5-50 KB的真实数据。

如果您遇到性能问题,则最有可能的原因是低效的查询,不恰当的索引,或者缺少WAL模式(而不是better-sqlite3本身)。但是,在某些情况下,better-sqlite3可能不合适:

  • 如果您期望大量并发读取,每个读取都会返回许多兆字节的数据(如视频)
  • 如果您期望大量并发写入(如社交媒体网站)
  • 如果数据库的大小接近TB级

对于这些情况,您可能应该使用成熟的RDBMS,例如PostgreSQL。

文档

许可证

MIT

Main metrics

Overview
Name With OwnerWiseLibs/better-sqlite3
Primary LanguageC++
Program languagePython (Language Count: 3)
PlatformLinux, Mac, Windows
License:MIT License
所有者活动
Created At2016-09-07 16:48:37
Pushed At2025-04-19 01:56:46
Last Commit At2025-04-19 03:56:45
Release Count147
Last Release Namev11.9.1 (Posted on 2025-03-17 23:48:25)
First Release Namev0.8.1 (Posted on 2016-09-09 17:30:54)
用户参与
Stargazers Count6k
Watchers Count53
Fork Count416
Commits Count1.5k
Has Issues Enabled
Issues Count1105
Issue Open Count84
Pull Requests Count159
Pull Requests Open Count15
Pull Requests Close Count54
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

better-sqlite3 Build Status Build status

The fastest and simplest library for SQLite3 in Node.js.

  • Full transaction support
  • High performance, efficiency, and safety
  • Easy-to-use synchronous API (faster than an asynchronous API... yes, you read that correctly)
  • Support for user-defined functions, aggregates, and extensions
  • 64-bit integers (invisible until you need them)

Help this project stay strong! 💪

better-sqlite3 is used by thousands of developers and engineers on a daily basis. Long nights and weekends were spent keeping this project strong and dependable, with no ask for compensation or funding, until now. If your company uses better-sqlite3, ask your manager to consider supporting the project:

How other libraries compare

select 1 row  get()  select 100 rows   all()   select 100 rows iterate() 1-by-1 insert 1 row run() insert 100 rows in a transaction
better-sqlite3 1x 1x 1x 1x 1x
sqlite and sqlite3 11.7x slower 2.9x slower 24.4x slower 2.8x slower 15.6x slower

You can verify these results by running the benchmark yourself.

Installation

npm install --save better-sqlite3

If you have trouble installing, check the troubleshooting guide.

Usage

const db = require('better-sqlite3')('foobar.db', options);

const row = db.prepare('SELECT * FROM users WHERE id=?').get(userId);
console.log(row.firstName, row.lastName, row.email);

Why should I use this instead of node-sqlite3?

  • node-sqlite3 uses asynchronous APIs for tasks that are either CPU-bound or serialized. That's not only bad design, but it wastes tons of resources. It also causes mutex thrashing which has devastating effects on performance.
  • node-sqlite3 exposes low-level (C language) memory management functions. better-sqlite3 does it the JavaScript way, allowing the garbage collector to worry about memory management.
  • better-sqlite3 is simpler to use, and it provides nice utilities for some operations that are very difficult or impossible in node-sqlite3.
  • better-sqlite3 is much faster than node-sqlite3 in most cases, and just as fast in all other cases.

When is this library not appropriate?

In most cases, if you're attempting something that cannot be reasonably accomplished with better-sqlite3, it probably cannot be reasonably accomplished with SQLite3 in general. For example, if you're executing queries that take one second to complete, and you expect to have many concurrent users executing those queries, no amount of asynchronicity will save you from SQLite3's serialized nature. Fortunately, SQLite3 is very very fast. With proper indexing, we've been able to achieve upward of 2000 queries per second with 5-way-joins in a 60 GB database, where each query was handling 5–50 kilobytes of real data.

If you have a performance problem, the most likely causes are inefficient queries, improper indexing, or a lack of WAL mode—not better-sqlite3 itself. However, there are some cases where better-sqlite3 could be inappropriate:

  • If you expect a high volume of concurrent reads each returning many megabytes of data (i.e., videos)
  • If you expect a high volume of concurrent writes (i.e., a social media site)
  • If your database's size is near the terabyte range

For these situations, you should probably use a full-fledged RDBMS such as PostgreSQL.

Documentation

License

MIT