# Node-RED freezes while sqlite query

**URL:** <https://discourse.nodered.org/t/node-red-freezes-while-sqlite-query/84675>\
**Category:** General\
**Tags:** sqlite\
**Created:** [18 January 2024 08:16 UTC](https://discourse.nodered.org/t/node-red-freezes-while-sqlite-query/84675 "2024-01-18T08:16:58Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [14 February 2024 17:27 UTC](https://discourse.nodered.org/t/node-red-freezes-while-sqlite-query/84675/21 "2024-02-14T17:27:03Z")

</div>

> [@mima232](#):
>
> Expected order should be: db node, function 16 node, debug 31 node, csv node ... right

no, it depends on what order the nodes were wired. This is not something to be concerned about usually but I can understand your curiosity having previously had a flow that was _heavy_ and so you could visualise the effects.

> [@mima232](#):
>
> Another thing Im curious about is whats the order of branches? Is it decided by order of links in flow.json?

yes. see previous response (its the same question and same answer essentially)

> [@mima232](#):
>
> Does it change when I delete the links and reattach debug node first and function 16 second

Yes. BUT. if function 16 is blocking (has no async / is a hard loop / no callbacks etc) then you might not see that debug until function 16 is done due to a combination of how the flow is ordered, how the NR core is optimised (i.e. it bundles debug messages to avoid excessive comms traffic), the state of the NodeJS event loop etc etc etc.

---

<div class="post-metadata">

**Author:** ![davidz](https://avatars.discourse-cdn.com/v4/letter/d/a88e57/32.png) [@davidz](https://discourse.nodered.org/u/davidz)\
**Post date:** [14 February 2024 18:48 UTC](https://discourse.nodered.org/t/node-red-freezes-while-sqlite-query/84675/22 "2024-02-14T18:48:17Z")

</div>

The bottleneck should be the CSV node. To confirm this, first delete the CSV node, and run the query without CSV conversion.

After finding the bottleneck, you can delete the CSV node, and write a simple CSV conversion function inside function 16 instead. This will speed up the process significantly. An example of the CSV conversion function is as the following.

```auto
function ConvertToCSV(objArray, str) {
    var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
    for (let i = 0; i < array.length; i++) {
        let line = '';
        for (var index in array[i]) {
            if (line != '') line += ','

            line += array[i][index];
        }
        str += line + '\r\n';
    }
    return str;
}

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 February 2024 21:19 UTC](https://discourse.nodered.org/t/node-red-freezes-while-sqlite-query/84675/23 "2024-02-14T21:19:40Z")

</div>

> [@davidz](#):
>
> To confirm this, first delete the CSV node

Or just remove the wire connecting the sql node to the following function node.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [17 February 2024 14:51 UTC](https://discourse.nodered.org/t/node-red-freezes-while-sqlite-query/84675/24 "2024-02-17T14:51:10Z")

</div>

> [@mima232](#):
>
> but the main problem now is probably the string conversion.

So what string conversion is taking place inside function 16? If you are trying to reformat the datetime information, that could be costly.

In general, output value formatting should take place inside the SQL statement, so you can skip that function code and send the query results directly to the CSV node in streaming mode.

> [@mima232](#):
>
> Im not really much familiar with DBs so I dont know all these tricks ...

Of course, you will have to learn some SQL syntax, but I've almost always found that databases are more efficient at processing the raw data than whatever code I write to loop over the results. And it doesn't tie up the nodejs task queue since it happens outside of node-red in the database itself.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [2 March 2024 14:51 UTC](https://discourse.nodered.org/t/node-red-freezes-while-sqlite-query/84675/25 "2024-03-02T14:51:55Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.

[Previous page](https://discourse.nodered.org/t/node-red-freezes-while-sqlite-query/84675.md?page=1)
