12.5 非外键匹配性检测的提速
在两个表中,根据非外键的匹配性检测查找记录,优化提速。
根据销售表和客户表,查询 2014 年各城市有销售记录的客户数量。
| Sales |
|---|
| ID |
| CustomerID |
| Date |
| Amount |
| Customer |
|---|
| ID |
| Name |
| City |
| … |
当数据按去重字段有序时,可以使用 groups 函数的 @o 选项提速。
脚本:
| A | |
|---|---|
| 1 | =connect(“db”) |
| 2 | =A1.query(“select * from Customer”) |
| 3 | =A1.query@x(“select * from Sales where year(Date)=2014 order by CustomerID”) |
| 4 | =A3.groups@o(CustomerID) |
| 5 | =A2.join@i(ID, A4:CustomerID) |
| 6 | =A5.groups(City; count(1):CustomerCount) |
A1 连接数据库
A2 查询客户表
A3 查询 2014 年的销售记录,并按客户 ID 排序
A4 使用 groups 函数按客户 ID 去重,有序时使用 @o 选项
A5 使用 A.join@i() 函数连接过滤
A6 分组汇总每个城市的客户数量
运行结果:
| City | CustomerCount |
|---|---|
| Dongying | 6 |
| Tangshan | 7 |
| … | … |
