Optimize fix script to run more efficiently

mkader
Kilo Guru

Hello,

I have a fix script that copies records from one table and inserts to another table. There are over 500,000 records being copied over. The fix script works fine, however, when I run the query it takes a very long time to run and will often timeout. In order for me to fully execute the fix script, I must run it several times. This is just a one off so we want to do this using a fix script. Below is the script I have written. 

var gr = new GlideRecord('u_table_1');
gr.query();
while (gr.next()){
    var gr1 = new GlideRecord('u_table_2');
    gr1.addQuery('u_description',gr.u_description);
    gr1.addQuery('u_short_description',gr.u_short_description);
    gr1.addQuery('u_name',gr.u_name);
    gr1.query();
    if(gr1.next())
{
//do nothing
}
//if not found then insert
else{
    gr1.initialize();
    gr1.u_description = gr.u_description;
    gr1.u_short_description = gr.u_short_description;
    gr1.u_name= gr.u_name;
    gr1.insert();
}
}

Is there maybe a way to segment the fix script so that it runs and completes much quicker?

Thanks!

1 ACCEPTED SOLUTION

Try this. 

var gr = new GlideRecord('u_table_1');
gr.query();

while (gr.next()){
    var gr1 = new GlideRecord('u_table_2');
    gr1.addQuery('u_description',gr.u_description);
    gr1.addQuery('u_short_description',gr.u_short_description);
    gr1.addQuery('u_name',gr.u_name);
    gr1.setLimit(1);
    gr1.query();

    if(!gr1.hasNext()){
        gr1.initialize();
        gr1.u_description = gr.u_description;
        gr1.u_short_description = gr.u_short_description;
        gr1.u_name= gr.u_name;
        gr1.setWorkflow(false); //stop execution of Business rule or other scripts.
        gr1.insert();
    }
}

If it stops in 5 mins then try running in background script then check logs if it throws any other issues. Also, can you confirm - both the tables are in same scope? 

Regards,
Muhammad

View solution in original post

27 REPLIES 27

Sudhanshu Talw1
Tera Guru

runScript();

function runScript(){

var gr = new GlideRecord('u_table_1');
gr.query();
while (gr.next()){
var gr1 = new GlideRecord('u_table_2');
gr1.addQuery('u_description',gr.u_description);
gr1.addQuery('u_short_description',gr.u_short_description);
gr1.addQuery('u_name',gr.u_name);
gr1.query();
if(!gr1.next())
{

gr1.initialize();
gr1.u_description = gr.u_description;
gr1.u_short_description = gr.u_short_description;
gr1.u_name= gr.u_name;
gr1.insert();
}
}

}

If the records being copied are in lakhs just do one thing,

Run fix script=>Process the script in background taking limit of 5k-10k.

& if you want to copy why are you looking up?

runScript();

function runScript(){

var gr = new GlideRecord('u_table_1');

gr.setLimit(5000);
gr.query();
while (gr.next()){
//var gr1 = new GlideRecord('u_table_2');
//gr1.addQuery('u_description',gr.u_description);
//gr1.addQuery('u_short_description',gr.u_short_description);
//gr1.addQuery('u_name',gr.u_name);
//gr1.query();
//if(!gr1.next())
//{

gr1.initialize();
gr1.u_description = gr.u_description;
gr1.u_short_description = gr.u_short_description;
gr1.u_name= gr.u_name;
gr1.insert();
}
//}

}

Fabian Kunzke
Kilo Sage
Kilo Sage

Hi,

 

I don't think you can do this faster. 1 Thing tho: You can run the fix script in the background instead. This does not lock your session and also does not get interrupted by the transaction timer.

Regards
Fabian

Correction: You can improve runtime. But that will take more time than your current script running in the background.