我应该打开一个或多个到本地 SQLite 数据库的连接吗? [英] Should I open a single or multiple connections to a local SQLite database?

查看:38
本文介绍了我应该打开一个或多个到本地 SQLite 数据库的连接吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写 Xamarin.Forms 应用程序.我有一个数据库管理器类,它处理来自本地 SQLite 数据库的插入、删除等操作.由于 DB 文件本地存储在单个设备上,因此在任何给定时间只有一个用户访问 DB.我不经常读/写数据库,但是当我这样做时,我会抓取很多数据.

I am in the process of writing a Xamarin.Forms app. I have a database manager class which handles inserting, deleting, etc. from a local SQLite database. Since the DB file is locally stored on a single device, only one user will ever be accessing the DB at any given time. I'm not reading/writing to the DB often, but when I do, I'm grabbing a lot of data.

我的问题是:我应该创建一个到数据库的连接并在整个应用程序中一遍又一遍地重复使用它,还是每次访问数据库时都创建一个新连接更好?每次创建一个新连接可能会更灵活(无论如何对于我的应用程序),但我担心开销.

My question is this: Should I create a single connection to the DB and simply reuse that over and over throughout the entire application, or is it better to create a new connection every time I access the DB? Creating a new connection each time would probably be more flexible (for my app anyway), but I'm worried about the overhead.

推荐答案

如果您要连续使用大量查询从数据库中提取数据,那么我将重用 SQLite 连接.每个连接请求都会产生开销,每次创建一个新请求并没有真正获得任何好处,只会增加工作量.

If you are going to use a lot of queries straight after each other to pull data from the DB, then I would reuse the SQLite connection. There is overhead for each connection request and nothing is really gained by creating a new one each time, other than just more work.

我不小心让我的应用程序在每个查询上创建新连接.当我改回重用单个连接时,我注意到速度有所提高.

I accidentally had my application creating new connections on each query. I noticed an increase in speed when I corrected it back to reusing a single connection.

这篇关于我应该打开一个或多个到本地 SQLite 数据库的连接吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆