公众号关注 “GitHub 宝典”
设为 “星标”,每天带你逛 GitHub!
上世纪 70 年代,当时首屈一指的科技公司 IBM 开发了一种用于管理其数据库系统的语言:SQL。
在接下来的数十年间,由于 SQL 的操作简单,对数据处理友好,让程序员针对数据的增删改查变得更为方便,使得它被广泛应用于各种企业和 Web 应用程序。
然而,SQL 也有一些坏处。比如,SQL 无法处理非关系型数据和大数据量的查询,性能较差,需要开发人员花费时间,不断优化查询语句和数据库结构。
在 Java、Python 等复杂的开发环境与编码需求中,SQL 的短板无疑暴露得更为明显。
因此,今天给大家推荐一款可用于替代 SQL 的结构化编程语言:SPL。
独立于数据库的轻量级计算能力;
支持有序计算和分步计算;
直接读取多个数据库,实现混合数据计算;
提供了大量的基础高性能算法(其中很多是业界首创)、高效的存储格式;
与 Excel 结合,支持在 Excel 中使用 SPL 函数。
SQL 困难的分析探讨
| sales_amount | 销售业绩表 |
| sales | 销售员姓名,假定无重名 |
| product | 销售的产品 |
| amount | 该销售员在该产品上的销售额 |
现在我们想知道出空调和电视销售额都在前 10 名的销售员名单。
1. 按空调销售额排序,找出前 10 名;
2. 按电视销售额排序,找出前 10 名;
select * from( select top 10 sales from sales_amount where product=\'AC\' order by amount desc )intersect( select top 10 sales from sales_amount where product=\'TV\' order by amount desc )
with A asselect top 10 sales from sales_amount where product=\'AC\' order by amount descB asselect top 10 sales from sales_amount where product=\'TV\' order by amount descselect * from A intersect B
句子没有更短,但分步后思路确实变清晰了。
1. 列出所有产品;
2.将每种产品的前 10 名取出,分别保存;
1.将数据按产品分组,将每组排序,取出前 10 名;
select salesfrom ( select sales,from ( select sales,rank() over (partition by product order by amount desc ) rankingfrom sales_amount)where ranking <=10 )group by saleshaving count(*)=(select count(distinct product) from sales_amount)
这是能写出来,但这样复杂的 SQL,有多少人会写呢?
虽然 SQL 有集合概念,但并未把集合作为一种基础数据类型提供,不能让变量或字段的取值是个集合,除了表之外也没有其它集合形式的数据类型,这使得大量集合运算在思维和书写时都需要绕路。
select salesfrom ( select A.sales sales, A.product product,(select count(*)+1 from sales_amountwhere A.product=product AND A.amount<=amount) rankingfrom sales_amount A )where product=\'AC\' AND ranking<=10
select salesfrom ( select A.sales sales, A.product product, count(*)+1 rankingfrom sales_amount A, sales_amount Bwhere A.sales=B.sales and A.product=B.product AND A.amount<=B.amountgroup by A.sales,A.product )where product=\'AC\' AND ranking<=10
这样的 SQL 语句,专业程序员写出来也未必容易吧!而仅仅是计算了一个前 10 名。
| employee | 员工表 |
| name | 员工姓名,假定无重名 |
| gender | 员工性别 |
我们已经计算出 “好” 销售员的名单,比较自然的想法,是用名单到花名册时找出其性别,再计一下数。但在 SQL 中要跨表获得信息需要用表间连接,这样,接着最初的结果,SQL 就会写成:
select employee.gender,count(*)from employee,( ( select top 10 sales from sales_amount where product=\'AC\' order by amount desc )intersect( select top 10 sales from sales_amount where product=\'TV\' order by amount desc ) ) Awhere A.sales=employee.namegroup by employee.gender
select sales.gender,count(*)from (…) // …是前面计算“好”销售员的SQLgroup by sales.gender
显然,这个句子不仅更清晰,同时计算效率也会更高(没有连接计算)。
mov ax,3mov bx,5mul bx,7add ax,bx
这样的代码无论书写还是阅读都远不如 3+5*7 了(要是碰到小数就更要命了)。虽然对于熟练的程序员也算不了太大的麻烦,但对于大多数人而言,这种写法还是过于晦涩难懂了,从这个意义上讲,FORTRAN 确实是个伟大的发明。
更多例子
这些问题本身应该也算不上很复杂,都是在日常数据分析中经常会出现的,但已经很难为 SQL 了。
集合无序
select name, birthdayfrom (select name, birthday, row_number() over (order by birthday) rankingfrom employee )where ranking=(select floor((count(*)+1)/2) from employee)
select max (consecutive_day)from (select count(*) (consecutive_dayfrom (select sum(rise_mark) over(order by trade_date) days_no_gainfrom (select trade_date,case whenclosing_price>lag(closing_price) over(order by trade_date)then 0 else 1 END rise_markfrom stock_price) )group by days_no_gain)
集合化不彻底
select * from employeewhere to_char (birthday, ‘MMDD’) in( select to_char(birthday, \'MMDD\') from employeegroup by to_char(birthday, \'MMDD\')having count(*)>1 )
select namefrom (select namefrom (select name,rank() over(partition by subject order by score DESC) rankingfrom score_table)where ranking<=10)group by namehaving count(*)=(select count(distinct subject) from score_table)
缺乏对象引用
select A.*from employee A, department B, employee Cwhere A.department=B.department and B.manager=C.name andA.gender=\'male\' and C.gender=\'female\'
select * from employeewhere gender=\'male\' and department in(select department from departmentwhere manager in(select name from employee where gender=\'female\'))
where gender=\'male\' and department.manager.gender=\'female\'select name, company, first_companyfrom (select employee.name name, resume.company company,row_number() over(partition by resume. nameorder by resume.start_date) work_seqfrom employee, resume where employee.name = resume.name)where work_seq=1
select name,(select company from resumewhere name=A.name andstart date=(select min(start_date) from resumewhere name=A.name)) first_companyfrom employee A
SPL 的引入
| A | |
| 1 | =employee.sort(birthday) |
| 2 | =A1((A1.len()+1)/2) |
对于以有序集合为基础的 SPL 来说,按位置取值是个很简单的任务。
任务 2
| A | |
| 1 | =stock_price.sort(trade_date) |
| 2 | =0 |
| 3 | =A1.max(A2=if(close_price>close_price[-1],A2+1,0)) |
SPL 按自然的思路过程编写计算代码即可。
任务 3
| A | |
| 1 | =employee.group(month(birthday),day(birthday)) |
| 2 | =A1.select(~.len()>1).conj() |
SPL 可以保存分组结果集,继续处理就和常规集合一样。
任务 4
| A | |
| 1 | =score_table.group(subject) |
| 2 | =A1.(~.rank(score).pselect@a(~<=10)) |
| 3 | =A1.(~(A2(#)).(name)).isect() |
使用 SPL 只要按思路过程写出计算代码即可。
任务 5
| A | |
| 1 | =employee.select(gender==\"male\" && department.manager.gender==\"female\") |
支持对象引用的 SPL 可以简单地将外键指向记录的字段当作自己的属性访问。
任务 6
| A | |
| 1 | =employee.new(name,resume.minp(start_date).company:first_company) |
SPL 支持将子表集合作为主表字段,就如同访问其它字段一样,子表无需重复计算。
…Class.forName(\"com.esproc.jdbc.InternalDriver\");Connection conn =DriverManager.getConnection(\"jdbc:esproc:local://\");Statement st = connection.();CallableStatement st = conn.prepareCall(\"{call xxxx(?,?)}\");st.setObject(1, 3000);st.setObject(2, 5000);ResultSet result=st.execute();...
总的来说,作为 SQL 的开源替代解决方案,相信 SPL 项目的未来应用前景将会十分广泛。
如果你想减轻当前项目的数据库计算负担,提升编程效率,优化开发体验,建议你尝试下 SPL 这个开源项目。
GitHub:https://github.com/SPLWare/esProc