Is there a way to tell what index is used for a GlideRecord query?

David Hubbard
Tera Guru

We have a table with several indexes on it - in some client usage they are seeing slow performance - so, I am wondering if there is a way to tell - either directly from GlideRecord (or Meta) what indexes are used for the query? 

 

I am aware of the "Slow Queries" list,  but don't see any index available there to view.

12 REPLIES 12

Thanks - useful to know, that I wasn't missing something on showing index usage.

 

Yes, I am aware of `sys_db_index` - for Scoped Application we have to also cater for SN custom field references (e.g. a_ref_5, a_str_16) - so my script for this, which converts these, is:

 

var application = 'My App Scope';
var tableName = 'x_my_app_my_table';   // optional, can comment out

// Application (Scope)
var appr = new GlideRecord('sys_scope');
appr.addQuery('name', application);
appr.query(); 
appr.next();
appr;

var indexes = [];

var rec = new GlideRecord('sys_db_object');
rec.addQuery('sys_scope', appr.sys_id);
if (typeof tableName != 'undefined' && tableName.length) {
	rec.addQuery('name', tableName);
}
rec.orderBy('name');
rec.query();
while (rec.next()) {
	var irec = new GlideRecord('v_index_creator');
	irec.addQuery('table', rec.sys_id);
	irec.addQuery('sys_scope', appr.sys_id);
	irec.orderBy('index_name');
	irec.orderBy('cols');
	irec.query();
	while (irec.next()) {
		var idx = {};
		idx.tname = rec.getValue('name');
		idx.name = irec.getValue('index_name');
		idx.table = irec.getValue('logical_table_name');
		var colNames = irec.getValue('index_col_name');
		idx.unconvert_cols = colNames;
		var cols = [];
		var colParts = colNames.split(',');
		colParts.forEach(function(c) {
			c = c.trim();
			if (c.length && c.startsWith('a_')) {
			    var srec = new GlideRecord('sys_storage_alias');
			    srec.addQuery('storage_alias', c);
			    srec.addQuery('table_name', idx.tname);
			    srec.query();
			    if (srec.next()) {
				// storage_alias
				cols.push(srec.getValue('element_name'));
			    }
			}
			else {
				cols.push(c);
			}
		});
		idx.cparts = colParts.length;
		idx.convert_cols = cols.join();

		idx.unique = !!irec.unique_index;
		idx.method = irec.getValue('access_method');
		indexes.push(idx);
	}
}

indexes;

 

 

Bhuvan
Giga Patron

@David Hubbard 

 

If you have multiple fields indexed that you are using in Query conditions of glide record, database will optimize the query and selects the best index available. It does not log or show the information of which indexed field is used for the query.

 

Check this comprehensive article that can help you to troubleshoot slow performance,

 

https://www.servicenow.com/community/developer-blog/what-index-should-it-be-or-slow-queries-explaine...

 

https://www.servicenow.com/community/developer-forum/demystifying-gliderecord-in-servicenow-what-why...

 

If this helped to answer your query, please mark it helpful & accept the solution. 

 

Thanks,

Bhuvan

Hi.  Thanks.  Interesting article on Slow Queries - I was aware of these menu option, but hadn't looked at the details before.

The really interesting part (to me) is on the "Explain Plan".

But I don't see such a field/column in the Slow Queries list with "Explain Plan" (I realise this is coming up for 10 years old) - any ideas were that information me be found?

@David Hubbard 

 

Go to Slow Queries -> Explain Plan

Bhuvan_0-1756744714157.png

If this helped to answer your query, please mark it helpful & accept the solution. 

 

Thanks,

Bhuvan

@David Hubbard 

 

Did you get a chance to review this ?

 

If my response helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan