# Log filter for slow queries?

**URL:** <https://forum.inductiveautomation.com/t/log-filter-for-slow-queries/53537>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [November 29, 2021, 4:45pm UTC](https://forum.inductiveautomation.com/t/log-filter-for-slow-queries/53537 "2021-11-29T16:45:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [November 29, 2021, 4:45pm UTC](https://forum.inductiveautomation.com/t/log-filter-for-slow-queries/53537/1 "2021-11-29T16:45:23Z")

</div>

On the gateway status page, if you click on database → Details, you get to see the most recent slow queries that executed.

I wanted to go over the past 5 or so days and see the slow queries from that time period. I assume this same data gets logged to the server logs as well, but I can’t seem to figure out what filter would help parse those out under status → logs  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/b/6bfeae2fd0ed54cc2b6a8bd0670a1b2c76d956c8.png)

How can I view a history of the slow queries on the database?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 29, 2021, 4:56pm UTC](https://forum.inductiveautomation.com/t/log-filter-for-slow-queries/53537/2 "2021-11-29T16:56:03Z")

</div>

There’s no special associated filtering key. It should be a `WARN` event from `gateway.Database.Updates` or `gateway.Database.Selects`, according to the code.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [November 29, 2021, 5:02pm UTC](https://forum.inductiveautomation.com/t/log-filter-for-slow-queries/53537/3 "2021-11-29T17:02:55Z")

</div>

Hmm I’m still having trouble then.

I know I had at least 1 slow query within the last half an hour -

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/4/b492cd4f98e1de6bc0a189aa1ccc667546fae330.png)

but setting my logs to show me WARN or higher only and I don’t see anything -

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/7/c7b5900cd8acdbc3fdb32edb173028ffae6880cb.png)

Would I be better off trying to GREP the log file directly?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 29, 2021, 6:07pm UTC](https://forum.inductiveautomation.com/t/log-filter-for-slow-queries/53537/4 "2021-11-29T18:07:56Z")

</div>

My bad, it looks like that table is populated by a different mechanism.

If you’ve already got a GatewayContext object, it’s pretty easy:  
`context.getDatasourceManager().getDatasource(name).getMetrics().getExpensiveQueries()`  
That’ll be a list of [`QueryMetric`](https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.12/com/inductiveautomation/ignition/common/datasource/QueryMetric.html) objects.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [November 29, 2021, 6:22pm UTC](https://forum.inductiveautomation.com/t/log-filter-for-slow-queries/53537/5 "2021-11-29T18:22:24Z")

</div>

Is there a way to get the `GatewayContext` object via jython scripting? Ultimately if I could put this into a function I run once a week, that would be ideal.

Also not sure what `name` is in this `context.getDatasourceManager().getDatasource(name).getMetrics().getExpensiveQueries()`

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [November 29, 2021, 6:24pm UTC](https://forum.inductiveautomation.com/t/log-filter-for-slow-queries/53537/6 "2021-11-29T18:24:06Z")

</div>

> [@bkarabinchak.psi](#):
>
> Also not sure what `name` is in this

The name of the data source connection that the query is being run against.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 29, 2021, 6:32pm UTC](https://forum.inductiveautomation.com/t/log-filter-for-slow-queries/53537/7 "2021-11-29T18:32:03Z")

</div>

The easy way to get a GatewayContext is to use `IgnitionGateway` - searching on the forum should get you the exact import path to use. `IgnitionGateway.get()` will work in all 8.X versions and return the singleton mega-class that basically _is_ the Ignition gateway. The only things you _should_ use on it are those listed in [`GatewayContext`](https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.12/com/inductiveautomation/ignition/gateway/model/GatewayContext.html).

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [November 29, 2021, 6:49pm UTC](https://forum.inductiveautomation.com/t/log-filter-for-slow-queries/53537/8 "2021-11-29T18:49:27Z")

</div>

Ok great I have it working well enough now so that I can revisit the queries and see some specifics.

For anyone wondering how to do the same thing, this is a function I call from a gateway message handler:

```auto
"""
These scripts can ONLY be run on the gateway context.
"""
from com.inductiveautomation.ignition.gateway import IgnitionGateway
import system.util

logger = system.util.getLogger("gateway.query")
LIMIT = 100

def getLongQueries():
	"""
	Note: 
	query.duration is a long, measuring milliseconds
	query.startTime is the unix-time
	"""
	context = IgnitionGateway.get()
	logger.info("Got context")
	name = 'myDatasource'
	queries = context.getDatasourceManager().getDatasource(name).getMetrics().getExpensiveQueries()
	for query in queries:
		if query.duration > LIMIT:
			logger.info("Query: " + str(query.getQuery()))
			logger.info("Length of query: " + str(query.duration))
			logger.info("Start time: " + str(query.startTime))

```
