2011/12/15 Alexandr Němec <[email protected]>

>
> Dear all,
>
> just a quick question, I did not find the answer in the various technical
> documents. I have two identical tables with a id INTEGER as a primary key,
> which means that SELECTions ORDERed BY id are very fast. Now if I do SELECT
> * FROM table1 UNION ALL SELECT * FROM table2 ORDER by id, rows from both
> tables appear in the resultset. But I was surprised that the speed of the
> sorting is still the same (as for one indexed column). Does it mean, that
> SQLite can (somehow) use the index to speed up the sorting when UNIONing
> several tables?
>

Yes.

The ORDER BY applies to the complete result of the UNION ALL.  So what
SQLite does in this case is run both subqueries in a parallel, delivering
the results of each subquery in sorted order (which is easy since the
source key is the primary key), and merge the results together.



>
> Thanks
> Alex
> _______________________________________________
> sqlite-users mailing list
> [email protected]
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
>



-- 
D. Richard Hipp
[email protected]
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to