Prepared statements versus dynamic queries #3119
Replies: 1 comment 4 replies
-
Statements are not currently cached by drift unless you enable the I'm not sure I fully understand the question though - is the idea to define the basic shape of a query in
Yeah they would benefit from caches if the option is enabled. I'm not sure if the overhead of resolving additional columns is that bad with sqlite3 though? If a network roundtrip is involved it's obvious that shaving off unnecessary columns is beneficial, but here you'll probably only save a few allocations. That can make a difference, but it's unlikely that it matters too much. |
Beta Was this translation helpful? Give feedback.
-
In my app, the same queries run pretty often. As a simple example, let's say I have a table
cars
which has 20 columns, and it needs to be queried fairly often (say, every time the user opens a specific screen).Now as it turns out, not all users need all columns. Maybe a user
salesman
only needs thelicenseplate
,price
andcolor
, whereas usermechanic
needslicenseplate
,last_serviced
andnoise_level
.In my current application, I have queries defined in
.drift
files, which just get all the columns. In Dart, I then use only the values relevant for the current user. E.g.Moreover, I do this multiple times: aside from
cars
, there is also a tableairplanes
and a tablemotorcycles
, all with certain columns relevant for certain users. What I do now is I query each of the tables once (in reality there are many more columns, and these queries already have some joins etc. present), getting all columns, and filtering for the specific user once in Dart code.Now for my question: would it be sensible to build the SQL query dynamically (at least per user session) in Dart code instead and run the query to retrieve exactly the values relevant for each user? This way, it would be possible to get a query like:
Which is nice because it only retrieves the values that are actually required for the current user. Of course this is a bit error prone and might unexpectedly break when I update my data model.
I am wondering, though, if there are other (dis)advantages to this approach. For the same user (at least during a given session) the same query will be called each time (though possibly with different parameters), so instinctively I'd expect it to be possble for them to be cached or remembered as prepared statements (which I think Drift is using for my 'normal' queries defined in
.drift
files), but is this possible?Beta Was this translation helpful? Give feedback.
All reactions