# Query each slow (Lucee 6.2.)

**URL:** <https://dev.lucee.org/t/query-each-slow-lucee-6-2/15241>\
**Category:** support\
**Created:** [July 4, 2025, 11:19am UTC](https://dev.lucee.org/t/query-each-slow-lucee-6-2/15241 "2025-07-04T11:19:38Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaeldiederich](https://dev.lucee.org/user_avatar/dev.lucee.org/michaeldiederich/32/8640_2.png) [@michaeldiederich](https://dev.lucee.org/u/michaeldiederich)\
**Post date:** [July 4, 2025, 11:19am UTC](https://dev.lucee.org/t/query-each-slow-lucee-6-2/15241/1 "2025-07-04T11:19:38Z")

</div>

I upgraded my local stack to lucee 6.2.2 / jdk 21 / tomcat 11.

I have a small script, uploading a csv file (6 columns), parse each data field (e.g. validate) and then i collect the rows in an array. If the array length is 100, I create one “INSERT INTO” sql statement with 100 rows.  
The file has 363 rows, really nothing special and this works within less seconds.

After the upgrade, the script ran in a timeout and the Query each-function is terrible slow:

 ![insert_100](https://dev.lucee.org/uploads/default/original/2X/4/462a43ac1b59c8cf76e33a55388cc4719b6353d7.png)

The Lucee web admin also shows a lot of implicit variable access - not sure if this is related:

 ![implizit_variable](https://dev.lucee.org/uploads/default/original/2X/e/ecfee45d9d0c3ae0ae325b8b7f80b6839a1615fe.png)

I switched from 100 data rows to 10 rows, makes it much faster:

 ![insert_10](https://dev.lucee.org/uploads/default/original/2X/2/2741415d71648e7816c2b5566dcdcf30761a51a1.png)

This feels like a bug?

---

<div class="post-metadata">

**Author:** ![michaeldiederich](https://dev.lucee.org/user_avatar/dev.lucee.org/michaeldiederich/32/8640_2.png) [@michaeldiederich](https://dev.lucee.org/u/michaeldiederich)\
**Post date:** [July 4, 2025, 12:04pm UTC](https://dev.lucee.org/t/query-each-slow-lucee-6-2/15241/2 "2025-07-04T12:04:32Z")

</div>

Here’s a small test case that reproduces the bug.  
To get it running, create the test table (first comment) and change the datasource (line 14). I hope this helps.

[query each slow.cfm](https://dev.lucee.org/uploads/short-url/9Suj2Pzsm4Sekhwqw3pIbN7iIqz.cfm) (2.4 KB)

---

<div class="post-metadata">

**Author:** ![michaeldiederich](https://dev.lucee.org/user_avatar/dev.lucee.org/michaeldiederich/32/8640_2.png) [@michaeldiederich](https://dev.lucee.org/u/michaeldiederich)\
**Post date:** [July 4, 2025, 1:20pm UTC](https://dev.lucee.org/t/query-each-slow-lucee-6-2/15241/3 "2025-07-04T13:20:48Z")

</div>

One more update: Lucee 6.1.1.118 on old hardware, same csv file as in my first posting:

 ![CleanShot 2025-07-04 at 15.17.17@2x](https://dev.lucee.org/uploads/default/original/2X/2/27bd2cf01942f5727ea5157235b5e4106335793c.png)

---

<div class="post-metadata">

**Author:** ![Zackster](https://dev.lucee.org/user_avatar/dev.lucee.org/zackster/32/3484_2.png) [@Zackster](https://dev.lucee.org/u/Zackster)\
**Post date:** [July 4, 2025, 3:26pm UTC](https://dev.lucee.org/t/query-each-slow-lucee-6-2/15241/4 "2025-07-04T15:26:03Z")

</div>

Does it happen with CF loop too?

I’m on my phone at the beach, you’re using queryExecute based on the helperBase.cfc, plain old cfquery should be fine tho

Obviously this seems to be a regression

---

<div class="post-metadata">

**Author:** ![dawesi](https://dev.lucee.org/user_avatar/dev.lucee.org/dawesi/32/122_2.png) [@dawesi](https://dev.lucee.org/u/dawesi)\
**Post date:** [July 5, 2025, 11:03am UTC](https://dev.lucee.org/t/query-each-slow-lucee-6-2/15241/5 "2025-07-05T11:03:27Z")

</div>

are you using the same db driver version?

---

<div class="post-metadata">

**Author:** ![michaeldiederich](https://dev.lucee.org/user_avatar/dev.lucee.org/michaeldiederich/32/8640_2.png) [@michaeldiederich](https://dev.lucee.org/u/michaeldiederich)\
**Post date:** [July 5, 2025, 8:28pm UTC](https://dev.lucee.org/t/query-each-slow-lucee-6-2/15241/6 "2025-07-05T20:28:27Z")

</div>

I tested the latest 5 mysql drivers, same results.

@Zackster I am unsure, what I should test. So when you are back from vacation, please give me some more details.

I tried several versions (lucee, tomcat) and this version is extremly faster:

Lucee: 6.1.1.118  
Tomcat: 9.0.105  
Java: 21.0.7

With tomat 9 and luce 6.2.1.122 it is slow. I hope this helps.

---

<div class="post-metadata">

**Author:** ![Zackster](https://dev.lucee.org/user_avatar/dev.lucee.org/zackster/32/3484_2.png) [@Zackster](https://dev.lucee.org/u/Zackster)\
**Post date:** [July 7, 2025, 1:39pm UTC](https://dev.lucee.org/t/query-each-slow-lucee-6-2/15241/7 "2025-07-07T13:39:05Z")

</div>

Ok, I’m back

Firstly, using `Query()` is the slowest way to do queries in Lucee

> **[Query Execution in Lucee :: Lucee Documentation](https://docs.lucee.org/recipes/query-handling.html)**
>
> Complete guide to executing database queries in Lucee with performance comparisons and best practices

This introduced some extra overhead and was missing some variable scoping, plus it’s revalidating all the params each time a param is added  
[https://luceeserver.atlassian.net/browse/LDEV-5050](https://luceeserver.atlassian.net/browse/LDEV-5050)

I’m tracking this with this ticket

[https://luceeserver.atlassian.net/browse/LDEV-5689](https://luceeserver.atlassian.net/browse/LDEV-5689)

Generally speaking `Query()` should be avoided if you care about performance

```auto
    qNew = new Query();
    qNew.setSQL( stringBufferOutput.ToString());
    qNew.setDatasource( datasource);

    for ( stParam in aParams) {
        qNew.addParam( argumentCollection = stParam);
    }
    qNew.execute().getResult();

```

438ms 6.1.1.118  
493ms - 6.2.2.68  
484ms - 6.2.2.69 (with some simple var scoping, better fix to come)

Compared to good old cfquery…

```auto
cfquery(sql=stringBufferOutput.ToString(), datasource=datasource, params=aParams );

```

23ms

This running tests vis script runner without tomcat, using java 21.0.6.7

---

<div class="post-metadata">

**Author:** ![michaeldiederich](https://dev.lucee.org/user_avatar/dev.lucee.org/michaeldiederich/32/8640_2.png) [@michaeldiederich](https://dev.lucee.org/u/michaeldiederich)\
**Post date:** [July 7, 2025, 2:36pm UTC](https://dev.lucee.org/t/query-each-slow-lucee-6-2/15241/8 "2025-07-07T14:36:05Z")

</div>

Thank you very much for your reply. Is there a chance you will merge the logic in the backend or do I have to rewrite the code? We have currently 888 “new Query” in the code…  
Are there any other deprecated functions I should know about? I just know this page: [Deprecated Tags & Functions :: Lucee Documentation](https://docs.lucee.org/reference/deprecated.html)

---

<div class="post-metadata">

**Author:** ![Zackster](https://dev.lucee.org/user_avatar/dev.lucee.org/zackster/32/3484_2.png) [@Zackster](https://dev.lucee.org/u/Zackster)\
**Post date:** [July 7, 2025, 2:58pm UTC](https://dev.lucee.org/t/query-each-slow-lucee-6-2/15241/9 "2025-07-07T14:58:04Z")

</div>

We will be addressing that perf regression, but it will never be as fast as plain old cfquery, especially when working with queries with a lot params, so refactoring is defiantly going to potentially bring performance benefits, depending on your usage

Adobe did deprecate all the `new Query()` stuff we haven’t as of yet, it’s just slower, but some people prefer the more expressive approach

As for other things, nothing springs to mind, using stringbuilder like that, especially with newer versions of java, might not be worth the effort, but that’s something to benchmark yourself

---

<div class="post-metadata">

**Author:** ![Zackster](https://dev.lucee.org/user_avatar/dev.lucee.org/zackster/32/3484_2.png) [@Zackster](https://dev.lucee.org/u/Zackster)\
**Post date:** [July 8, 2025, 2:17pm UTC](https://dev.lucee.org/t/query-each-slow-lucee-6-2/15241/10 "2025-07-08T14:17:28Z")

</div>

`6.2.2.72-SNAPSHOT` has the additional performance tweak

with a test case with 500 params, it now takes `232ms`, rather than `345ms`

The latest Lucee snapshots aren’t currently available via the admin due to some maven problems, you can just drop this into `/deploy` to test [https://cdn.lucee.org/6.2.2.72-SNAPSHOT.lco](https://cdn.lucee.org/6.2.2.72-SNAPSHOT.lco)

---

<div class="post-metadata">

**Author:** ![kenricashe](https://dev.lucee.org/user_avatar/dev.lucee.org/kenricashe/32/3476_2.png) [@kenricashe](https://dev.lucee.org/u/kenricashe)\
**Post date:** [July 10, 2025, 3:08am UTC](https://dev.lucee.org/t/query-each-slow-lucee-6-2/15241/11 "2025-07-10T03:08:51Z")

</div>

> [@Zackster](#):
>
> it will never be as fast as plain old cfquery

Or `QueryExecute()`?

---

<div class="post-metadata">

**Author:** ![Zackster](https://dev.lucee.org/user_avatar/dev.lucee.org/zackster/32/3484_2.png) [@Zackster](https://dev.lucee.org/u/Zackster)\
**Post date:** [July 10, 2025, 9:29am UTC](https://dev.lucee.org/t/query-each-slow-lucee-6-2/15241/12 "2025-07-10T09:29:15Z")

</div>

As per the documentation, `cfquery` is the fastest

> **[Query Execution in Lucee :: Lucee Documentation](https://docs.lucee.org/recipes/query-handling.html#the-three-query-dialects)**
>
> Complete guide to executing database queries in Lucee with performance comparisons and best practices

---

<div class="post-metadata">

**Author:** ![kenricashe](https://dev.lucee.org/user_avatar/dev.lucee.org/kenricashe/32/3476_2.png) [@kenricashe](https://dev.lucee.org/u/kenricashe)\
**Post date:** [July 16, 2025, 7:41pm UTC](https://dev.lucee.org/t/query-each-slow-lucee-6-2/15241/13 "2025-07-16T19:41:52Z")

</div>

Ah, I see. Well, glad to see that `QueryExecute()` has near-identical performance because cfscript is so much easier to read, especially when the `<cfquery>` has tons of ugly `<cfqueryparam>` tags, which every secure query should, of course.

Also, the `QueryExecute()` code example in that doc page is more verbose than it needs to be, and oddly uses positional parameter binding instead of named parameters which are much easier to read.

---

<div class="post-metadata">

**Author:** ![Zackster](https://dev.lucee.org/user_avatar/dev.lucee.org/zackster/32/3484_2.png) [@Zackster](https://dev.lucee.org/u/Zackster)\
**Post date:** [July 16, 2025, 8:08pm UTC](https://dev.lucee.org/t/query-each-slow-lucee-6-2/15241/14 "2025-07-16T20:08:22Z")

</div>

Wanna dive in and improve the example, or add another?

---

<div class="post-metadata">

**Author:** ![kenricashe](https://dev.lucee.org/user_avatar/dev.lucee.org/kenricashe/32/3476_2.png) [@kenricashe](https://dev.lucee.org/u/kenricashe)\
**Post date:** [July 26, 2025, 1:57am UTC](https://dev.lucee.org/t/query-each-slow-lucee-6-2/15241/15 "2025-07-26T01:57:52Z")

</div>

Done!

---

<div class="post-metadata">

**Author:** ![Zackster](https://dev.lucee.org/user_avatar/dev.lucee.org/zackster/32/3484_2.png) [@Zackster](https://dev.lucee.org/u/Zackster)\
**Post date:** [July 28, 2025, 2:46pm UTC](https://dev.lucee.org/t/query-each-slow-lucee-6-2/15241/16 "2025-07-28T14:46:26Z")

</div>

merged, thanks!
