我正在尝试一些方法来优化以下sql语句:
exe_sql "DELETE FROM tblEvent_type WHERE eguid in (SELECT rowid FROM tblEvent_basic WHERE sguid=11);";
exe_sql "DELETE FROM tblEvent_group WHERE eguid in (SELECT rowid FROM tblEvent_basic WHERE sguid=11);";
据说sqlite3在子查询中表现不佳,并注意到上面两个sql执行了两次"(SELECT rowid FROM tblEvent_basic WHERE sguid=11)"`,所以我想尝试将子查询拆分成如下所示:
result = exe_sql "(SELECT rowid FROM tblEvent_basic WHERE sguid=11);";
exe_sql "DELETE FROM tblEvent_type WHERE eguid in (result)
exe_sql "DELETE FROM tblEvent_group WHERE eguid in (result)
如何做到这一点呢?我不知道如何将parmater (result)绑定到sqlite中的后续语句。
"DELETE FROM tblEvent_group WHERE eguid in (?) #how to bind result here
我直接使用sqlite3 C应用编程接口。
发布于 2009-07-09 12:18:51
您实际上需要常用表表达式,但SQLite不支持。
另一种选择是将第一个查询的结果存储在临时表中,然后在两个delete语句中使用该表:
CREATE TEMP TABLE items AS SELECT rowid FROM tblEvent_basic WHERE sguid=11
DELETE FROM tblEvent_type WHERE eguid in (select rowid from items)
DELETE FROM tblEvent_group WHERE eguid in (select rowid from items)
DROP TABLE items
DROP TABLE是可选的,因为该表仅在与数据库的连接期间存在。
https://stackoverflow.com/questions/1103241
复制相似问题