Best performances between pluck and ActiveRecord::Base.connection.execute

Hello,

I must create a very big products search on my website.
For these search, there are around a hundred variables.
Yes, It’s a lot !

I don’t know if I have to use pluck or ActiveRecord::Base.connection.execute to have the best performance.

I create a performance test with AI in develop environment.

This is the result :

With pluck
{
“description”: “Fonction Fast (10 000 itérations)”,
“timestamp”: “2026-04-28T15:32:52+02:00”,
“iterations”: 10000,
“mean_time_ms”: 2.5226,
“median_time_ms”: 2.5358,
“top_30_slowest_ms”: [
38.9619,
9.439,
9.3257,
8.7979,
8.7481,
8.6839,
8.58,
8.5528,
8.4951,
8.4839,
8.3041,
8.2378,
8.137,
8.0898,
8.055,
8.0152,
7.936,
7.684,
7.5908,
7.1769,
6.593,
6.592,
6.5494,
6.4111,
6.3999,
6.3608,
6.305,
6.2869,
6.217,
6.2032
]
}

With ActiveRecord::Base.connection.execute
{
“description”: “Fonction Slow (10 000 itérations)”,
“timestamp”: “2026-04-28T15:33:05+02:00”,
“iterations”: 10000,
“mean_time_ms”: 1.2791,
“median_time_ms”: 1.1309,
“top_30_slowest_ms”: [
41.744,
40.4241,
14.648,
14.3962,
13.1261,
7.9329,
7.221,
7.0429,
7.02,
6.9571,
6.6752,
6.6509,
6.53,
6.4342,
6.424,
6.1779,
6.134,
5.9302,
5.4102,
5.4021,
5.3151,
5.1999,
5.1541,
5.0938,
4.5462,
4.5257,
4.5109,
4.463,
4.3521,
4.2942
]
}

I don’t know what is the important thing. Is it the more long time or the mean and median?

I don’t understand the difference with the first result and the last result of every array “top_30_slowest_ms”.

I hope my english is correct.

Thank you very much for your help!

The mean/median tell you typical latency; the top_30_slowest_ms is the tail—occasional GC, IO, or DB scheduling spikes. For a web search, tail latency (p95/p99) usually matters more, so record percentiles over many runs. The execute version looks faster mainly because it skips ActiveRecord’s result processing/type casting, but you lose that convenience and must bind parameters carefully. Both should send the same SQL, so indexes, query shape, and the hundred conditions will dominate. Measure with realistic data and limit/cache results; don’t choose based only on a dev microbenchmark.