I am building a flutter app with web support and using drift as local storage db. I have a table with following schema:
CREATE TABLE Tasks(
Id INT NOT NULL PRIMARY KEY AUTOINCREMENT,
Name TEXT NOT NULL,
Priority INT NOT NULL
IsSynced BOOLEAN DEFAULT false);
Is it okay to make different drift asynchronous db calls to get the records with different priority that is not synced or should I write a single drift query to get all the non synced records and separate them based on priority in code. I know it does not make much different in this scenario. Since drift is used for local storage. Should I be concerned with the number of db calls.
First
will result in an error as
AUTOINCREMENTis only allowed if the column type is specifically INTEGER. You would get a failure asAUTOINCREMENT is only allowed on an INTEGER PRIMARY KEYSecond
If you are considering efficiency then consider if you REALLY need
AUTOINCREMENT.AUTOINCREMENTconfusingly does not actually result in a unique id being generated rather it is a constraint(rules) that imposes the condition that the value, if generated, MUST be greater than any value that has existed (if it does not).The constraint/rule is afforded by an additional table sqlite_sequence which, a row per table, holds the highest ever assigned id value.
see https://www.sqlite.org/autoinc.html for a more comprehensive overview that also makes mention of the rowid which if INTEGER PRIMARY KEY is used is aliased by the column with that definition.
note the very first statement.
Regarding the number of db calls, it depends on the usage scenario. Will the single call, when not split, return a great deal of data. If so all data in a single chunk may be less efficient to handle, this versus multiple calls where the communication with the server could be the bottleneck.
You should undertake research based upon your needs.
Demonstration of
AUTOINCREMENTv justINTEGER PRIMARY KEY(and column type flexibility)Perhaps consider the following code (used in an SQLite Tool (Navicat for SQLite in this case)):-
The above results in 4 results. They are:-
Result 1
through to:-
Result 2 (i.e. very much the same table but without
AUTOINCREMENT)also the ridiculous column type that demonstrates SQLite flexibility with column types (unless STRICT is used) see https://sqlite.org/datatype3.html
through to:-
Result 3
i.e. no row for tasks2 table (so no overheads for tasks2 table)
Result 4
i.e. no issue inserting rows with values lower than the highest if providing the value (irrespective of AUTOINCREMENT)
Discussion of 1 v 2
In short there is no difference other than the priority which is based upon a random value. So as far as the 1000 rows is concerned AUTOINCREMENT or not makes no difference.
As can be see the ridiculous column name makes no real difference (in fact BOOLEAN itself is not one of the actual column affinity types, these being NULL, INTEGER, REAL, TEXT, BLOB and NUMERIC). However bar the rowid or an alias thereof (which id is in both tasks and tasks2) then a column can hold a value of any type, which is a feature that most other RDB's do not offer.
When it comes down to AUTOINCREMENT this keyword was chosen to allow SQLite to be more compatible with other RDB's.