Quellcode durchsuchen

提高查询效率

master
qdjjx vor 3 Jahren
Ursprung
Commit
d869075940

+ 13
- 1
专题/后端/2021.2.3好用的sql语句.md Datei anzeigen

@@ -2,4 +2,16 @@
2 2
 BEGIN
3 3
 SELECT SUM(power),yyyymmdd FROM powers WHERE monthweekday="HOURS" AND StartTime<=STR_TO_DATE(yyyymmdd,'%Y%m%d%H') AND EndTime>=STR_TO_DATE(yyyymmdd,'%Y%m%d%H') GROUP BY yyyymmdd;
4 4
 END
5
-```
5
+```
6
+
7
+```
8
+select count(*) from breakerdatas where mac='98CC4D213004'
9
+select count(*) from breakerdatas where mac='187ED53338C4'
10
+select count(*) from breakerdatas where mac='187ED53159E4'
11
+select count(*) from breakerdatas where mac='98CC4D213003'
12
+
13
+delete from breakerdatas where mac='187ED53338C4' and str_to_date(CreateTime, '%Y-%m-%d %H:%i:%s') <= str_to_date('2021-02-23 18:54:00', '%Y-%m-%d %H:%i:%s')
14
+
15
+select CreateTime, mac, addr from  breakerdatas where mac='187ED53338C4' and addr='1' and str_to_date(CreateTime, '%Y-%m-%d %H:%i:%s') >= str_to_date('2021-02-23 19:30:00', '%Y-%m-%d %H:%i:%s')
16
+```
17
+

+ 65
- 0
专题/后端/DDD/9、EF Core使用Sql语句提高查询效率.md Datei anzeigen

@@ -0,0 +1,65 @@
1
+一种会报错的写法
2
+
3
+```
4
+var macBreakerDatas = _breakerDataRepo.GetAll().Where(t => t.mac == mac.Mac);
5
+
6
+var removedBreakerDatas = macBreakerDatas.Where(t => (DateTime.Now - DateTime.Parse(t.CreateTime)).Minutes >= 65);
7
+
8
+_breakerDataRepo.RemoveRange(removedBreakerDatas);
9
+```
10
+
11
+
12
+
13
+不报错,但查询效率慢
14
+
15
+```
16
+var macBreakerDatas = _breakerDataRepo.GetAll().Where(t => t.mac == mac.Mac);
17
+
18
+var removedBreakerDatas = macBreakerDatas.ToList().Where(t => (DateTime.Now - DateTime.Parse(t.CreateTime)).Minutes >= 65);
19
+
20
+_breakerDataRepo.RemoveRange(removedBreakerDatas);
21
+```
22
+
23
+
24
+
25
+修改基础设施层
26
+
27
+```
28
+    public interface IOpenBreakerDataRepository : IRepository<BreakerData, Guid>
29
+    {
30
+        IQueryable<BreakerData> GetAll();
31
+        IQueryable<BreakerData> GetAllBySql(string sql);
32
+    }
33
+    public class OpenBreakerDataRepository : Repository<BreakerData, Guid, OpenAPIContext>, IOpenBreakerDataRepository
34
+    {
35
+        public OpenBreakerDataRepository(OpenAPIContext context) : base(context)
36
+        {
37
+
38
+        }
39
+
40
+        public IQueryable<BreakerData> GetAll()
41
+        {
42
+            return DbContext.BreakerDatas.Where(t => true);
43
+            
44
+        }
45
+
46
+        public IQueryable<BreakerData> GetAllBySql(string sql)
47
+        {
48
+            return DbContext.BreakerDatas.FromSqlRaw(sql);
49
+        }
50
+    }
51
+```
52
+
53
+
54
+
55
+最后改成
56
+
57
+```
58
+string sixtyFiveAgoTimeStr = DateTime.Now.AddMinutes(-65).ToFullTimeStr();
59
+
60
+string rawSql = $"select * from  breakerdatas where mac='{mac.Mac}' and str_to_date(CreateTime, '%Y-%m-%d %H:%i:%s') <= str_to_date('{sixtyFiveAgoTimeStr}', '%Y-%m-%d %H:%i:%s')";
61
+
62
+var removedBreakerDatas = _breakerDataRepo.GetAllBySql(rawSql);
63
+_breakerDataRepo.RemoveRange(removedBreakerDatas);
64
+```
65
+

Laden…
Abbrechen
Speichern