web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Performance issue: how...
Power Apps
Unanswered

Performance issue: how to improve complex query?

(1) ShareShare
ReportReport
Posted on by 35

Hi PowerApps colleagues!

 

I'm having troubles with the following case:

- I have an app that contains knowledge articles.

- The app is based on Azure SQL DB as data source. The data source contains 2 tables: Articles & Article_Likes (sub-table containing all the people who liked the articles.)

- A user can check the details of an article. On the detailed screen there is a count of all the amount of likes for this article,  the like/unlike functionality and a button to navigate to a screen where you can see all users who have like the article.

- The like/unlike functionality checks if you as a user already liked the article. If yes, the description is 'unlike', if no, then 'like'

- The query behind this button is shared below and this causes the biggest issue: performance. 

- The performance issues are on multiple levels: when you load the article screen, it takes too long to load the value of the count field, and the logic to check if you already liked the article or not. As a result a user might think he/she didn't like it yet and press the button.

- Another issue is when you actually press the like/unline button. The series of checks & patches/deletes that occur causes confusion at the end user who thinks that it didn't work and presses again and again...

 

So basically my question (and hopefully I'm doing it terribly wrong here): what is wrong in the below 'onselect' statement which causes this performance issues?

 

Thanks!

 

If(
First(varLikeButton).Value = true, 
Remove( 
'[dbo].[article_Likes]’, 
First(
Filter(
'[dbo].[article_Likes]’, 
ArticleNumberID = ElementRecord2.ID && Person = Label3.Text
)
)
),
Patch(
'[dbo].[article_Likes]’, 
Defaults(
'[dbo].[article_Likes]’
), 
{
ArticleDate: ElementRecord2.Created_x0020_Date, 
ArticleNumberID: ElementRecord2.ID, 
Person: User().Email
}
)
);
If(
 First(varLikeButton).Value = true,
 Patch(
 '[dbo].[Sheet1$]’, 
 LookUp(
 '[dbo].[Sheet1$]’, 
 ID = ElementRecord2.ID), 
  {
 eLikeCount: If(
 First(varOldCount).Value - 1 < 0, 
 0, 
 First(varOldCount).Value - 1)
 }
 ),
 Patch(
 '[dbo].[Sheet1$]’, 
 LookUp(
 '[dbo].[Sheet1$]’, 
 ID = ElementRecord2.ID
 ), 
 {
 eLikeCount: First(varOldCount).Value + 1
 }
 )
 );
 If(
 First(varLikeButton).Value = true,
 ClearCollect(
 varOldCount, 
 First(varOldCount).Value - 1
 ), 
 ClearCollect(
 varOldCount, 
 First(varOldCount).Value + 1
 )
 );
Refresh('[dbo].[Sheet1$]’);
Refresh('[dbo].[article_Likes]’);
ClearCollect(varLikeButton, !First(varLikeButton).Value)
Categories:
I have the same question (0)
  • Meneghino Profile Picture
    6,949 on at

    Hi @GertVonck, you pose an excellent question that has been poorly addressed in the documentation. I believe this is because PowerApps is being marketed as a no-coding app, so technical issues are brushed under the carpet.  Fortunately this seems to be improving, and issues such as the treatement of null values are now inevitably being addressed.

     

    I will address some of the issues with your code in separate posts, starting with this

     

    1) You use the Defaults() function.  The function will be called remotely at every instance.  Since the defaults of a table should never change during a session, I normally load the defaults into a global record type variable only once using the OnStart property of the home screen:

    Set(TableXDefaults, Defaults('[dbo].[TableX]'))

    In fact, if your dafaults are trivial (i.e. null values), then you don't even need to call the function or use a variable, you just do this:

    Patch('[dbo].[MyTable]', {ID: Blank()}, {MyColumn: 123, MyOtherColumn: "Hello world"})

    Just make sure that the second parameter includes the primary key columns

     

     

     

     

  • Meneghino Profile Picture
    6,949 on at

    2) You use the User().Email function.  This will also make a remote call every time it is used.  Since the user does not change during a session by definition, then I load this into a global variable only once in the OnStart property of the home screen, hence:

    Set(CurrentUser, User())

    This creates a record type variable, so you can refer to CurrentUser.Email elsewhere in your code

  • Meneghino Profile Picture
    6,949 on at

    3) You have this code, which again makes a remote call every time

    Patch(
     '[dbo].[Sheet1$]’, 
     LookUp(
     '[dbo].[Sheet1$]’, 
     ID = ElementRecord2.ID
     ),
    etc. etc.

    Again, it is not your fault but due to the paucity of the documentation.

     

    All you need to identify a record in the second parameter of a Patch function is the value of the primary key column(s).

     

    This is really important, so that is why I put it in bold for posterity.

     

    In your specific case it seems that you only have a simple ID column as primary key, hence you can use this code instead:

     

    Patch(
     '[dbo].[Sheet1$]’, 
     {ID: ElementRecord2.ID},
    etc. etc.

    This will work also in the case of composite keys (i.e. when two or more columns participate in the primary key definition)

  • Meneghino Profile Picture
    6,949 on at

    4) I believe that the Refreshes at the end are superfluous.  Any controls dependent on the tables being updated should also automatically be updated in the background by PowerApps once the patch is executed, without the explicit need for a refresh call.  This will just make two extra remote calls that will add to execution time.

    I am less sure of this than the previous three issues, so please let me have your feedback on this.

     

    There is one caveat, currently there are a few bugs in the Azure SQL DB connector.  One of these bugs means that any updates to a date type column will not be automatically refreshed in the PowerApps controls, so if your code includes updates to a date type column, then you need the explicit Refresh.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 392 Most Valuable Professional

#2
11manish Profile Picture

11manish 181 Super User 2026 Season 2

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 112 Super User 2026 Season 2

Last 30 days Overall leaderboard