几何尺寸与公差论坛

 找回密码
 注册
查看: 688|回复: 4

sqlSugar.Insertable(_Data).ExecuteCommand(), this is running slowly.

  [复制链接]
发表于 2023-2-11 09:52:50 | 显示全部楼层 |阅读模式
sqlSugar.Insertable(_Data).ExecuteCommand(), this is running slowly, how to improve the speed using c# or lock the data to avoid SQLite error (5): database is locked?
If the sqlSugar.Insertable(_Data).ExecuteCommand() method is running slowly, there are several things you can try to improve its performance:

    Use batch inserts: If you need to insert a large number of records, you can use batch inserts to improve performance. SqlSugar provides a Insertable<T>().AddRange(List<T>) method that allows you to insert a list of records in a single transaction.

For example:
List<Data> dataList = GetDataList();
sqlSugar.Insertable<Data>(dataList).ExecuteCommand();
This will insert all records in dataList in a single transaction, which can improve performance.

    Use ExecuteCommandAsync() method: If you're running the ExecuteCommand() method on the UI thread, it can slow down the application's responsiveness. To avoid this, you can use the ExecuteCommandAsync() method instead, which runs the command on a background thread.

For example:
await sqlSugar.Insertable(_Data).ExecuteCommandAsync();
 楼主| 发表于 2023-2-11 09:53:33 | 显示全部楼层
This will run the insert command on a background thread, which can improve performance and responsiveness.

    Use a lock to avoid database locks: If you're getting SQLite error (5): database is locked, it's possible that multiple threads or processes are trying to access the database at the same time. To avoid this, you can use a lock to ensure that only one thread or process can access the database at a time.

For example:
// Define a static object for locking
private static object _lockObject = new object();

// Lock the code that accesses the database
lock (_lockObject)
{
    sqlSugar.Insertable(_Data).ExecuteCommand();
}
This will ensure that only one thread can access the database at a time, which can prevent database locks and improve performance.

I hope these suggestions help you improve the performance of your sqlSugar.Insertable(_Data).ExecuteCommand() method!
 楼主| 发表于 2023-2-11 09:55:30 | 显示全部楼层
This will run the insert command on a background thread, which can improve performance and responsiveness.

    Use a lock to avoid database locks: If you're getting SQLite error (5): database is locked, it's possible that multiple threads or processes are trying to access the database at the same time. To avoid this, you can use a lock to ensure that only one thread or process can access the database at a time.

For example:
// Define a static object for locking
private static object _lockObject = new object();

// Lock the code that accesses the database
lock (_lockObject)
{
    sqlSugar.Insertable(_Data).ExecuteCommand();
}
This will ensure that only one thread can access the database at a time, which can prevent database locks and improve performance.

I hope these suggestions help you improve the performance of your sqlSugar.Insertable(_Data).ExecuteCommand() method!
 楼主| 发表于 2023-2-11 10:42:45 | 显示全部楼层
//Task.Run(() => InsertAsync(_Data));
        public static void InsertAsync(OperateData _Data)
        {
            // Lock the code that accesses the database
            lock (sqlSugar)
            {
                sqlSugar.Insertable(_Data).ExecuteCommandAsync();
            }
        }
 楼主| 发表于 2023-2-11 11:08:20 | 显示全部楼层
  // Specify the number of threads to use
            int numThreads = 4;

            // Create the database if it doesn't exist
            if (!sqlSugar.DbMaintenance.IsAnyTable("OperateData", true))
            {
                // Use multiple threads to create the database
                Parallel.For(0, numThreads, i =>
                {
                    sqlSugar.DbMaintenance.CreateDatabase();
                    sqlSugar.CodeFirst.InitTables<OperateData, AlarmData>();
                });
            }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-5-5 21:33 , Processed in 0.038264 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表