Skip to main content

missing index on old store and suggestions for new index

3 hours 5 minutes ago #100000911 by dan@drumscore.com
-- HikaShop version -- : 6.8.3
-- Joomla version -- : 5.4
-- PHP version -- : 8.3
-- Browser(s) name and version -- : Firefox 161.0

We run HikaShop Business 6.8.3 on Joomla 5.4 MariaDB, on a shop that has been upgraded through many versions. While reviewing slow queries we found two things.

1. Possible upgrade bug: #__hikashop_order_product has no product_id index
tables.sql in 6.8.3 defines KEY `product_id` (`product_id`) on #__hikashop_order_product, but our database does not have it. Every other index in tables.sql is present, so it looks like this key was added to tables.sql for new installs without a matching ALTER in the update routine. Could you check, and add it for upgraded sites?

2. Suggested new indexes
Each of these columns is filtered on by core queries but has no usable index, so the query scans the whole table (confirmed with EXPLAIN). Timings are warm-cache, before -> after adding the index.

a) #__hikashop_file (file_ref_id, file_type)
  16k rows. Used to load images/files for products and categories, on nearly every front-end page. 11.4 ms -> 1.1 ms.

b) #__hikashop_category (category_parent_id) and (category_left)
  10k rows. Child-category listings and tree lookups. 13.7 ms -> 0.3 ms and 2.4 ms -> 0.1 ms.

c) #__hikashop_product_related (product_related_id, product_related_type)
  256k rows. The primary key starts with product_id, so reverse lookups scan the table, e.g. the bundle check in classes/cart.php (around line 3606: "pr.product_id IN (...) OR pr.product_related_id IN (...)"). 28.9 ms -> 0.4 ms.

d) #__hikashop_download (order_id)
  173k rows. The primary key is (file_id, order_id, file_pos), so lookups by order alone scan the table: views/user/view.html.php (around line 856, "WHERE order_id IN (...)") and classes/withdrawal.php (around line 164). 13.1 ms -> 0.5 ms.

e) #__hikashop_order (order_number)
  129k rows. Order lookup by number. 40.0 ms -> 0.3 ms.

f) #__hikashop_cart (cart_type, cart_modified)
  582k rows. The old-cart cleanup query in helpers/helper.php (around line 702: "WHERE cart_type IN ('cart','onepage') AND cart_modified < ... ORDER BY cart_modified ASC LIMIT ..."). 144.7 ms -> 0.7 ms.

g) #__hikashop_email_log (email_log_date)
  60k rows, 1.2 GB of data. The back-end email log listing sorted by date sorts the whole table. 299.2 ms -> 0.6 ms.

The statements we used:
ALTER TABLE #__hikashop_file            ADD INDEX file_ref (file_ref_id, file_type);
ALTER TABLE #__hikashop_category        ADD INDEX category_parent (category_parent_id), ADD INDEX category_left (category_left);
ALTER TABLE #__hikashop_product_related ADD INDEX related_reverse (product_related_id, product_related_type);
ALTER TABLE #__hikashop_download        ADD INDEX download_order (order_id);
ALTER TABLE #__hikashop_order_product  ADD INDEX product_id (product_id);
ALTER TABLE #__hikashop_order          ADD INDEX order_number (order_number);
ALTER TABLE #__hikashop_cart            ADD INDEX cart_type_modified (cart_type, cart_modified);
ALTER TABLE #__hikashop_email_log      ADD INDEX email_log_date (email_log_date);

Two questions:
- Would you consider adding these to the core schema?
- If we add them ourselves in the meantime, will a future HikaShop update leave them alone, and is there a naming convention you would prefer so they don't clash with indexes you add later?

Thank you.

Please Log in or Create an account to join the conversation.

Time to create page: 0.049 seconds
Powered by Kunena Forum