Trying to understand databases better in general, and sqlite3 in particular:
Are views in sqlite3 mainly an organizational feature, allowing complex queries to be broken into a series of smaller ones; or do views actually affect the performance of queries that use them?
I noticed that views are stored in the database itself as part of the schema. Are views stored on disk, updated dynamically as dependent tables are updated; or are they evaluated on demand?
Thanks.
All views have impact on database performance. Think of it as just that a "view" into the underlining data. As long as there are data reads and writes it will affect the performance and response to your database.
SQL Server views are helpful in many ways, for example in encapsulating complex multi-table query logic, allowing us to simplify client code. Views make queries faster to write, but they don't improve the underlying query performance.
The SQLite docs explains why this is so slow: Transaction speed is limited by disk drive speed because (by default) SQLite actually waits until the data really is safely stored on the disk surface before the transaction is complete. That way, if you suddenly lose power or if your OS crashes, your data is still safe.
Views will always be executed on demand (sqlite3 or otherwise), so the results they return are never persistently stored.
As for performance, while I can't speak to sqlite3 specifically, usually using views will have slightly less overhead as the query parser/planner doesn't have to reparse the raw sql on each execution. It can parse it once, store its execution strategy, and then use that each time the query is actually run.
The performance boost you see with this will generally be small, in the grand scheme of things. It really only helps if its a fast query that you're executing frequently. If its a slow query you execute infrequently, the overhead associated with parsing the query is insignificant. Views do, of course, provide a level of organization which is nice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With