looping through the records slowly.

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

looping through the records slowly.

Postby ashutoslenka426 » Mon Jun 13, 2016 10:22 am

Hi All,

I have some 1.5 lakh records in db . I am looping through each record and again doing find operation to find all the duplicate records . I am doing this operation in the batch processor . This operation is running very slowly . Why ? .How can I make it to run fast ? .
Code: Select all
var XXXFs = datasources.db.YYYYY.XXXX.getFoundSet();
XXXFs.loadAllRecords(); // loading the foundset.
for(recIndex = 1 ; recIndex <= XXXFs.getSize() ; recIndex++){ /
XXXRec = XXXFs.getRecord(recIndex);
}


This is in Servoy 7.4.2 and DB is postgres .
AL
ashutoslenka426
 
Posts: 295
Joined: Thu Jan 26, 2012 3:38 pm

Re: looping through the records slowly.

Postby Ruben79 » Mon Jun 13, 2016 12:22 pm

Maybe it's an option to let the database do the work?
Code: Select all
SELECT mycolumn,
COUNT(mycolumn) AS numoccurrences
FROM mytable
GROUP BY mycolumn
HAVING ( COUNT(mycolumn) > 1 )
Ruben de Jong
Stb Software Development
SAN Partner

Stb Software Development - http://www.stb.nl
User avatar
Ruben79
 
Posts: 97
Joined: Wed Apr 18, 2007 12:43 pm

Re: looping through the records slowly.

Postby juan.cristobo » Tue Jun 14, 2016 9:44 am

It seems that forEach is very fast:
Code: Select all
var XXXFs = datasources.db.YYYYY.XXXX.getFoundSet();
XXXFs.loadAllRecords(); // loading the foundset.
XXXFs.forEach(function(rec, recIndex, foundset) {
        //every record is in rec variable
        //the current index is in recIndex variable
    }
);
Juan
Madrid (Spain)

Servoy 7.4.x - MySQL / SQL Server 2008-2016
Windows 10 Pro
juan.cristobo
 
Posts: 186
Joined: Thu Apr 19, 2012 9:12 am

Re: looping through the records slowly.

Postby ROCLASI » Tue Jun 14, 2016 11:06 am

Hi Ashutos,

Querying one record at a time to loop through your records and do a comparison is always much slower than sending just 1 query to the database to do the same thing.
So Ruben's suggestion is the fastest option.
However since Servoy requires the PK value to fetch any record you do have to add the PK in your SELECT list. That of course kills the method to find any duplicates because you have to add the PK also to the GROUP BY (and PK's are by definition unique).
So you need to rewrite that query to find the duplicates and then find the corresponding PK's like so:
Code: Select all
foundset.loadRecords("SELECT myPK \
                    FROM myTable \
                    WHERE myValueColumn IN ( \
                        SELECT myValueColumn \
                        FROM myTable \
                        GROUP BY myValueColumn \
                        HAVING count(myValueColumn) > 1 \
                    )");


Essentially a one-liner :)
Hope this helps.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium


Return to Programming with Servoy

Who is online

Users browsing this forum: Bing [Bot] and 19 guests

cron