portal entry

select a category, or use search below
(searches all categories and all time range)

Alternatives to Query of Query

| View in Portal
October 01, 2019 07:57:18 PM GMT
<p>CFML has had Query of Queries for years and it's been a useful tool but in modern CFML there is no need to use it at all.</p>
<p>The post <a rel="nofollow" href="https://coldfusion.adobe.com/2019/10/alternatives-query-query/">Alternatives to Query of Query</a> appeared first on <a rel="nofollow" href="https://coldfusion.adobe.com">ColdFusion</a>.</p>
Labels: Blog, ColdFusion, Language, 2018, blog, language, modern cfml


Thanks for this, i can see some uses right away.  Couple questions. Does this run faster than query of queries? Some cases required duplicating the query to do the function.  Any concerns on memory usage or is the idea you won't need to do that enough that it matters?   Thanks
Comment by Grae Desmond
2374 | October 08, 2019 08:46:00 PM GMT
Nice stuff, John (though the need to duplicate things in those couple of cases may negate the value of this over q of q). That said, you have not really said why this approach would be "better" than a q of q. That may help some readers. It may be merely that it's a more modern (functional programming) approach, sure. To that point, though, I'll add that while you refer to things like querysort, queryfilter, and querymap, then you never use them. Instead, you use the implicit "higher order" functions, appended to the objects. For readers who may want to understand more on that, see some other resources on the topic like <a href="https://coldfusion.adobe.com/2017/10/map-reduce-and-filter-functions-in-coldfusion/">https://coldfusion.adobe.com/2017/10/map-reduce-and-filter-functions-in-coldfusion/</a> and <a href="http://ryanguill.com/functional/higher-order-functions/2016/05/18/higher-order-functions.html" rel="nofollow">http://ryanguill.com/functional/higher-order-functions/2016/05/18/higher-order-functions.html</a>. Finally, you conclude with a mention of the option to use "arrow functions" also, but we should note for readers that that is only supported in CF2018 (and that as of only its update 5 from last month) but it has not yet been offered in CF2016 (even per its update 12 of last month). Lest anyone may ask: I am not aware of whether the plan is for those to be offered in CF2016 in the future. Hope that's helpful. None of it is meant as criticism. Again, thanks for the post. Just expanding on things a bit for some readers.
Comment by Charlie Arehart
2373 | October 08, 2019 09:22:07 PM GMT
The mutating of the original query is a bug (Lucee does not mutate it). The issue is raised here <a href="https://tracker.adobe.com/#/view/CF-4203366" rel="nofollow">https://tracker.adobe.com/#/view/CF-4203366</a> please vote! Is it faster? I really don't know. I tend to code for readability rather than performance and then optimize later if required - I've not noticed any issues with it. If you are concerned about performance then I would expect a database engine to be much faster than using CF to sort / filter etc as that's what they are built to do. In terms of memory usage, having to duplicate it is annoying. If you don't care about the original query being mutated then you don't have to duplicate - just make sure you have good code coverage so you don't trip up your future self when you forget about this annoying behaviour (did I mention people should vote for CF-4203366!). Charlie knows way more about the inner mysteries of ColdFusion performance than I do, but I believe that if you do the manipulation inside a function, when the function exits, the references to variables created inside that function (apart from anything returned) are available for Garbage Collection, so I don't think you'll get a memory leak. That said, if you have large datasets, then the database is the best place to be manipulating them if at all possible.
Comment by aliaspooryorik
2394 | October 09, 2019 07:39:52 AM GMT
Thanks for the clarifications Charlie 
Comment by aliaspooryorik
2397 | October 09, 2019 07:56:39 AM GMT
Thanks for the reply, that makes sense.  I went and voted and maybe the discussion here will help get it on the radar in addition to the bug tracker.
Comment by Grae Desmond
2396 | October 09, 2019 12:31:35 PM GMT
As for the objects created in a function being released for GC after the function finishes, we should clarify for readers that this would be true as long as you var scope (or use the local scope) for the variables. You did for your var q, but then that was in a function creating the query that would be duplicated. If the rest of the code (creating the dupe) were itself in some other function or method of a CFC, that is where it would be important to have var-scoped the rest. (I know you and Grae know this. I'm writing for other readers.)
Comment by Charlie Arehart
2402 | October 09, 2019 03:04:30 PM GMT
One key reason not to use Query of Queries is that, if you have to maintain your codebase on both Lucee and ACF, QoQ is very slow on Lucee and specifically recommended against using.
Comment by Benjamin Reid
2459 | October 22, 2019 11:22:47 PM GMT