luadbi和luasql两种lua duckdb驱动的性能对比

利用自定义函数输出各种类型数据的luadbi驱动ducktpch.lua

lua 复制代码
DBI = require "DBI"

dbd, err = DBI.Connect( 'DuckDB', 'tpch_duckdb', 'dbuser', 'password' )
assert(dbd, err)

dbd:autocommit(true)


statement = assert(dbd:prepare( "SELECT * from lineitem" ))
statement:execute()
tabcol={}
tabval={}
i=1
for row in statement:rows(true) do

if (i==1)
then
 for key, value in pairs(row) do tabcol[i]=key i=i+1 end
 headerstring=table.concat(tabcol, ",")
 print(headerstring)
end


for key, value in pairs(tabcol)do tabval[key]=row[tabcol[key]] end
rowstring=table.concat(tabval, ",")
print(rowstring)
end

利用duckdb C API函数duckdb_value_varchar输出各种类型数据的字符串的luasql驱动ducktpch2.lua

lua 复制代码
driver = require"luasql.duckdb"

env = assert (driver.duckdb())

con = assert (env:connect("tpch_duckdb"))

cur = assert (con:execute"SELECT * from lineitem")

row = cur:fetch ({}, "a")	-- the rows will be indexed by field names

tabcol={}
i=1
for key, value in pairs(row) do tabcol[i]=key i=i+1 end
headerstring=table.concat(tabcol, ",")
print(headerstring)

tabval={}

while row do
for key, value in pairs(tabcol)do tabval[key]=row[tabcol[key]] end
rowstring=table.concat(tabval, ",")
print(rowstring)
  row = cur:fetch (row, "a")	-- reusing the table of results
end

执行结果

复制代码
root@66d4e20ec1d7:/par/luadbi# lua ../ducktpch.lua
l_linestatus,l_linenumber,l_comment,l_tax,l_commitdate,l_extendedprice,l_shipdate,l_suppkey,l_partkey,l_receiptdate,l_shipmode,l_returnflag,l_discount,l_shipinstruct,l_quantity,l_orderkey
O,1,to beans x-ray carefull,0.02,1996-02-12,24386.67,1996-03-13,785,15519,1996-03-22,TRUCK,N,0.04,DELIVER IN PERSON,17.00,1

root@66d4e20ec1d7:/par/luadbi# time lua ../ducktpch.lua >/dev/null

real	0m7.979s
user	0m7.756s
sys	0m0.352s


root@66d4e20ec1d7:/par/luadbi# time lua ../ducktpch2.lua >/dev/null

real	0m9.301s
user	0m8.424s
sys	0m1.140s

root@66d4e20ec1d7:/par/luadbi# time lua ../ducktpch.lua >/tmp/1.csv 

real	0m9.137s
user	0m8.196s
sys	0m1.096s
root@66d4e20ec1d7:/par/luadbi# time lua ../ducktpch2.lua > /tmp/2.csv

real	0m9.905s
user	0m8.636s
sys	0m1.296s

ls -l /tmp/1.csv  /tmp/2.csv
-rw-r--r-- 1 root root 75524898 Nov  3 14:59 /tmp/1.csv
-rw-r--r-- 1 root root 75448328 Nov  3 15:00 /tmp/2.csv

两种驱动没有明显差异。

如果把print到控制台再重定向改为直接对文件写操作,则性能提升,不过这与duckdb驱动无关。

lua 复制代码
-- 以附加的方式打开只写文件
file = io.open("testlua.csv", "a")

-- 设置默认输出文件为 test.lua
io.output(file)
io.write(headerstring)
io.write("\n")

while row do
 for key, value in pairs(tabcol)do tabval[key]=row[tabcol[key]] end
local rowstring=table.concat(tabval, ",")
-- print(rowstring)
io.write(rowstring)
io.write("\n")
  row = cur:fetch (row, "a")	-- reusing the table of results
end
-- 关闭打开的文件
io.close(file)

排除print慢的操作,都用直接写文件来对比,还是平局。

复制代码
root@66d4e20ec1d7:/par/luadbi# time lua ../ducktpchf.lua 

real	0m8.632s
user	0m8.096s
sys	0m0.136s


root@66d4e20ec1d7:/par/luadbi# time lua ../ducktpch2f.lua 

real	0m7.259s
user	0m7.068s
sys	0m0.348s

完整直接写文件脚本如下

luadbi ducktpchf.lua

lua 复制代码
DBI = require "DBI"

dbd, err = DBI.Connect( 'DuckDB', 'tpch_duckdb', 'dbuser', 'password' )
assert(dbd, err)

dbd:autocommit(true)


statement = assert(dbd:prepare( "SELECT * from lineitem" ))
statement:execute()
tabcol={}
tabval={}
i=1

-- 以附加的方式打开只写文件
file = io.open("testlua.csv", "w")

-- 设置默认输出文件为 test.lua
io.output(file)


for row in statement:rows(true) do

if (i==1)
then
 for key, value in pairs(row) do tabcol[i]=key i=i+1 end
 headerstring=table.concat(tabcol, ",")
 print(headerstring)
 io.write(headerstring)
io.write("\n")
end


for key, value in pairs(tabcol)do tabval[key]=row[tabcol[key]] end
rowstring=table.concat(tabval, ",")
-- print(rowstring)
io.write(rowstring)
io.write("\n")
end
-- 关闭打开的文件
io.close(file)

luadbi ducktpch2f.lua

lua 复制代码
driver = require"luasql.duckdb"

env = assert (driver.duckdb())

con = assert (env:connect("tpch_duckdb"))

cur = assert (con:execute"SELECT * from lineitem")

local row = cur:fetch ({}, "n")	-- the rows will be indexed by field number

local tabcol={}
i=1
for key, value in pairs(row) do tabcol[i]=key i=i+1 end
local headerstring=table.concat(tabcol, ",")
print(headerstring)

local tabval={}

-- 以附加的方式打开只写文件
file = io.open("testlua.csv", "w")

-- 设置默认输出文件为 test.lua
io.output(file)
io.write(headerstring)
io.write("\n")
while row do
 for key, value in pairs(tabcol)do tabval[key]=row[tabcol[key]] end
local rowstring=table.concat(tabval, ",")
-- print(rowstring)
io.write(rowstring)
io.write("\n")
  row = cur:fetch (row, "n")	-- reusing the table of results
end
-- 关闭打开的文件
io.close(file)
相关推荐
国服第二切图仔2 小时前
Rust开发实战之使用 Reqwest 实现 HTTP 客户端请求
开发语言·http·rust
weixin_497845542 小时前
Windows系统Rust安装慢的问题
开发语言·后端·rust
骚戴3 小时前
PDF或Word转图片(多线程+aspose+函数式接口)
java·开发语言
姓蔡小朋友3 小时前
SpringDataRedis
java·开发语言·redis
Predestination王瀞潞4 小时前
Python3:Eighth 函数
开发语言·python
夜晚中的人海4 小时前
【C++】分治-快速排序算法习题
开发语言·c++·排序算法
爱编程的鱼4 小时前
想学编程作为今后的工作技能,学哪种语言适用性更强?
开发语言·算法·c#·bug
yugi9878384 小时前
基于MATLAB的心电信号去噪
开发语言·matlab
国服第二切图仔4 小时前
Rust入门开发之Rust中如何实现面向对象编程
开发语言·后端·rust