From 6df0693804de6a8b579039f175c7f4072bffef19 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sat, 2 Nov 2024 13:47:16 -0500 Subject: [PATCH] Add a few more notes to the latest TIL --- ...er-select-all-over-execute-for-read-queries.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/rails/prefer-select-all-over-execute-for-read-queries.md b/rails/prefer-select-all-over-execute-for-read-queries.md index 8822083..a7605c5 100644 --- a/rails/prefer-select-all-over-execute-for-read-queries.md +++ b/rails/prefer-select-all-over-execute-for-read-queries.md @@ -35,10 +35,19 @@ result = ActiveRecord::Base.connection.select_all(books_by_status_query) ``` It has the advantage of semantically communicating that it's just a read and -won't have any side-effects. +won't have any side-effects. It also avoids an unnecessary clear of the query +cache. -> Note: the query is assumed to have side effects and the query cache will be -> cleared. If the query is read-only, consider using select_all instead. +> Note: [when execute is used] the query is assumed to have side effects and +> the query cache will be cleared. If the query is read-only, consider using +> select_all instead. + +The `#execute` method also has been known to leak memory with some database +connectors. + +> Note: depending on your database connector, the result returned by this +> method may be manually memory managed. Consider using exec_query wrapper +> instead. We can then iterate through and transform the results just as we would have done with `#execute`.