1

所以我附加了多个数据库,然后创建了一个 TEMP VIEW,它结合了来自各种数据库的所有 virtualTables。

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 的原因吗?如果是这样,有什么解决方法吗?

谢谢

4

2 回答 2

1

MATCH仅适用于实现此运算符的虚拟表,而不适用于视图。

您将不得不重写所有查询以MATCH在各个表上使用,并将这些结果与UNION ALL.

或者,将所有表的数据复制到一个临时表中。

于 2013-03-05T11:59:00.380 回答
0

这就是我发现的。VIEWS 在组合多个数据库/表时效率低下,因为一旦它们包含超过 2 个表,它们就会失去下划线索引。

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 毫秒


MATCH 使用 CL.'s answer of 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'"

希望这可以帮助某人。

于 2013-03-05T17:14:42.253 回答