From 20cc3a9cf362fbc9f5a1adfa428916d092baaefc Mon Sep 17 00:00:00 2001 From: johnnyq Date: Fri, 13 Nov 2026 12:18:21 -0500 Subject: [PATCH] Speed up the dashboard's Hardware not on assets count and the invoices list's hardware filter: productHardwarePending and the hardware filter in agent/invoices.php had a COUNT(*) subquery a line on assets by the line's invoice and line, and where nearly every asset has asset_invoice_id 0 the index statistics say a lookup finds every asset, so the database read the whole assets table once a hardware line, 1.89 seconds and 3.1 million rows on a forum install and 22.9 seconds and 185 million rows on a test install with 25,000 assets none from an invoice; both now count each line's assets once and join them through the new productHardwareMadeJoinSql, a LEFT JOIN onto invoice_items of assets with asset_invoice_id > 0 grouped by invoice and line, a range the database measures on the index itself, with productHardwareShortSql comparing against hardware_made.made; run with none, 3,000 and 21,000 of 25,000 assets made from an invoice, the same counts as before each time and 18 to 32 ms; the lookups by one invoice give the database a constant and are unchanged; add both to the 26.11.8 changelog section --- CHANGELOG.md | 2 ++ agent/invoices.php | 2 +- functions/products.php | 45 +++++++++++++++++++++++++++++++----------- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a7d36129..7d6733ca1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ Update from Maintenance > Update — Queue Update hands the job to cron and it a - SLAs: the message after saving an SLA with an apostrophe in its name showed a backslash in it. - Contracts: a contract's response times - on its page, in its window and in the document the client signs - were worded in plain time, where SLA clocks count only your business hours. With a 9 to 5 day, a 48-hour resolution target read "2 days" and a 24-hour one "24 hrs", where the clock gives 6 and 3 business days. They now read as the clock counts them - "4 business hours", "6 business days" - the same as Admin > SLAs and the emails to clients. A contract already signed, or out for signature, keeps the wording it was sent with and is not marked as changed. Open the signed copy of any contract whose targets run to a business day or more: if it promises more than you meant, set the SLA's targets to what you meant in Admin > SLAs, or send the contract to be signed again. - Contracts: because the wording now follows your business hours, changing them changes what a target of a business day or more comes to. A contract signed from this release on is then marked as changed since it was signed, as it is when its SLA's targets are changed. +- Dashboard: it could take seconds to open - 2 seconds on one install, over 20 on a test one - on an ITFlow with many assets that weren't made from an invoice, which is most of them. The Hardware not on assets count was to blame, and the invoices list it opens, with Hardware not on assets as its filter, was as slow. Both are back to a few hundredths of a second, and every number is the same as before. ### Developer Updates @@ -71,6 +72,7 @@ Update from Maintenance > Update — Queue Update hands the job to cron and it a - `slaMinutesText()` (`functions/contract.php`), which words a target for `contractSlaTargets()` and the contract document, is now `formatSlaMinutes($minutes, true)`. `formatSlaMinutes()` gains `$exact`, which drops no remainder ("1 business day 7 hours 30 minutes"): a contract and Admin > SLAs say a target in full, and the emails keep the rounder wording, as the deadline is next to it there. - The old wording stays behind `$old_wording` on `slaMinutesText()`, passed along by `contractSlaTargets()`, `contractSlaTableHtml()`, `contractPlaceholderValues()`, `contractFillTerms()` and `contractDocumentHtml()`, and only `contractDocumentIs($row, $as_of, $hash)` asks for it. `contractSignatureCheckChanged()` and `contractSignatureOutdated()` compare a round's `contract_signature_document_hash` through it - the document in today's wording, then in the old - so a round sent up to 26.11.7 is not withdrawn or marked outdated by the rewording alone. A document's fingerprint now depends on the business days and hours wherever a target is a business day or more. - Hooks for tests and scripts: `data-sla-view` on each view's link, `data-sla-list` with `data-sla-id` on each row, `data-sla-default-for`, `data-sla-archive` and `data-sla-restore`; `data-sla-days` with `data-sla-day` (the date) on each row; `data-sla-new`, `data-sla-new-day` and `data-sla-us-holidays`; `data-sla-default` (the priority) on each default's select, `data-sla-closures`, `data-sla-alerts-stopped` and `data-sla-now`. +- `productHardwarePending()` and the invoices list's `hardware` filter count each line's assets once and join them, through the new `productHardwareMadeJoinSql()` (a `LEFT JOIN` onto `invoice_items` of assets with `asset_invoice_id > 0`, grouped by invoice and line), with `productHardwareShortSql()` now comparing against `hardware_made.made`. Both had a `COUNT(*)` subquery a line, `WHERE asset_invoice_id = AND asset_invoice_item_id = `. With a value that changes from row to row the database goes by the index's statistics, and where nearly every asset has `asset_invoice_id` 0 they say a lookup finds every asset - so it read the whole assets table once a hardware line. On a test install with 25,000 assets, none from an invoice, and 21,000 hardware lines, the dashboard's count took 22.9 seconds and read 185 million rows; it now takes 21 ms and reads 80,000. `asset_invoice_id > 0` is a range, which the database measures on the index itself, so the join reads only the assets made from an invoice whatever the statistics say. Run with none, 3,000 and 21,000 of the 25,000 assets made from an invoice: the same counts as before each time, and 21 to 32 ms. The queries that look assets up by one invoice (`productInvoiceHardware()`, the invoice PDF's serials) give the database a constant and were never affected. ## [26.11.7] diff --git a/agent/invoices.php b/agent/invoices.php index b8fc2dffa..0a8915e19 100644 --- a/agent/invoices.php +++ b/agent/invoices.php @@ -52,7 +52,7 @@ $recurring_filter_row = $recurring_filter ? billingRow('recurring', $recurring_f // Hardware sold on them that isn't on the client's assets yet (functions/products.php) $hardware_filter = !empty($_GET['hardware']); $hardware_query = $hardware_filter ? "AND " . productHardwareInvoiceSql() . " AND invoices.invoice_id IN (SELECT invoice_items.item_invoice_id FROM invoice_items - JOIN products ON products.product_id = invoice_items.item_product_id WHERE " . productHardwareShortSql() . ")" : ''; + JOIN products ON products.product_id = invoice_items.item_product_id " . productHardwareMadeJoinSql() . " WHERE " . productHardwareShortSql() . ")" : ''; // Category Filter $category_filter = intval($_GET['category'] ?? 0); diff --git a/functions/products.php b/functions/products.php index a9710aa7e..8689e4aad 100644 --- a/functions/products.php +++ b/functions/products.php @@ -657,11 +657,33 @@ function productHardwareInvoiceSql() { return "invoices.invoice_status NOT IN ('Draft', 'Cancelled') AND invoices.invoice_recurring_invoice_id = 0"; } -/** Lines with units still to add to assets - a condition on invoice_items joined to products. */ +/* + * How many assets each invoice line has made, in SQL - a LEFT JOIN onto + * invoice_items giving hardware_made.made (NULL for none). With it in the + * FROM, productHardwareShortSql() is the condition. + * + * Counted once, over the assets made from an invoice, and joined - not a + * COUNT(*) a line. A subquery a line looks its assets up by the line's + * invoice, and the database picks how by asset_invoice_id's statistics; on + * an install where few or none of the assets came from an invoice - most of + * them, as assets are made by hand - nearly every asset has 0 there, the + * statistics say a lookup finds every asset, and the database read the whole + * assets table once for every hardware line. That was the dashboard's + * Hardware not on assets: 22 seconds on a test install with 25,000 assets + * and two years of hardware sales. asset_invoice_id > 0 is a range the + * database measures on the index itself, so it reads only the assets made + * from an invoice, however its statistics stand. + */ +function productHardwareMadeJoinSql() { + return "LEFT JOIN (SELECT asset_invoice_id AS made_invoice_id, asset_invoice_item_id AS made_item_id, COUNT(*) AS made FROM assets + WHERE asset_invoice_id > 0 AND asset_invoice_item_id > 0 GROUP BY asset_invoice_id, asset_invoice_item_id) AS hardware_made + ON hardware_made.made_item_id = invoice_items.item_id AND hardware_made.made_invoice_id = invoice_items.item_invoice_id"; +} + +/** Lines with units still to add to assets - a condition on invoice_items joined to products and productHardwareMadeJoinSql(). */ function productHardwareShortSql() { return productHardwareSql() . " AND invoice_items.item_quantity > 0 - AND CEIL(ROUND(invoice_items.item_quantity, 2)) > (SELECT COUNT(*) FROM assets - WHERE assets.asset_invoice_id = invoice_items.item_invoice_id AND assets.asset_invoice_item_id = invoice_items.item_id)"; + AND CEIL(ROUND(invoice_items.item_quantity, 2)) > COALESCE(hardware_made.made, 0)"; } /** @@ -671,15 +693,14 @@ function productHardwareShortSql() { function productHardwarePending($days = 90) { global $mysqli; $days = max(1, intval($days)); - $row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT COALESCE(SUM(units - made), 0) AS units, COUNT(DISTINCT invoice_id) AS invoices FROM ( - SELECT invoice_id, CEIL(ROUND(item_quantity, 2)) AS units, - (SELECT COUNT(*) FROM assets WHERE asset_invoice_id = invoice_id AND asset_invoice_item_id = item_id) AS made - FROM invoice_items - JOIN invoices ON invoice_id = item_invoice_id - JOIN products ON product_id = item_product_id - WHERE " . productHardwareSql() . " AND item_quantity > 0 AND " . productHardwareInvoiceSql() . " - AND invoice_date BETWEEN CURDATE() - INTERVAL $days DAY AND CURDATE() " . clientScopeSql('invoice_client_id') . " - ) AS hardware WHERE units > made")); + $row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT COALESCE(SUM(CEIL(ROUND(invoice_items.item_quantity, 2)) - COALESCE(hardware_made.made, 0)), 0) AS units, + COUNT(DISTINCT invoices.invoice_id) AS invoices + FROM invoice_items + JOIN invoices ON invoices.invoice_id = invoice_items.item_invoice_id + JOIN products ON products.product_id = invoice_items.item_product_id + " . productHardwareMadeJoinSql() . " + WHERE " . productHardwareShortSql() . " AND " . productHardwareInvoiceSql() . " + AND invoices.invoice_date BETWEEN CURDATE() - INTERVAL $days DAY AND CURDATE() " . clientScopeSql('invoices.invoice_client_id'))); return ['units' => intval($row['units'] ?? 0), 'invoices' => intval($row['invoices'] ?? 0)]; }