Skip to content

Analytics: Database and Cache

Analytics reads live data from the order, invoice, credit memo, cart, customer and stock tables. Each report runs a few SELECT statements with GROUP BY. These settings keep the load on the database under control.

The report_read connection

Analytics reads through the connection report_read. By default, it uses the connection core_read. In app/etc/local.xml, you can give it a timeout for each statement, or point it at a read replica.

Set a statement timeout

Add the initStatements line for your database. The examples stop a statement after 30 seconds.

MySQL:

<config>
    <global>
        <resources>
            <report_read>
                <connection>
                    <use/>
                    <host>localhost</host>
                    <username>maho</username>
                    <password>secret</password>
                    <dbname>maho</dbname>
                    <initStatements>SET NAMES utf8mb4; SET SESSION max_execution_time=30000</initStatements>
                    <model>mysql4</model>
                    <type>pdo_mysql</type>
                    <active>1</active>
                </connection>
            </report_read>
        </resources>
    </global>
</config>
Database initStatements
MySQL SET SESSION max_execution_time=30000 (milliseconds)
MariaDB SET SESSION max_statement_time=30 (seconds)
PostgreSQL SET statement_timeout=30000 (milliseconds)

The empty <use/> stops the connection from using core_read, so it needs its own host, user and database. When the database stops a statement, the page shows an error and the log gets the exception.

Use a read replica

Give report_read the host of the replica, as in the example. The reports then do not load the primary database. A replica that lags shows the newest orders later.

Upgrade

  • The upgrade adds two indexes: sales_flat_order_item (product_id, created_at) and sales_flat_quote (updated_at). On a large store, ./maho migrate can take some minutes.
  • The upgrade does not enable the cache type Analytics Results. Enable it in System > Cache Management, or every report reads the database again.

A large store

A report reads every record of its period, so its time grows with the number of orders. On a store with many orders:

  • Set Daily Totals from (days) in System > Configuration > Reports > Analytics, for example to 7. An order report on a period of at least that number of days then reads the table report_order_daily, with one row for each day and each combination of its fields, and not every order. The numbers stay the same. After you set it, the cron job reports_update_daily_totals builds the history, 92 days every 5 minutes.
  • Keep the cache type Analytics Results on. A report on a past period stays in the cache for 24 hours by default, and a report that includes today for 5 minutes.
  • Point report_read at a read replica for long periods.

To see the SQL and the time of each statement of a report, run:

./maho report:explain --preset=sales_overview --range=last_12_months

Exports

An export with more rows than Rows per Page runs in the queue reports and writes a file in var/reports/export. A daily job deletes the files after 7 days. The storage mount reports_exports can point at shared storage (see Shared storage).

Report emails

The cron job reports_send_subscriptions runs each hour. It puts each report email that is due in the reports queue, which goes to the slow worker pool. So the emails need both cron and a queue worker. If the worker fails, the store does not send that email again. The message waits in System > Message Queue, where you can retry it.