Skip to content

Conversation

@ChristophWurst
Copy link
Member

@ChristophWurst ChristophWurst commented Mar 12, 2025

  • Resolves: #

Summary

Search in in addressbooks dispatches a query like

SELECT DISTINCT `cp`.`cardid`
FROM `oc_cards_properties` `cp`
WHERE (`cp`.`addressbookid` IN (18))
  AND (`cp`.`name` IN ('FN'))
  AND (`cp`.`value` COLLATE utf8mb4_general_ci LIKE '%john%')
LIMIT 20

MariaDB picks card_name_index, which only covers name, and still has to scan 712 index entries and then do a table access.

{
  "query_optimization": {
    "r_total_time_ms": 0.144922892
  },
  "query_block": {
    "select_id": 1,
    "cost": 0.6448338,
    "r_loops": 1,
    "r_total_time_ms": 1.652976417,
    "temporary_table": {
      "nested_loop": [
        {
          "table": {
            "table_name": "cp",
            "access_type": "range",
            "possible_keys": ["card_name_index"],
            "key": "card_name_index",
            "key_length": "259",
            "used_key_parts": ["name"],
            "loops": 1,
            "r_loops": 1,
            "rows": 712,
            "r_index_rows": 712,
            "r_rows": 712,
            "cost": 0.6448338,
            "r_table_time_ms": 1.404896605,
            "r_other_time_ms": 0.240330463,
            "r_engine_stats": {
              "pages_accessed": 1426
            },
            "filtered": 100,
            "r_total_filtered": 0.140449438,
            "index_condition": "cp.`name` = 'FN'",
            "r_icp_filtered": 100,
            "attached_condition": "cp.addressbookid = 18 and cp.`value` collate utf8mb4_general_ci like '%user0%'",
            "r_filtered": 0.140449438
          }
        }
      ]
    }
  }
}

Look at the r_rows

With the new query everything but the cardid is covered. It takes 1 index access and one table access to find the result.

{
  "query_optimization": {
    "r_total_time_ms": 0.258566593
  },
  "query_block": {
    "select_id": 1,
    "cost": 0.6448338,
    "r_loops": 1,
    "r_total_time_ms": 0.712225566,
    "temporary_table": {
      "nested_loop": [
        {
          "table": {
            "table_name": "cp",
            "access_type": "range",
            "possible_keys": ["card_name_index", "cards_prop_abid_name_value"],
            "key": "cards_prop_abid_name_value",
            "key_length": "267",
            "used_key_parts": ["addressbookid", "name"],
            "loops": 1,
            "r_loops": 1,
            "rows": 638,
            "r_index_rows": 638,
            "r_rows": 1,
            "cost": 0.6448338,
            "r_table_time_ms": 0.672583116,
            "r_other_time_ms": 0.029195924,
            "r_engine_stats": {
              "pages_accessed": 6
            },
            "filtered": 100,
            "r_total_filtered": 0.156739812,
            "index_condition": "cp.addressbookid = 18 and cp.`name` = 'FN' and cp.`value` collate utf8mb4_general_ci like '%user0%'",
            "r_icp_filtered": 0.156739812,
            "r_filtered": 100
          }
        }
      ]
    }
  }
}

I have also experimented with cardid but then MariaDB no longer uses the index. This is likely because a custom collation is used on value, so query optimizer sees the index as not worthy.

TODO

  • Make the changes
  • Test the changes

How to test

  1. Check out the branch
  2. Run example queries with EXPLAIN

Checklist

@ChristophWurst ChristophWurst self-assigned this Mar 12, 2025
@ChristophWurst ChristophWurst requested a review from a team as a code owner March 12, 2025 12:14
@ChristophWurst ChristophWurst requested review from ArtificialOwl, skjnldsv and sorbaugh and removed request for a team March 12, 2025 12:14
@ChristophWurst
Copy link
Member Author

/backport to stable31

@ChristophWurst
Copy link
Member Author

/backport to stable30

@ChristophWurst
Copy link
Member Author

/backport to stable29

@ChristophWurst
Copy link
Member Author

@ArtificialOwl @skjnldsv @sorbaugh 🏓 for review

@kesselb kesselb merged commit cc5ca58 into master Apr 24, 2025
190 checks passed
@kesselb kesselb deleted the perf/db/cards-properties-abid-name-value-idx branch April 24, 2025 11:33
@nextcloud-bot nextcloud-bot mentioned this pull request Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants