Skip to content

fix(ui): cache total count once across CSV export pagination (#40232)#40832

Open
lbajsarowicz wants to merge 1 commit into
magento:2.4-developfrom
lbajsarowicz:fix/40232-csv-export-n1
Open

fix(ui): cache total count once across CSV export pagination (#40232)#40832
lbajsarowicz wants to merge 1 commit into
magento:2.4-developfrom
lbajsarowicz:fix/40232-csv-export-n1

Conversation

@lbajsarowicz
Copy link
Copy Markdown
Contributor

Description

ConvertToCsv::getCsvFile() called $searchResult->setTotalCount($totalCount)
on the search result after getItems(). DataProvider::getSearchResult()
delegates to Reporting::search() which builds a fresh collection per call,
so the cached count was discarded between iterations and each page paid
another getSelectCountSql() round trip. With pageSize=1 this scales to
N count queries for an N-row export.

Computed total count once before the loop, reused the first search result
for iteration 1, and seeded the cached total on every subsequent fresh
result via setTotalCount() so downstream getTotalCount() callers skip
the redundant aggregate query.

Fixes #40232

Fixed Issues

Manual testing scenarios

  1. Place 10 orders.
  2. Temporarily reduce $pageSize in Magento\Ui\Model\Export\ConvertToCsv::__construct to 1 to amplify the issue.
  3. Add SLEEP(1) to the order grid count query (or profile with Xdebug/SPX).
  4. Export Sales > Orders as CSV.
  5. After this fix: a single getSelectCountSql call. Before: one per page.

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All automated tests passed successfully (18 unit tests pass)
  • Changes to the codebase comply with technical guidelines

ConvertToCsv::getCsvFile() called \$searchResult->setTotalCount(\$totalCount)
on the search result *after* getItems(). DataProvider::getSearchResult()
delegates to Reporting::search() which builds a fresh collection per call,
so the cached count was discarded between iterations and each page paid
another getSelectCountSql() round trip. With pageSize=1 this scales to
N count queries for an N-row export.

Compute total count once before the loop, reuse the first search result
for iteration 1, and seed the cached total on every subsequent fresh
result via setTotalCount() so downstream getTotalCount() callers skip
the redundant aggregate query.

Fixes magento#40232
@m2-assistant
Copy link
Copy Markdown

m2-assistant Bot commented May 25, 2026

Hi @lbajsarowicz. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.
❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

@lbajsarowicz
Copy link
Copy Markdown
Contributor Author

@magento run all tests

@lbajsarowicz lbajsarowicz changed the title fix(ui): cache total count once across CSV export pagination fix(ui): cache total count once across CSV export pagination (#40232) May 25, 2026
@engcom-Hotel engcom-Hotel added the Priority: P3 May be fixed according to the position in the backlog. label May 26, 2026
@github-project-automation github-project-automation Bot moved this to Pending Review in Pull Requests Dashboard May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Priority: P3 May be fixed according to the position in the backlog. Progress: pending review

Projects

Status: Pending Review

Development

Successfully merging this pull request may close these issues.

Slow performance when exporting CSV from the UI listing component with large dataset

2 participants