Insert data in database (Mysql) from Lua table -
Insert data in database (Mysql) from Lua table -
i trying feed table in mysql database 1 000 000 lines. using lua , function :
conn:execute("insert orders (dates, ordertype) values ('"..tab[1][dateindex]......
for each line. problem is long , need more efficiency. have others solutions (maybe creating .csv , loading mysql, maybe there function can load matrix in database more efficiently,...). using lua obligation using existing project.
thank help
first can stop committing on each insert.
also can utilize prepared query. provides lua-dbi , lua-odbc utilize odbc.
local env = odbc.environment() lcoal db = env:driverconnect{ driver = is_windows , '{mysql odbc 5.2 ansi driver}' or 'mysql'; db='test'; uid='root'; }; cnn:set_autocommit(false) local stmt = db:prepare("insert orders (dates, ordertype) values(?,?)") i, row in ipairs(tab) stmt:bindstr(row[dateindex]) ... stmt:execute() if % 1000 == 0 cnn:commit() end end
also odbc provide variables. may faster because not phone call sqlbindparam each time.
-- create stmt preview ... local datevalue = odbc.date():bind_param(stmt, 1) local ordervalue = odbc.ulong():bind_param(stmt, 2) i, row in ipairs(tab) datevalue:set(row[1]) -- info yyyy-mm-dd e.g. 2014-10-14 ordervalue:set(row[2]) stmt:execute() ... -- same preview
mysql insert lua bigdata lua-table
Comments
Post a Comment