Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQLite |从两个(模式)相同的表中检索数据_Sql_Database_Sqlite - Fatal编程技术网

SQLite |从两个(模式)相同的表中检索数据

SQLite |从两个(模式)相同的表中检索数据,sql,database,sqlite,Sql,Database,Sqlite,我有一个SQLite数据库,通过下面的查询从两个表中获取数据 这是最好、最有表现力的方式吗?我已将报警对象存储在数据库中。因此,很容易会有超过一百万个条目。对于您的特定查询,最好先限制每个表中的行,如下所示: with h as ( select h.* from history h where Station = @station and TimeStampCome <= @till and TimeStampCome >= @from

我有一个SQLite数据库,通过下面的查询从两个表中获取数据


这是最好、最有表现力的方式吗?我已将报警对象存储在数据库中。因此,很容易会有超过一百万个条目。

对于您的特定查询,最好先限制每个表中的行,如下所示:

with h as (
      select h.*
      from history h
      where Station = @station and TimeStampCome <= @till and TimeStampCome >= @from 
      order by TimeStampCome desc
      limit 101
     ),
     p as (
      select p.*
      from pending p
      where Station = @station and TimeStampCome <= @till and TimeStampCome >= @from 
      order by TimeStampCome desc
      limit 101
     )
select pe.*
from (select h.* from h union all
      select p.* from p
     ) pe
order by TimeStampCome desc
limit 100 offset 1;
但是,如果开始使用不同的偏移,这将变得不太可行

请注意,如果性能是一个问题,请从Station上的索引开始,时间戳出现在两个表中。

Ah这是一个很好的索引提示!我根据提交的过滤器站构建查询,从,到。。。所以这不会是个问题。
with h as (
      select h.*
      from history h
      where Station = @station and TimeStampCome <= @till and TimeStampCome >= @from 
      order by TimeStampCome desc
      limit 101
     ),
     p as (
      select p.*
      from pending p
      where Station = @station and TimeStampCome <= @till and TimeStampCome >= @from 
      order by TimeStampCome desc
      limit 101
     )
select pe.*
from (select h.* from h union all
      select p.* from p
     ) pe
order by TimeStampCome desc
limit 100 offset 1;