cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
AchatesChen
Helper I
Helper I

Poor performance when patch whole collection to database

In my case (a quiz app), every time I need to submit tens or even 100 records to MySQL server, however, either using ForAll or Collect(database, collection), system is working per each row, so the speed is very slow. I even tried Flow, it also needs about 2s for each record. Is there any way can bulk insert row to database?

11 REPLIES 11
mdevaney
Super User
Super User

@AchatesChen 

Yes, you can bulk update records in SQL using the PATCH function like this.  I'll provide a link to a tutorial article at the bottom of this post. 

 

Patch(your_SQL_table, your_updates_collection)

 

The update happens faster because multiple records are being simultaneously modified at-once.   Using a FORALL loop is a slower way to PATCH because the records get changed one-at-a-time.  However, you must follow a few rules:  Any column names in your_updates_collection must match the column names in your_SQL_table exactly.  If you don't include an ID number in your_updates_collection it will insert new rows.  If you include an ID number it will edit existing rows.

 

Link to article "PATCH Multiple Records 10x Faster":
https://matthewdevaney.com/patch-multiple-records-in-power-apps-10x-faster/

 

 

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

not sure what is the difference but I will receive errors as below.

the first one is my database, the second is is the collection.

BTW, I am inserting, not updating. Any difference? I remember, in doc, when update, need to mention base records.

 

AchatesChen_0-1599571725881.png

 

@AchatesChen 

Can you please show me how you built the collection Result_Head?

See below:

AchatesChen_0-1599613919798.png

 

@AchatesChen 
For whatever reason I have found that it is sometimes necessary to define the collection schema in the OnStart property of the app like this.  Make sure to Run App OnStart after inputting this code.

ClearCollect(Result_head,
    FirstN({
        id: 0,
        qzid: 0,
        email: "A",
        name: "A",
        start_time: Now(),
        end_time: Now(),
        total_point: 0,
        total_score: 0
    }),0
);
        

 

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

Sorry but it still doesn't work. 

it says that "the specific record was not found...". Looks like it is trying to update instead of insert.

Really frustrating!😫

@AchatesChen 
If you are trying to insert new records you must remove any references to an id column in Result_head and the schema.  That’s why PowerApps thinks you want to update.

 

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

Sorry for the late response.

I think because I am using MySQL, it will show error "Field 'id' is required.".

This is known issue that Power Apps doesn't support auto incremental id field of MySQL, while MSSQL or SP should be fine.

I guess no solution for me.

Hi @mdevaney ,

 

Can this method work for all datasources? Not just for SQL? Can this be for SharePoint?

 

I actually read your blog post from your site before stumbling to this thread. Great approach. This is of interest to me as I notice slow performance with ForAll()+Patch() and looking for ways to optimize it.

Helpful resources

Announcements
PA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Welcome Super Users.jpg

Super User Season 2

Congratulations, the new Super User Season 2 for 2021 has started!

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

Top Solution Authors
Top Kudoed Authors
Users online (2,425)