我在SQLite数据库中有两个表,就像这样
| t1 |
|===============|
| t1_id | other |
|-------|-------|
| 1 | sdfds |
| 2 | asdaa |
| 3 | lkjeq |
| t2 |
|=======================|
| t2_id | t1_id | other |
|-------|-------|-------|
| 1 | 1 | dggeh |
| 2 | 1 | iohio |
| 3 | 3 | ytucc |
| 4 | 3 | .noih |
| 5 | 3 | /oioi |
大约有。两个表中有300K +行。我希望在 t2 中有相关行的 t1 计数以及没有的行。也就是说,
"count of t1 with related t2" : 2
"count of t1 with no related t2": 1
当然,我可以从中获得“与相关的t2相关的t1计数”
SELECT Count(*) FROM t1 JOIN t2 ON t1.t1_id = t2.t1_id;
从 t1 的总数中减去上述值,从而获得和“与t2无关联的t1计数”。但是,如何从SQL查询中高效获取呢?我尝试了以下
EXPLAIN QUERY PLAN SELECT t1_id FROM t1 AS t
WHERE NOT EXISTS (SELECT * FROM t2 WHERE t.t1_id = t2.t1_id);
,我发现未使用表 t2 中t1_id
上的索引。当我尝试查询时,它会花费很长时间,例如100秒。
答案 0 :(得分:0)
几种可能性:
componentDidMount() {
this.setState({ loading: true })
console.log('app mounted');
fetch('https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=8')
.then(data => data.json())
.then(data => this.setState({ data: data.articles, loading: false}))
.then(() => {
console.log('Results', this.state.data);
})
}
或
SELECT count(*) FROM
(SELECT t1_id FROM t1
EXCEPT
SELECT t1_id FROM t2);
或(与原件略有不同):
SELECT count(*)
FROM t1
WHERE t1_id NOT IN (SELECT t1_id FROM t2);
所有这三个都应在SELECT count(*)
FROM t1
WHERE NOT EXISTS (SELECT 1 FROM t2 WHERE t1.t1_id = t2.t1_id);
上使用索引。您必须对真实数据进行基准测试,以查看哪种速度更快。