Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up Download Spreadsheet from SearchKit results #26214

Conversation

larssandergreen
Copy link
Contributor

@larssandergreen larssandergreen commented May 13, 2023

Overview

Downloading a spreadsheet from SearchKit results is often slow when you have even a thousand rows. Timeouts are common, depending on your environment, with a few thousand rows.

It turns out that almost all the execution time was being used to generate the menu column, which of course isn't included in the spreadsheet download, so doesn't need to be generated. I suspect there is also some work to be done to make that faster in general to speed up results in SearchKit (not a big deal with 50 rows, but if you increase the page size it gets slower).

Related issue.

Before

Slow! Timeouts! Can't download more than a couple thousand rows.

After

At least an order of magnitude faster, for simple rows. Timeouts much less likely.
Can download 10,000 plus rows (I was hitting memory limits while testing, rather than timeouts).

@civibot
Copy link

civibot bot commented May 13, 2023

(Standard links)

@civibot civibot bot added the master label May 13, 2023
@larssandergreen larssandergreen changed the title Speed up Download Spreadsheet from SearchKit results 20x Speed up Download Spreadsheet from SearchKit results May 14, 2023
@larssandergreen larssandergreen marked this pull request as draft May 14, 2023 05:33
@larssandergreen larssandergreen force-pushed the Improve-slow-download-from-SearchKit-results branch from 989466b to d02b388 Compare May 14, 2023 16:00
@larssandergreen larssandergreen marked this pull request as ready for review May 14, 2023 16:01
@larssandergreen
Copy link
Contributor Author

Related issue on the potential to speed up SearchKit in general by optimizing how we check access for the menu link (View / Edit / Delete), which is where a lot of the time is going.

@colemanw
Copy link
Member

I like the concept! 2 thoughts about the implementation:

  1. Unsetting array items within a foreach loop is not stable because the unset reindexes the items which haven't been iterated yet. Easiest workaround is to foreach(array_reverse(....
  2. I think the conditional checking that type is ['menu', 'buttons', 'links'] is a bit too specific. A more generic check would be if the column has a key. I think the following code could be removed from line 78 and that condition used in your new loop instead:
      if (!empty($col['key'])) {
        $columns[$index] = $col;
      }
    

@larssandergreen larssandergreen force-pushed the Improve-slow-download-from-SearchKit-results branch from d02b388 to 0f8de2a Compare May 16, 2023 02:00
@larssandergreen
Copy link
Contributor Author

I don't think unset reindexes an array in PHP (at least not since PHP 7), so that should be OK.

Yes, that makes more sense to check for the key, have updated.

@colemanw
Copy link
Member

Ok, great @larssandergreen please just add a comment on l57 to explain that non-spreadsheet columns are being unset for performance reasons.

@colemanw colemanw added the merge ready PR will be merged after a few days if there are no objections label May 16, 2023
@larssandergreen larssandergreen force-pushed the Improve-slow-download-from-SearchKit-results branch from 0f8de2a to 6950579 Compare May 16, 2023 14:19
@larssandergreen
Copy link
Contributor Author

@colemanw thanks, comment added

@colemanw colemanw merged commit a599e40 into civicrm:master May 16, 2023
@colemanw
Copy link
Member

Thanks @larssandergreen for this PR, I'm sure everyone with big spreadsheets to download will appreciate it!

@colemanw
Copy link
Member

@larssandergreen I'm curious to know what tool you used to determine which part of the code was taking the most time.

@larssandergreen larssandergreen deleted the Improve-slow-download-from-SearchKit-results branch May 24, 2023 05:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
master merge ready PR will be merged after a few days if there are no objections
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants