Deleting Fast in ServiceNow
Another post dedicated to the die hard ServiceNow developers. Have you ever wondered how fast (or slow) the deletion methods that ServiceNow employs are? I’m talking about GlideRecord deleteRecord, GlideRecord deleteMultiple and GlideMultipleDelete execute (undocumented). Today we’re going to rank all the methods, and put some numbers behind the mystery.
Due to personal experience, I’ve noticed that some sources are prioritized in terms of deletes, so again here are numbers but with different sources overlaid on them. I ran tests with 1k, 10k, 50k, 100k, and 200k to get a fair representation.
In my personal experience again, I had a gut feeling that the workflow activities are somehow optimized for a slightly less deletion overhead - and script actions as being relatively slow.
Delete Multiple - The fastest method by order of magnitude of 10
GlideMultipleDelete execute - The second fastest method (10x slower than DeleteMutiple, but very fast)
Delete Record - There is absolutely never a good reason to use the method ever. It’s 1,270x slower than Delete Multiple. Unless you just want to waste everyone’s time. We’re talking 1s versus 24min difference for 200k deletes.
Note: The numbers shown were all run on a personal developer instance with no other processes running. Additionally I was running these delete methods with basically no filters. Filters will extremely slow down your deletion rate as well as how big the table is you’re operating on. Your numbers could vary based on node count, jobs running, active users, and the database server load and capacity as well. They could also vary depending on what else is running in your system (ie if your event log is overflowing, script actions will be slower).
Want to validate my results? Here is some sample code you can adapt.
deleteUsingGRDelete : function(){ this.startTimer(); var grTestData = new GlideRecord(this.tbl); grTestData.query(); while(grTestData.next()){ grTestData.deleteRecord(); } this.endTimer('GlideRecord deleteRecord'); }, deleteUsingGRDeleteMultiple : function(){ this.startTimer(); var grTestData = new GlideRecord(this.tbl); grTestData.query(); grTestData.deleteMultiple(); this.endTimer('GlideRecord deleteMultiple'); }, deleteUsingGlideMultiple : function(){ this.startTimer(); var md = new GlideMultipleDelete(this.tbl); md.execute(); this.endTimer('GlideMultipleDelete execute'); }, startTimer : function(){ this.start = new Date().getTime(); }, endTimer : function(test_name){ var end = new Date().getTime(); var dur = end - this.start; gs.log('Test '+this.size+' '+ test_name + ': '+ dur,this.type); var grResults = new GlideRecord('u_deletetesterresults'); grResults.initialize(); grResults.u_record_count = this.size; grResults.u_test_name = test_name; grResults.u_source = this.source; grResults.u_execution_ms = dur; grResults.u_execution_time.setDateNumericValue(dur); grResults.insert(); },
Further areas of research to consider… If there is enough interest I may do a follow up post.
Historically ServiceNow has recommended the Table Cleaner (sys_auto_flush) for mass deletions. The deletion times on that could be slightly more optimized.
Audited & Update sync table deletions could be much slower and heavier. It would be interesting to see the measured impact to deletions by adding these options.
Number of columns and column types on the table. I did mine with the base system fields as well as a string description field with size 80. If you added 2 string fields with size 4k, would it make a measurable impact to deletions? Also does null data delete faster vs filled columns or is it treated the same?
One final thought… The most optimal I measured for large data deletions was about 152 deletes/ms, or about 152,000 deletes/s on my ‘clean’ instance and simple table without filters. I would recommend running some deletion baselines in your instance to see what you can expect out of performance.