所以我附加了多个数据库,然后创建了一个TEMP VIEW,它结合了各种数据库中的所有虚拟表。
theDatabase.execSQL("CREATE TEMP VIEW virtualView AS SELECT * FROM Virtual_Sites UNION SELECT * FROM db1.Virtual_Sites UNION SELECT * FROM db2.Virtual_Sites");
是否可以在此VIEW上使用MATCH查询?
theDatabase.rawQuery(SELECT * FROM virtualView WHERE all_text MATCH 21033, null)
我目前收到此错误。
sqlite returned: error code = 1, msg = statement aborts at 46: [SELECT * FROM virtualView WHERE all_text MATCH 21033] unable to use function MATCH in the requested context, db=xxx
exception: SQL logic error or missing database; query: SELECT * FROM virtualView WHERE all_text MATCH 21033
我已经读过以这种方式创建VIEW不会继承基础表索引,这就是我无法做MATCH的原因吗?如果有任何解决方法?
由于
答案 0 :(得分:1)
MATCH
仅适用于实现此运算符的虚拟表,而不适用于视图。
您必须重写所有查询才能在各个表格上使用MATCH
,并将这些结果与UNION ALL
合并。
或者,将所有表的数据复制到一个临时表中。
答案 1 :(得分:0)
这是我发现的。 VIEWS在组合多个数据库/表时效率不高,因为它们在包含2个以上表时会松散其下划线索引。
<强> ATTACH 强>
String newDbPath = DB_PATH + DB_NAME;
theDatabase.execSQL("ATTACH DATABASE '" + newDbPath + "' as db1;");
效率低下
Create VIEW
theDatabase.execSQL("CREATE TEMP VIEW view1 AS SELECT * FROM Sites_hsf UNION SELECT * FROM db1.Sites_hsf UNION SELECT * FROM db2.Sites_hsf;");
Query VIEW
sqlStatement = "SELECT * FROM view1 s INNER JOIN view2 con ON s.site_uid = con.site_uid AND con.ap_active = 1 WHERE s.site_uid = 1114331";
执行时间 16281毫秒
有效的方式
sqlStatement = "SELECT * FROM Sites_hsf s INNER JOIN Connections_hsf con ON s.site_uid = con.site_uid AND con.ap_active = 1 WHERE s.site_uid = 1114331 " +
"UNION ALL " +
"SELECT * FROM db1.Sites_hsf s1 INNER JOIN db1.Connections_hsf con1 ON s1.site_uid = con1.site_uid AND con1.ap_active = 1 WHERE s1.site_uid = 1114331 " +
"UNION ALL " +
"SELECT * FROM db2.Sites_hsf s2 INNER JOIN db2.Connections_hsf con2 ON s2.site_uid = con2.site_uid AND con2.ap_active = 1 WHERE s2.site_uid = 1114331";
执行时间 4毫秒
使用CL。对UNION ALL的答案进行匹配
sqlStatement = "SELECT * FROM Virtual_Sites_hsf WHERE all_text MATCH '40227' " +
"UNION ALL SELECT * FROM db1.Virtual_Sites_hsf WHERE all_text MATCH '40227'" +
"UNION ALL SELECT * FROM db2.Virtual_Sites_hsf WHERE all_text MATCH '40227'"
希望这有助于某人。