1.4 把枚举字符串变成小整数

将取值可能有限的枚举字符串转换成整数后可以获得更好的存储和计算性能。

1.4.1 转储时转换

将枚举字段用取值序列的序号代替,这里以 ShipVia 举例

A
1 =file(“ShipVia.btx”).import@bi()
2 =file(“Orders.txt”).cursor@mt(CustomerID:string, OrderDate:datetime, ProductID:string, Quantity:int, Unit:string, Price:decimal, Amount:decimal, EmployeeID:int,EmployeeName:string,ShipVia:string).run(OrderDate=days@o(OrderDate), ShipVia=A1.pos@b(ShipVia))
3 =file(“Orders.ctx”).create@y(CustomerID,OrderDate,ProductID, Quantity, Unit, Price, Amount, EmployeeID,EmployeeName,ShipVia)
4 =A3.append(A2)
5 =A3.close()

A1 读入 ShipVia 字段的取值序列,一般在文件中已经排好序了,@i 选项表示读成序列

取值序列也可以直接写在脚本中

A
1 [Federal Shipping,Speedy Express,United Package]

A1 把 ShipVia 转成对应序列中的序号,因为取值序列有序,所以使用 @b 选项

1.4.2 用转换后的数据计算

SQL

SELECT sum(Orders.Quantity) AS Quantity,ShipVia
FROM Orders
WHERE ShipVia='Federal Shipping' or ShipVia='Speedy Express'
GROUP BY ShipVia

SPL

A
1 >arg1=“Federal Shipping”,arg2=“Speedy Express”
2 =file(“ShipVia.btx”).import@ib()
3 =arg1=A2.pos@b(arg1),arg2=A2.pos@b(arg2)
4 =file(“Orders.ctx”).open().cursor@mx(Quantity, ShipVia; ShipVia==arg1 || ShipVia==arg2)
5 =A4.groups(ShipVia;sum(Quantity):Quantity)
6 =A5.run(ShipVia=A2(ShipVia))

A3 按取值序列的顺序,计算参数值对应的小整数
A6 把结果集中的 ShipVia 转回对应的字符串值


SQL

SELECT sum(Orders.Quantity) AS Quantity,ShipVia
FROM Orders
WHERE ShipVia in ('Federal Shipping','Speedy Express')
GROUP BY ShipVia

SPL

A
1 >arg=[“Federal Shipping”,“Speedy Express”]
2 =file(“ShipVia.btx”).import@ib()
3 =A2.(arg.contain@b(~))
4 =file(“Orders.ctx”).open().cursor@mx(Quantity, ShipVia;A3(ShipVia))
5 =A4.groups(ShipVia;sum(Quantity):Quantity)
6 =A5.run(ShipVia=A2(ShipVia))

A3 过滤参数是集合。按取值序列的顺序,计算对应的 boolean 序列,表示当前成员是否在参数集合中
A4 过滤时用序号从 A3 中取值,以判断当前值在参数集合中
A6 把结果集中的 ShipVia 转回对应的字符串值