Skip to content

Commit 105947c

Browse files
committed
Optimize single table read performance
1 parent 7290109 commit 105947c

File tree

9 files changed

+139
-219
lines changed

9 files changed

+139
-219
lines changed

Examples/orm_vs/Program.cs

Lines changed: 110 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using FreeSql.Internal;
1+
using Dapper;
2+
using FreeSql.Internal;
3+
using Microsoft.Data.SqlClient;
24
using Microsoft.EntityFrameworkCore;
35
using SqlSugar;
46
using System;
@@ -12,14 +14,15 @@
1214
using System.Text;
1315
using System.Threading;
1416
using System.Threading.Tasks;
17+
using FreeSql;
1518

1619
namespace orm_vs
1720
{
1821
class Program
1922
{
2023
static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
21-
//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=20")
22-
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=20")
24+
.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=tedb;Pooling=true;Max Pool Size=20")
25+
//.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=20")
2326
//.UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=20")
2427
.UseAutoSyncStructure(false)
2528
.UseNoneCommandParameter(true)
@@ -32,30 +35,31 @@ static SqlSugarClient sugar
3235
{
3336
var db = new SqlSugarClient(new ConnectionConfig()
3437
{
35-
//ConnectionString = "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Min Pool Size=20;Max Pool Size=20",
36-
//DbType = DbType.SqlServer,
37-
ConnectionString = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Min Pool Size=20;Max Pool Size=20",
38-
DbType = DbType.MySql,
38+
ConnectionString = "Data Source=.;Integrated Security=True;Initial Catalog=tedb;Pooling=true;Min Pool Size=20;Max Pool Size=20",
39+
DbType = DbType.SqlServer,
40+
//ConnectionString = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Min Pool Size=20;Max Pool Size=20",
41+
//DbType = DbType.MySql,
3942
//ConnectionString = "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=21",
4043
//DbType = DbType.PostgreSQL,
4144
IsAutoCloseConnection = true,
4245
InitKeyType = InitKeyType.Attribute
4346
});
44-
db.Aop.OnLogExecuting = (sql, pars) =>
45-
{
46-
Console.WriteLine(sql);//输出sql,查看执行sql
47-
};
47+
//db.Aop.OnLogExecuting = (sql, pars) =>
48+
//{
49+
// Console.WriteLine(sql);//输出sql,查看执行sql
50+
//};
4851
return db;
4952
}
5053
}
5154

5255
class SongContext : DbContext
5356
{
5457
public DbSet<Song> Songs { get; set; }
58+
public DbSet<PatientExamination_2022> PatientExamination_2022s { get; set; }
5559
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
5660
{
57-
//optionsBuilder.UseSqlServer(@"Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Min Pool Size=21;Max Pool Size=21");
58-
optionsBuilder.UseMySql("Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Min Pool Size=21;Max Pool Size=21");
61+
optionsBuilder.UseSqlServer(@"Data Source=.;Integrated Security=True;Initial Catalog=tedb;Pooling=true;Min Pool Size=21;Max Pool Size=21");
62+
//optionsBuilder.UseMySql("Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Min Pool Size=21;Max Pool Size=21");
5963
//optionsBuilder.UseNpgsql("Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=21");
6064
}
6165

@@ -69,8 +73,101 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
6973
}
7074
}
7175

76+
//CREATE TABLE [dbo].[PatientExamination_2022] (
77+
// [Id] uniqueidentifier NOT NULL,
78+
// [CreateTime] datetime NOT NULL,
79+
// [ExamKindId] uniqueidentifier NOT NULL,
80+
// [ExamKindName] nvarchar(255) COLLATE Chinese_PRC_CI_AS NULL,
81+
// [PatientGuid] uniqueidentifier NOT NULL,
82+
// [PatientName] nvarchar(255) COLLATE Chinese_PRC_CI_AS NULL,
83+
// [AnesthesiaType] int NOT NULL,
84+
// [DiaRoomId] uniqueidentifier NULL,
85+
// [DiaRoomName] nvarchar(255) COLLATE Chinese_PRC_CI_AS NULL,
86+
// [QueueIndex] int NOT NULL,
87+
// [QueueNum] int NOT NULL,
88+
// [OrderDateTime] datetime NOT NULL,
89+
// [TimeType] int NOT NULL,
90+
// [SignInTime] datetime NULL,
91+
// [StartCheckTime] datetime NULL,
92+
// [EndCheckTime] datetime NULL,
93+
// [VerifyTime] datetime NULL,
94+
// [ReportTime] datetime NULL,
95+
// [ExaminationState] int NOT NULL
96+
//)
97+
[Table("PatientExamination_2022")]
98+
class PatientExamination_2022
99+
{
100+
public Guid Id { get; set; }
101+
public DateTime CreateTime { get; set; }
102+
public Guid ExamKindId { get; set; }
103+
public string ExamKindName { get; set; }
104+
public Guid PatientGuid { get; set; }
105+
public string PatientName { get; set; }
106+
public int AnesthesiaType { get; set; }
107+
public Guid? DiaRoomId { get; set; }
108+
public string DiaRoomName { get; set; }
109+
public int QueueIndex { get; set; }
110+
public int QueueNum { get; set; }
111+
public DateTime OrderDateTime { get; set; }
112+
public int TimeType { get; set; }
113+
public DateTime? SignInTime { get; set; }
114+
public DateTime? StartCheckTime { get; set; }
115+
public DateTime? EndCheckTime { get; set; }
116+
public DateTime? VerifyTime { get; set; }
117+
public DateTime? ReportTime { get; set; }
118+
public int ExaminationState { get; set; }
119+
}
120+
121+
static void TestFreeSqlSelectPatientExamination_2022()
122+
{
123+
var list = fsql.Select<PatientExamination_2022>().Limit(40000).ToList();
124+
//var list = fsql.Ado.Query<PatientExamination_2022>("select top 40000 * from PatientExamination_2022");
125+
}
126+
static void TestEfSelectPatientExamination_2022()
127+
{
128+
using (var ctx = new SongContext())
129+
{
130+
var list = ctx.PatientExamination_2022s.Take(40000).AsNoTracking().ToList();
131+
}
132+
}
133+
static void TestSqlSugarSelectPatientExamination_2022()
134+
{
135+
var list = sugar.Queryable<PatientExamination_2022>().Take(40000).ToList();
136+
}
137+
static void TestDapperSelectPatientExamination_2022()
138+
{
139+
using (var conn = new SqlConnection("Data Source=.;Integrated Security=True;Initial Catalog=tedb;Pooling=true;Min Pool Size=21;Max Pool Size=22"))
140+
{
141+
var list = conn.Query<PatientExamination_2022>("select top 40000 * from PatientExamination_2022");
142+
}
143+
}
144+
145+
static DbConnection fsqlConn = null;
72146
static void Main(string[] args)
73147
{
148+
var count = 0;
149+
var sws = new List<long>();
150+
Console.WriteLine("观察查询4万条记录内存,按 Enter 进入下一次,按任易键即出程序。。。");
151+
//while(Console.ReadKey().Key == ConsoleKey.Enter)
152+
//using (var fcon = fsql.Ado.MasterPool.Get())
153+
//{
154+
//fsqlConn = fcon.Value;
155+
for (var a = 0; a < 80; a++)
156+
{
157+
Stopwatch sw = Stopwatch.StartNew();
158+
TestFreeSqlSelectPatientExamination_2022();
159+
//TestEfSelectPatientExamination_2022();
160+
//TestSqlSugarSelectPatientExamination_2022();
161+
//TestDapperSelectPatientExamination_2022();
162+
sw.Stop();
163+
sws.Add(sw.ElapsedMilliseconds);
164+
Console.WriteLine($"第 {++count} 次,查询4万条记录, {sw.ElapsedMilliseconds}ms,平均 {(long)sws.Average()}ms");
165+
}
166+
//}
167+
Console.ReadKey();
168+
fsql.Dispose();
169+
return;
170+
74171
//fsql.CodeFirst.SyncStructure(typeof(Song), typeof(Song_tag), typeof(Tag));
75172
//sugar.CodeFirst.InitTables(typeof(Song), typeof(Song_tag), typeof(Tag));
76173
//sugar创建表失败:SqlSugar.SqlSugarException: Sequence contains no elements

FreeSql.DbContext/FreeSql.DbContext.xml

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)