SQLite Forum

Should SQLite be able to optimize this query?
Login
```
explain query plan select country_long, count(*)
from (
  select * from [global-power-plants] order by rowid
)
where country_long is not null
group by country_long
order by count(*) desc
```
[Output](https://global-power-plants.datasettes.com/global-power-plants?sql=explain+query+plan+select+country_long%2C+count%28*%29%0D%0Afrom+%28%0D%0A++select+*+from+%5Bglobal-power-plants%5D+order+by+rowid%0D%0A%29%0D%0Awhere+country_long+is+not+null%0D%0Agroup+by+country_long%0D%0Aorder+by+count%28*%29+desc):
```
id,parent,notused,detail
2,0,0,CO-ROUTINE 1
5,2,0,SCAN TABLE global-power-plants
52,0,0,SCAN SUBQUERY 1
57,0,0,USE TEMP B-TREE FOR GROUP BY
92,0,0,USE TEMP B-TREE FOR ORDER BY
```
And for the query without the `order by rowid`:
```
explain query plan select country_long, count(*)
from (
  select * from [global-power-plants]
)
where country_long is not null
group by country_long
order by count(*) desc
```
[Output](https://global-power-plants.datasettes.com/global-power-plants?sql=explain+query+plan+select+country_long%2C+count%28*%29%0D%0Afrom+%28%0D%0A++select+*+from+%5Bglobal-power-plants%5D%0D%0A%29%0D%0Awhere+country_long+is+not+null%0D%0Agroup+by+country_long%0D%0Aorder+by+count%28*%29+desc):
```
id,parent,notused,detail
8,0,0,"SCAN TABLE global-power-plants USING INDEX ""global-power-plants_country_long"""
40,0,0,USE TEMP B-TREE FOR ORDER BY
```